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.

Say I have a simple game with a gameLoop function that gets called every 50 milliseconds or something similar, and I have a box2d object. Is it bad practice/too CPU intensive to use SetLinearVelocity on the object every time the gameLoop is called?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

It's only bad if you find this to be detrimental to the performance of your game. I find it unlikely that setting the velocity directly would be very expensive.

If you do find that this is an issue, by profiling the code, you can create a simple function that only updates the box2D object's velocity if your external velocity value has changed. Something like:

if(velocity != oldVelocity) {
    box2DObject.SetLinearVelocity(velocity);
    oldVelocity = velocity;
}
share|improve this answer
    
Many thanks for the help! –  EpicPineapple Mar 19 '13 at 22:12
1  
Most physics engines don't like it much if you're setting velocity directly, instead of applying accelerations (or even better, impulses); it often leads to bad behaviour in collisions. But no, there's no CPU performance reason not to do it. –  Trevor Powell Mar 19 '13 at 22:26
    
Make sure its a kinematic bodytype and the are no worries about the engine handling it incorrectly. –  ClassicThunder Mar 20 '13 at 0:20
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.