Force independent of timestep

Hello,

I am newby towards box2d... I just tried the manual tutorial with the gravity set to -10.0.

I am not completely happy with the tutorial... mostly I am unhappy with the way the force value is supplied.

The problem seems to be when I change the timestep the force values react differently.

I would like to be able to change the timestep in case I need to do so in the future for performance reasons.

And it would be nice if the forces keep reacting somewhat similiarly.

I read a post about how force is applied over time... so it seems to make sense to calculate force value's as follows:

TimeIndepedentForceValue = ForceValue * Timestep;

A timestep is x-th of a second... for example 1/60. (60 being the frequency).

So to me it seems to make sense to supply force values in "force per second"... and then simply adjust it to:

ForceValue/Frequency or simply:
ForceValue * Timestep;

Would that make sense ?!?

Furthermore I am not completely happy with the tutorial... especially when it comes to the settings and the gravity...

The little box doesn't seem to fall realistically... I am not sure if it's because of wrong tutorial settings or if it's because it's in meters pretending to be a high building ?!?

It would be nice if a tutorial could show a bouncing ball realistically... just to add confidence towards box2d having somewhat realistic physics.

Also it doesn't seem to explain how to draw manually instead of debugdraw... but I figured it out... it's using the transform matrix... xf to multiply with the vertices... so I managed to figure that out... (I am using Delphi version by the way 😉 which has the GetTransform method for Bodie's.)

Anyway please let me know if I am (somewhat) correct about how to specify "force indepedent values of timestep" and how to calculate timestep depedent values...

I shall continue experimenting with box2d.

I haven't tried the zoom/scale tip yet... maybe I have to do that manually or maybe opengl has a zoom functionality.

So far I used ortho2D to set viewport to -width/2, +width/2, height/2, -height/2.

I am not sure if this is pixel accurate anymore... it used to be pixel accurate with 0, width, height, 0.

Something I might need to look into.

Also I am not sure if I am actually going to use pixel-accurate graphics or something totally different...
but for now pixel-accurate seems nice for accuracy 😉 🙂

Bye,
Skybuck.
In Box2D, forces are already independent of the time step. However, the numerical integrator (http://en.wikipedia.org/wiki/Semi-implicit_Euler_method) is not.
So I take it that box2d uses it... I think I have seen it mentioned somewhere in the manual or faq... ?

So does this mean once a timestep is chosen there is no way around it/changing it without forces being wrong ?
The forces are not wrong. The solution to the differential equation for the position and velocity is not exact. The accuracy depends on the time step. Smaller time steps generally yield more accurate results.

Yes, there is a FAQ: http://code.google.com/p/box2d/wiki/FAQ

There is also a Forum FAQ that points to the FAQ: http://box2d.org/forum/viewtopic.php?f=3&t=558
It doesn't seem to be an accuracy problem... it's just way off...

The force needed to make the box go up again is somewhere in +50000 to +5000000.

I wouldn't call that an accuracy problem.

Maybe you should try the tutorial out yourself visually/graphically and then mess with the timestep.

And try to apply a force to the body... you'll see what I mean 😉
I'm a confused here.

Please paste your code that shows the problem.
When timestep changes from

1. mTimestep := 1.0 / 1.0;

to

2. mTimestep := 1.0 / 10.0;

to

3. mTimestep := 1.0 / 20.0;

to

3. mTimestep := 1.0 / 40.0

to

4. mTimestep := 1.0 / 60.0

During different tests/reruns the following happens:

The box reacts differently somewhat when the button2 is pressed.

With 1 the box almost doesn't go up.
With 2 the box goes up and spins real fast.
With 3 the box goes up a bit further and spins more slowly.
With 4 the box goes up a little bit and spins real slowly 360 degrees.
With 5 the box goes not really up and spins 90% degrees.

The frame rate during all tests is about 40.
(I am using a timer for now... maybe I will try multi-threading in the future.. I think
the timer is causing a slowdown to 40 fps)

So clearly the forces are having a different effect depending on the mTimestep.

Here is the code:

Code:
Form...
  public
   { Public declarations }
   mWorld : Tb2World;

   mInsideTimer : boolean;
   mTimeStep : double;
   mVelocityIterations : integer;
   mPositionIterations : integer;

   mGroundBodyDefinition : Tb2BodyDef;
   mGroundBody : Tb2Body;
   mGroundPolygon : Tb2PolygonShape;

   mDynamicBodyDefinition : Tb2BodyDef;
   mDynamicBody : Tb2Body;
   mDynamicPolygon : Tb2PolygonShape;
   mDynamicFixtureDefinition : Tb2FixtureDef;

   mApplyForce : boolean;

   mFrames : integer;
   mSecondTick1 : int64;
   mSecondTick2 : int64;
   mSecondFrequency : int64;
   mSecondInterval : int64;
   mSecond : double;
  end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   mApplyForce := true;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
   vGravity : Tvector2;
begin
   QueryPerformanceFrequency( mSecondFrequency );
   QueryPerformanceCounter( mSecondTick2 );
   mSecondTick1 := mSecondTick2;
   mTimeStep := 1.0 / 20.0; // unrelated to frame rate, this is simple simulation rate
   mVelocityIterations := 10; // number of passes to correct velocities/apply forces on bodies, higher is better accuracy but costs more cpu.
   mPositionIterations := 10; // number of passes to correct positions on bodies, higher is better accuracy but costs more cpu.

   vGravity.x := 0;
   vGravity.y := -10.0;

   // create the world
   // sleep is true indicates bodies may rest and don't need to be simulated.
   mWorld := Tb2World.Create( vGravity, true );

   // create the static ground body

   // create a body definition
   mGroundBodyDefinition := Tb2BodyDef.Create;

   // set the position
   mGroundBodyDefinition.position.SetValue( 0.0, -10.0 );

   // now create the body based on the definition, false
   mGroundBody := mWorld.CreateBody( mGroundBodyDefinition, false );

   // now create a ground polygon, apperently automatically freed as well.
   mGroundPolygon := Tb2PolygonShape.Create;

   // use set as box shortcut to form a box on the center of the parent body.
   mGroundPolygon.SetAsBox( 50.0, 10.0 );

   // create a fixture by using a shortcut/predefined fixture definition
   mGroundBody.CreateFixture( mGroundPolygon, 0 );

   // create the dynamic body
   mDynamicBodyDefinition := Tb2BodyDef.Create;

   // set the body type to dynamic
   mDynamicBodyDefinition.bodyType := b2_dynamicBody;

   mDynamicBodyDefinition.position.SetValue( 0.0, 100.0 );

   mDynamicBody := mWorld.CreateBody( mDynamicBodyDefinition, true );

   mDynamicPolygon := Tb2PolygonShape.Create;
   mDynamicPolygon.SetAsBox( 5.0, 5.0 );

   mDynamicFixtureDefinition := Tb2FixtureDef.Create;

   mDynamicFixtureDefinition.shape := mDynamicPolygon;
   mDynamicFixtureDefinition.density := 1.0;
   mDynamicFixtureDefinition.friction := 0.3;

   mDynamicBody.CreateFixture( mDynamicFixtureDefinition );
end;

// timer seems to fuck up but no problem... next version I move the engine
// code into a thread maybe that will be better.
procedure TForm1.Timer1Timer(Sender: TObject);
var
   vForce : Tvector2;
   vPoint : Tvector2;
begin
   if not mInsideTimer then
   begin
      mInsideTimer := true;

      mFrames := mFrames + 1;

      if mApplyForce then
      begin
         vForce.x := 0;
         vForce.y := +50000;

         vPoint.x := mDynamicBody.GetPosition.x+2;
         vPoint.y := mDynamicBody.GetPosition.y-4;
         mDynamicBody.ApplyForce( vForce, vPoint );
         mApplyForce := false;
      end;

      mWorld.Step( mTimeStep, mVelocityIterations, mPositionIterations );
      mWorld.ClearForces;
      mInsideTimer := false;
      OpenGLControl1.Repaint;

      QueryPerformanceCounter( mSecondTick2 );
      mSecondInterval := mSecondTick2 - mSecondTick1;

      if mSecondInterval > mSecondFrequency then
      begin
         mSecond := mSecondInterval / mSecondFrequency;
         Label1.Caption := FloatToStr( mFrames / mSecond ) + ' frames per second';
         mSecondTick1 := mSecondTick2;
         mFrames := 0;
      end;
   end;
end;


(I didn't post the painting of the polygons/lines I don't think that has anything to do with it. But in case you wanna know
this is how the verteces are calculated in loops etc

Code:
// get transform retrieves body.xf
vTempVertices[vVertexIndex] := b2Mul( vBody.GetTransform^, vPolygonShape.m_vertices[vVertexIndex] );
OpenGLAPI.glVertex2d( vTempVertices[vVertexIndex].x, vTempVertices[vVertexIndex].y );


Bye,
Skybuck.
I have deleted the timer and made a multi threaded version. Just one thread for (GUI+OpenGl) and one thread for the physics simulator "game engine".

So in total there are two threads.

The maximum frame rate achieveable with this application/opengl/physics simulator/tutorial seems to be 9000 frames per second on AMD X2 3800+ dual core.

If I change the timestep from 1/60 to 1/6000 or 1/9000 it's definetly noticeable that the forces act different than when it runs at 60 fps with timestep 1/60.

So there is definetly something weird going on with your physiscs library/simulator... I kinda used to having physics related stuff work with similiar values no matter what the frame rate is... at least I think so.. so I find this kinda weird.

I hope you will check it out and come back to me to confirm if it's a problem with the library or if something else is wrong... or if there is a work around...

Otherwise I will probably need to make up my mind on an appriorate timestep ?!

It's kinda preventing me from going forward... if I choose wrong timestep I might be screwed later on... 30 seems nice... but the monitor runs on 60...

Maybe 30 might not be smooth enough in the future with fast moving stuff... so aiming for 60 might be nice but then I get worried about potential performance issue's...

For now I will go forward with benchmarking your physics library I guess 😉 🙂

To see what it can do... I'm kinda curious how many boxes it can handle on the screen.

If necessary maybe I try and give it an optimization attempt via something else than a bsp 😉 🙂

Bye,
Skybuck.
Why do you use such a large force? Try applying a smaller force at the center of the box. You should only need a force of 250 to overcome gravity. Try applying the force at the center of the box using b2Body::GetWorldCenter.

A force only affects acceleration and not velocity directly. So if you want to see the same effect on velocity, you should use an impulse.

You can get the same force to have the same effect for different time steps if you apply the force for the same about of time. For example, these two cases are equivalent:

time step = 0.5, force = 1 applied over 10 time steps
time step = 0.25, force = 1 applied over 20 time steps
I think I figured out part of it why such big force was necessary... I kinda suspected it already.

The tutorial sets a box of 5.0 by 5.0 it mentions the units are in meters... so this would be as large as a house... so that's why the force had to be so big.

I made the box smaller like 0.2 by 0.2 and now only a little force of 250 or so is needed... GetPosition works and GetWorldPosition works as well.

This still doesn't explain the different behaviours... but I guess that has to do with the timestep... and how the force is applied.

The force is only applied for one step... I would like it to act like a kick against the box.

So I guess if the timestep changes the force changes as well... ? the integrator/library will consider it a bigger force if the timestep is bigger, correct ?

So I guess I have to compensate forces for the timestep... I'll try that out but I don't think it's gonna work because I already noticed weird behaviour but gonna try it anyway... it's late so I am really tired unfortunately 😉 🙂

Also in your example it was:

TimeStep 0.5 so that would be 2 steps per second.
or
TimeStep 0.25 so that would be 4 steps per second.

So if a force of 1 was applied and I wanted to correct it... for example I want the force to be applied for 0.5 seconds...

Then in example 1 I would need a force of 1.0... for 0.5 seconds.

In example two the force would need to be half that 0.5 for 0.25 seconds... and another 0.5 for another 0.25 seconds ?!?

Would that be exactly the same ? or would it be different ? I think it would probably be different ? 😉 But closer ?

Bye,
Skybuck.

Original thread: https://box2d.org/forum/viewtopic.php?t=5422