Monday, March 5, 2018

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



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...