Start with Chipmunk Pro and Cocos2D

Official forum for the Chipmunk2D Physics Library.
Post Reply
linknik
Posts: 3
Joined: Sun Aug 12, 2012 11:46 am
Contact:

Start with Chipmunk Pro and Cocos2D

Post by linknik »

Hello everyone,
i have configured successfully Cocos2D and Chipmunk Pro.
But i have no idea, how make a sprite with physics.
Also i have not problems to define space, bodies, shapes etc.

Here is a code snippet:

Code: Select all

spriteTexture = [[CCTextureCache sharedTextureCache] addImage:@"Icon.png"];
        
        float moment = cpMomentForBox(1.0f, self.contentSize.width, self.contentSize.height);
        
        ChipmunkBody *body = [[ChipmunkBody alloc] initWithMass:1.0f andMoment:moment];
        
        body.pos = ccp(120, 120);
        body.data = spriteTexture;
        [space add:body];
        
        //row 1, col 1
        int num = 0;
        
        ChipmunkPolyShape *shape = [[ChipmunkPolyShape alloc] initWithBody:body count:num verts:NULL offset:CGPointZero];
        shape.elasticity = 0.5f;
        shape.friction = 0.5f;
        [space add:shape];
But the Screen is black of the iphone.
Also i have implemented a update function.

Have someone a solution?

greeting linknik
Vico
Posts: 13
Joined: Fri Jul 06, 2012 10:12 am
Contact:

Re: Start with Chipmunk Pro and Cocos2D

Post by Vico »

Hi,

There is nothing to be displayed on your screen since you didn't have added any visible object to the scene.
What you did is creating a texture (not a sprite), then you add a body and a shape to the space who are both invisible objects. Assigning the texture to the body data property will not add it to the scene. You have to do it manually.

Keeping the same code, just create a CCSprite with a method from your choice instead of a texture, assign it to the body's data property if needed (optional), and add it to the scene with addChild:(CCNode*)

Don't forget you'll have to update the sprite position/rotation values according to the body values to see it acting as in the physical space.

Does it sound clear to you ? There are some tutorials and example codes that should be a good starting point:

http://chipmunk-physics.net/documentation.php
or
http://www.alexandre-gomes.com/articles/chipmunk/
linknik
Posts: 3
Joined: Sun Aug 12, 2012 11:46 am
Contact:

Re: Start with Chipmunk Pro and Cocos2D

Post by linknik »

Yeah ! Thanks to you, i have understand it now ;)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Start with Chipmunk Pro and Cocos2D

Post by slembcke »

You can (and I'd really recommend that you do) avoid needing to manually sync the bodies pretty easily with Chipmunk Pro. For Cocos2D 1.x, I have the ChipmunkCocosBody class, which you can find in the Angry Chipmunks project. In Cocos2D 2.x I worked with Riq to make sprites subclassable and implement them to automatically track a physics body. That should be included with the example projects in Cocos2D now.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
linknik
Posts: 3
Joined: Sun Aug 12, 2012 11:46 am
Contact:

Re: Start with Chipmunk Pro and Cocos2D

Post by linknik »

Okay, nice hint :D I will work with it ;) It has been a big help.
Post Reply

Who is online

Users browsing this forum: Heise IT-Markt [Crawler] and 13 guests