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

OR

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

+2


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.

" http://groups.google.com/group/cocos2d-iphone-discuss/browse_thread/thread/ae37e90eac059135/081da9bcefec76ba?#081da9bcefec76ba "



" http://www.cocos2d-iphone.org/forum/topic/877 "

" http://jeanbombeur.com/content/2009-07-08-synchronizing-sprites-positions-and-rotations-between-chipmunk-and-cocos2d "

Welcome and good luck!

PS
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.

+3


source


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.

+1


source







All Articles