7

I'm using box2dweb version 2.1.a.3 (javascript, ported from flash), to create game. Some examples that I've got from Google used:

setInterval(
     function(){
          world.Step(1/60 , 10, 10)
          world.ClearForces()
     }
,1000/60)

I tried to remove the line world.ClearForces() but things behaved the same. I wonder what function ClearForces() does? What trouble can I get if I remove it like that ? Thanks!

vantrung -cuncon
  • 10,207
  • 5
  • 47
  • 62
  • 2
    From the C++ version: http://code.google.com/searchframe#ncwl7ziM06g/trunk/Box2D/Box2D/Dynamics/b2World.h&q=ClearForces%20package:box2d%5C.googlecode%5C.com&l=98 – Matt Ball Jan 24 '13 at 05:22
  • @MattBall : thanks, it's, no doubt, the most correct answer, but I wonder if it was easy-2-understand for me & some others who'll read this question later. Anyway, thanks so much. – vantrung -cuncon Jan 24 '13 at 06:48

1 Answers1

4

I can't say for sure about the Flash and Javascript versions, but the ClearForces function was originally necessary in early versions of Box2D. Back then if you did ApplyForce to move an object, that force would remain in effect indefinitely, but now you need to do ApplyForce every time step if you want a continuous force. So effectively, the engine is calling this ClearForces for you every step. If you can take it out without changing anything you might as well.

iforce2d
  • 8,194
  • 3
  • 29
  • 40
  • I've tried to put a ApplyForce-command before-and-after the ClearForce command, and I've figured out the differences. Thanks so much ! – vantrung -cuncon Jan 24 '13 at 07:05