# When and how should I apply forces in a Cocos2D + Box2D game?

I have some small circles just rolling across the bottom of the screen in my Cocos2D/Box2D iOS app. The bodies are dynamic... so I make them roll by applying a horizontal force to the center of mass (when the linear velocity is below the max velocity).

Currently I apply this force in -(void) tick: (ccTime) dt. Is this the best place to apply it? Or should I subclass something and implement an update function somewhere? Please keep in mind I'm very new to Cocos2D/Box2D.

To achieve a constant acceleration, apply a force every frame some time before you call b2World::Step. To achieve constant velocity (in the absence of friction and damping), apply a force for one frame.

You should definitely not be subclassing anything in Box2D. None of its classes are designed to be base classes. You can subclass CCNode/CCSprite/etc. to help manage your b2Body pointers.

Incidentally, applying a force to the exact center of a circle will cause it to slide, not roll. To roll, apply the force above the center.

The tick method is called on every frame. So this is actually what you call your update function.

So calling the apply force method in the tick method is right, since the force has to be applied continuously (every frame) to make the object move.

As a contrast you could try the apply impulse method that accelerates your object. But you should not call this method in the tick method because the impulse would be added many times.

• Applying a force every frame will result in an acceleration every frame. Even if no force is applied in a frame, a force from a previous frame may still impart velocity, i.e. movement. – user744 Jul 2 '11 at 12:50