Cocos2d iphone - game architecture: physics VS. code simulated sprite behavior

Hello stackoverflow community!

I am trying to figure out how to architect my game with cocos2d.

My problem is that the cocos2d physics engine (I'm talking about chipmunk) lies in the world behind the sprites.

When I move the sprite I wonder if I should

* 1) move it by applying force to the physical body behind it


2) if I should just simulate motions through code then only use physics on collision. *

I know that you can tell me that. But what was the intended behavior as a cocos2d iphone?

I thank you in advance <3


source to share

2 answers

To keep it within the chipmunk, you will apply forces / impulses. There are examples of people taking control of the movement of a sprite with their own code, forcing the body to follow the sprite and then returning the body back to modeling the chipmunk.

Quite a few people also just use chipmunk for collision detection and sprite manipulation with their own code. This can be done by adding a body to the chipmunk space, but not adding shape to the space.

Here are some discussions on the subject .. I cannot post them as hyperlinks because I am a new user.

" "

" "

" "

Welcome and good luck!

There is also a way to enable Chipmunk mode, which gives you a little more freedom over what you can control, but at the cost of a more realistic simulation.



For the sprites that you want to keep in line with the shapes, you need to bind the sprite as shape-> data when you create the shape.

Then use each form function

cpSpaceHashEach (cosmos> activeShapes, & functioncallback, data)

In your callback function using shape-> data you can update the position of the sprites to the position of the body attached to the shape

Sprite s = shape->data
[s setPosition:cpv(body->p.x,body->p.y)];


use Cocos schedule to call cpSpaceStep () and cpSpaceHashEach (). Make sure you don't call them every frame, or you will have trouble syncing things up.

I use

[self schedule:@selector(step:) interval:(1.0f/60.0f)/3]


This way my sprites follow chimpmunk data and do physics with no extra work from me.



All Articles