In general, you use Box2D to simulate the physical behavior of objects in relation to each other. The rules of mechanics implemented by Box2D dictate how your cocos2d CCSprite
s move if you continuously update the translation and rotation of your sprites according to their corresponding Box2d b2Body
. You will have some kind of repeatedly invoked tick:
method in which you step
your Box2d world along in time, and in which you update your sprite positions according to simulation results of Box2d.
This pattern corresponds to b2Body
s of type b2_dynamicBody
. Physical laws dictate the motion of the body in this case, and your sprites will follow these simulation results. This is why setting a conflicting position of a sprite by means of a CCAction
or even directly will be undone almost instantaneously with the next tick:
.
Solution 1: kinematic body type
However, there do exist other modes for a b2Body
, and one of these is b2_kinematicBody
in which the translation is no longer governed by the world but by the velocities or angular speeds you dictate through setters. So it would be one solution to work with body type b2_kinematicBody
as in:
b2BodyDef bdef;
bdef.type = b2_kinematicBody;
With this you would manipulate the velocity and angular speed of a b2Body
explicitly. In this case, Box2d is still responsible for moving bodies around, but according the velocities you dictate, and without any force effects of collision or gravity applied to this particular body. Also with this strategy, your CCSprite
s will follow b2Body
s. Setting conflicting positions for your sprites directly or by applying a CCAction
would not make sense for the same reason as described above.
Solution 2: decouple sprites from Box2d
An alternative way to animating sprites would be to fully decouple those sprites from Box2d. In this case, you would simply not have any b2Body
that governs the position of the sprite you are going to animate. Now, in this case, only you will dictate the position and rotation of your CCSprite
, i.e. directly either through its position
property or by applying a CCAction
.