Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.