Beginner Question

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Beginner Question

Postby dxtzero » Wed Dec 14, 2016 8:05 pm

Hello everyone!

I have this code:
Code: Select all
void apply_force_and_torque (const NewtonBody* const body, dFloat timestep, int threadIndex)
{
   float gravity = 10.0f;

   float force[3] =
   {
      0.0f,
      gravity,
      0.0f,
   };

   NewtonBodySetForce (body, force);
}

void newton_test (int steps_per_second)
{
   float transformation_matrix[16] =
   {
      1.0f, 0.0f, 0.0f, 0.0f,
      0.0f, 1.0f, 0.0f, 0.0f,
      0.0f, 0.0f, 1.0f, 0.0f,
      0.0f, 0.0f, 0.0f, 1.0f
   };
   
   NewtonWorld* const world = NewtonCreate ();
   NewtonCollision* const collision = NewtonCreateBox (world, 10, 10, 10, 0, NULL);
   NewtonBody* const body = NewtonCreateDynamicBody (world, collision, transformation_matrix);

   NewtonBodySetForceAndTorqueCallback (body, apply_force_and_torque);
   NewtonBodySetMassProperties (body, 10, collision);

   int i;
   for (i = 0; i < 10 * steps_per_second; i++)
   {
      NewtonUpdate (world, 1.0f / steps_per_second);
   }

   printf ("%d steps at %d steps per second (%.2f seconds)\n", i, steps_per_second, (1.0f * i / steps_per_second));

   float velocity[3];
   NewtonBodyGetVelocity (body, velocity);
   printf ("velocity: %.2f\n", velocity[1]);

   NewtonBodyGetMatrix (body, transformation_matrix);
   printf ("position: %.2f\n\n", transformation_matrix[13]);

   NewtonDestroyAllBodies (world);
   NewtonDestroy (world);
}

int main (int argc, const char * argv[])
{
   newton_test (50);
   newton_test (60);
   newton_test (100);
   newton_test (120);

   return 0;
}


with this output:
Code: Select all
500 steps at 50 steps per second (10.00 seconds)
velocity: 6.68
position: 38.26

600 steps at 60 steps per second (10.00 seconds)
velocity: 5.70
position: 34.37

1000 steps at 100 steps per second (10.00 seconds)
velocity: 2.78
position: 20.76

1200 steps at 120 steps per second (10.00 seconds)
velocity: 1.98
position: 16.02


I'm wondering, since the world is being simulated for the same total amount of time for each timestep, shouldn't the velocity and postion of the body end at about the same value for each?

I assume I'm missing something, but I don't know what.
dxtzero
 
Posts: 3
Joined: Tue Dec 13, 2016 5:48 pm

Re: Beginner Question

Postby Julio Jerez » Wed Dec 14, 2016 9:04 pm

No that's the problem with numerical integration, it is not exact.
however you have a different problem there, you need to set linear damping to zero after you create the body and the difference should be closer.
Code: Select all
NewtonBodySetLinearDamping (body, 0.0f);

what version of the engine are you using?

also you should no think of running a any numerical integrating system as variable frame rate. Numerical integration problems become much worse with variable time steps because tehr are many non lineal forces acting on the system.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Beginner Question

Postby dxtzero » Wed Dec 14, 2016 10:34 pm

thanks!

i'm using whatever the current version on github is. also, i wasn't thinking of running with a variable frame rate, just wanted to see how things looked with a few different constant rates :)
dxtzero
 
Posts: 3
Joined: Tue Dec 13, 2016 5:48 pm

Re: Beginner Question

Postby Julio Jerez » Thu Dec 15, 2016 12:23 am

try NewtonBodySetLinearDamping the difference should be much closer, almost zero.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Beginner Question

Postby dxtzero » Thu Dec 15, 2016 2:40 am

i did:

Code: Select all
500 steps at 50 steps per second (10.00 seconds)
velocity: 10.00
position: 50.10

600 steps at 60 steps per second (10.00 seconds)
velocity: 10.00
position: 50.08

1000 steps at 100 steps per second (10.00 seconds)
velocity: 10.00
position: 50.05

1200 steps at 120 steps per second (10.00 seconds)
velocity: 10.00
position: 50.04


much closer to what i was expecting!
dxtzero
 
Posts: 3
Joined: Tue Dec 13, 2016 5:48 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 19 guests

cron