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?
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;
}
-
1\$\begingroup\$ 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. \$\endgroup\$ Mar 19 2013 at 22:26
-
\$\begingroup\$ Make sure its a kinematic bodytype and the are no worries about the engine handling it incorrectly. \$\endgroup\$ Mar 20 2013 at 0:20