Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-25-2010, 05:33 AM   #1 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Johanovski is on a distinguished road
Default Help using Cocos2D from the template!

Hi there!

I'm exploring Cocos2D (with Chipmunk integrated) and it looks really interesting! However it's hard to understand how it works because I'm not able to find any kind of method's documentation and the methods itself aren't very explicit with their parameters (of course I know there are lots of tutorial that begin from the very beginning, but I just want documentation for some specific methods). I have a game scene with a square affected by gravity and another surface that is not affected by gravity (so it's a solid platform). The problem is that the dynamic square doesn't "touch" the static one and it goes through it as it doesn't even exists... How can you make a shape, let them static in the screen, but make it affect other shapes?
Here is how I modified the method to difference between a static and a dynamic shape:

Code:
-(void) addNewSpriteX: (float)x y:(float)y tag:(int)t isDynamic:(BOOL)dynamic
{
	int posx, posy;
	int width, height;
	posx = 0;
	posy = 0;
	width = 0;
	height = 0;
	CGPoint verts[4];
	
	if (t == kTagAtlasSpriteSheetBox) {
		width = 32;
		height = 32;
		
		verts[0] = ccp(-16,-16);
		verts[1] = ccp(-16, 16);
		verts[2] = ccp( 16, 16);
		verts[3] = ccp( 16,-16);
	}else if (t == kTagAtlasSpriteSheetBlock) {
		width = 32;
		height = 32;
		
		verts[0] = ccp(-16,-16);
		verts[1] = ccp(-16, 16);
		verts[2] = ccp( 16, 16);
		verts[3] = ccp( 16,-16);
	}
	
	CCSpriteSheet *sheet = (CCSpriteSheet*) [self getChildByTag:t];	
	
	CCSprite *sprite = [CCSprite spriteWithSpriteSheet:sheet rect:CGRectMake(posx, posy, width, height)];
	[sheet addChild: sprite];
	
	sprite.position = ccp(x,y);
	
	int num = 4;
	
	if (dynamic) {
		// New DYNAMIC object
		cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, CGPointZero));
		
		// TIP:
		// since v0.7.1 you can assign CGPoint to chipmunk instead of cpVect.
		// cpVect == CGPoint
		body->p = ccp(x, y);
		cpSpaceAddBody(space, body);
		
		cpShape* shape = cpPolyShapeNew(body, num, verts, CGPointZero);
		shape->e = 0.5f; shape->u = 0.5f;
		shape->data = sprite;
		cpSpaceAddShape(space, shape);
	}else {
		// New STATIC object
		cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, CGPointZero));
		
		// TIP:
		// since v0.7.1 you can assign CGPoint to chipmunk instead of cpVect.
		// cpVect == CGPoint
		body->p = ccp(x, y);
		
		cpShape* shape = cpPolyShapeNew(body, num, verts, CGPointZero);
		shape->e = 1.0f; shape->u = 1.0f;
		shape->data = sprite;
		cpSpaceAddStaticShape(space, shape);
	}
}
A shape declared as static is not affected by gravity, but it doesn't interact with dynamic shapes... Anyone knows how this works?

Thanks in advance!
Johanovski is offline   Reply With Quote
Old 03-25-2010, 05:41 AM   #2 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Johanovski is on a distinguished road
Default

Uo! I think I've found it by myself! ^_^

The clue is when you declare the shape's body and you need to specify it's mass and moment. A static shape (completely static like a wall) should have and infinite mass and an infinite moment (I'm not sure about what the "moment" is, if someone knows it an explanation should be fine to help me understand why things are like they are), so in my code there is only need to change this:

Code:
        if (dynamic) {
		// new DYNAMIC object
		(...)
	}else {
		// New STATIC object

                // HERE IS THE CLUE!
		cpBody *body = cpBodyNew(INFINITY, INFINITY);
		
		// TIP:
		// since v0.7.1 you can assign CGPoint to chipmunk instead of cpVect.
		// cpVect == CGPoint
		body->p = ccp(x, y);
		
		cpShape* shape = cpPolyShapeNew(body, num, verts, CGPointZero);
		shape->e = 1.0f; shape->u = 1.0f;
		shape->data = sprite;
		cpSpaceAddStaticShape(space, shape);
	}
Hope it helped if anybody has the same problem!
Johanovski is offline   Reply With Quote
Reply

Bookmarks

Tags
chipmunk, cocos2d, dynamic, shapes, static

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 346
12 members and 334 guests
akacaj, c2matrix, cgokey, esoteric, givensur, HemiMG, Mirotion22, mjnafjke, Pudding, SLIC, Sonuye857, Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,652
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Sonuye857
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:17 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0