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?
Asked
Active
Viewed 287 times
1 Answers
2
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;
}

House
- 73,224
- 17
- 184
- 273
-
1Most 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 00:20