A place to discuss everything related to Newton Dynamics.
Moderators: Sascha Willems, walaber
by Rodion Karimov » Sat Jul 04, 2009 7:17 am
Hello, I'm trying to use Newton by only retriving velocity from it, I'm initializing Newton like in example SDLNewtonBasicDemo by Sascha Willems -
- Code: Select all
NewtonWorld := NewtonCreate ( nil, nil );
NewtonSetPlatformArchitecture ( NewtonWorld, 2 );
NewtonSetSolverModel ( NewtonWorld, 5 );
NewtonSetFrictionModel ( NewtonWorld, 0 );
with Scene.FirstRelief.FirstMovingObject ^ do
begin
Collision := NewtonCreateSphere ( NewtonWorld, 0.5, 0.5, 0.5, nil );
Bounding := NewtonCreateBody ( NewtonWorld, Collision );
NewtonReleaseCollision ( NewtonWorld, Collision );
Mass := 10.0;
Inertia [ 0 ] := Mass * 0.18 / 12.0;
Inertia [ 1 ] := Mass * 0.18 / 12.0;
Inertia [ 2 ] := Mass * 0.18 / 12.0;
NewtonBodySetMassMatrix ( Bounding, Mass,
Inertia [ 0 ], Inertia [ 1 ], Inertia [ 2 ] );
NewtonBodyGetMatrix ( Bounding, @ Matrix [ 0, 0 ] );
Matrix [ 3, 0 ] := TVec4 ( Coordinates ^ ) [ 0 ];
Matrix [ 3, 1 ] := TVec4 ( Coordinates ^ ) [ 1 ];
Matrix [ 3, 2 ] := TVec4 ( Coordinates ^ ) [ 2 ];
NewtonBodySetMatrix ( Bounding, @ Matrix [ 0, 0 ] );
NewtonBodySetForceAndTorqueCallBack ( Bounding, ForceAndTorqueCallBack );
end; //-with
with Scene.FirstRelief ^ do
begin
Collision := NewtonCreateBox ( NewtonWorld, 100.0, 0.5, 100.0, nil );
Bounding := NewtonCreateBody ( NewtonWorld, Collision );
NewtonReleaseCollision ( NewtonWorld, Collision );
NewtonBodySetMassMatrix ( Bounding, 0.0, 0.0, 0.0, 0.0 );
NewtonBodyGetMatrix ( Bounding, @ Matrix [ 0, 0 ] );
Matrix [ 3, 0 ] := 0.0;
Matrix [ 3, 1 ] := 1.23;
Matrix [ 3, 2 ] := 0.0;
NewtonBodySetMatrix ( Bounding, @ Matrix [ 0, 0 ] );
end; //-with
PreviousPhysicsUpdate := ReadTime;
Then before physics update I write
- Code: Select all
with Scene.FirstRelief.FirstMovingObject ^ do
begin
NewtonBodyGetVelocity ( Bounding, Velocity );
TVec4 ( Velocity ^ ) [ 3 ] := 0.0;
ADD_ASM ( Coordinates, Velocity );
end;
PhysicsUpdate := ReadTime;
NewtonUpdate ( NewtonWorld, PhysicsUpdate - PreviousPhysicsUpdate );
PreviousPhysicsUpdate := PhysicsUpdate;
and the sphere is continuosly falling down, but it must stop on the plane that is located on the height 1.23, height of the sphere is 5.0.
But when I'm writing
- Code: Select all
with Scene.FirstRelief.FirstMovingObject ^ do
begin
NewtonBodyGetVelocity ( Bounding, Velocity );
TVec4 ( Velocity ^ ) [ 3 ] := 0.0;
ADD_ASM ( Coordinates, Velocity );
NewtonBodyGetMatrix ( Bounding, @ Matrix [ 0, 0 ] );
Matrix [ 3, 0 ] := TVec4 ( Coordinates ^ ) [ 0 ];
Matrix [ 3, 1 ] := TVec4 ( Coordinates ^ ) [ 1 ];
Matrix [ 3, 2 ] := TVec4 ( Coordinates ^ ) [ 2 ];
NewtonBodySetMatrix ( Bounding, @ Matrix [ 0, 0 ] );
end;
PhysicsUpdate := ReadTime;
NewtonUpdate ( NewtonWorld, PhysicsUpdate - PreviousPhysicsUpdate );
PreviousPhysicsUpdate := PhysicsUpdate;
it is working as it must. Please tell me how to do it right to use only velocities of objects, without using matrices? Or it must be done with updating coordinates in physics engine? As far as I understand it must by itself update these coordinates?
Edit - Oh, I'm sorry I forgot to multiply velocity by time, so the code now looks so
- Code: Select all
with Scene.FirstRelief.FirstMovingObject ^ do
begin
NewtonBodyGetVelocity ( Bounding, Velocity );
TVec4 ( Velocity ^ ) [ 3 ] := 0.0;
PhysicsUpdate := ReadTime;
Time := PhysicsUpdate - PreviousPhysicsUpdate;
TVec4 ( Velocity ^ ) [ 0 ] := TVec4 ( Velocity ^ ) [ 0 ] * Time;
TVec4 ( Velocity ^ ) [ 1 ] := TVec4 ( Velocity ^ ) [ 1 ] * Time;
TVec4 ( Velocity ^ ) [ 2 ] := TVec4 ( Velocity ^ ) [ 2 ] * Time;
ADD_ASM ( Coordinates, Velocity );
end;
NewtonUpdate ( NewtonWorld, Time );
PreviousPhysicsUpdate := PhysicsUpdate;
but the problem is still the same.
-
Rodion Karimov
-
- Posts: 13
- Joined: Sat Jul 04, 2009 6:46 am
- Location: Russia
-
by JernejL » Sat Jul 04, 2009 10:31 am
You are not using newton properly, if you set the body matrix it will not respond to collisions properly, use the forcetorque callback with addforce and addtorque calls to move objects during the simulation.
Setmatrix is only useful to set body's location when created and when the body is "teleported".
So, you are teleporting a body around which ofcourse won't work, instead you should add forces to it and let newton simulate it during newtonupdate.
-

JernejL
-
- Posts: 1587
- Joined: Mon Dec 06, 2004 2:00 pm
- Location: Slovenia
-
by Rodion Karimov » Sat Jul 04, 2009 10:59 am
I'm teleporting only in the second case, that is working, and I'm letting Newton to simulate everything and only retrive from it velocity in the first case, that is not working. In both cases I use
- Code: Select all
procedure ForceAndTorqueCallback ( const Bounding : PNewtonBody ); cdecl;
begin
NewtonBodyGetMassMatrix ( Bounding, @ Mass,
@ Inertia [ 0 ], @ Inertia [ 1 ], @ Inertia [ 2 ] );
Force [ 0 ] := 0.0;
Force [ 1 ] := - 0.98 * Mass;
Force [ 2 ] := 0.0;
NewtonBodyAddForce ( Bounding, @ Force );
end;
Idea here is so that if the velocity is equal in Newton and that I retrive from it, and time is equal, so the path of the object and its new coordinates must be equal in Newton and in my engine. I'm doing so because I want to calculate physics 5 - 10 times in second but animate over 30.
-
Rodion Karimov
-
- Posts: 13
- Joined: Sat Jul 04, 2009 6:46 am
- Location: Russia
-
by JernejL » Sat Jul 04, 2009 12:25 pm
5-10 times a second is way too little for any real time visual physics simulations, what are you simulating, a game or something else?
-

JernejL
-
- Posts: 1587
- Joined: Mon Dec 06, 2004 2:00 pm
- Location: Slovenia
-
by Rodion Karimov » Sat Jul 04, 2009 12:42 pm
A game, 5 - 10 times in second I will only calculate forces, accelerations and velocities. And 30 times in second I will add velocities to coordinates.
-
Rodion Karimov
-
- Posts: 13
- Joined: Sat Jul 04, 2009 6:46 am
- Location: Russia
-
by JernejL » Sat Jul 04, 2009 2:58 pm
Is there any particular reason you are doing it in such a way? because no physics engine is intended to be used like this, physics engines are supposed to run at same or highier framerate that the graphics system, and you are doing everything totally wrong, you shouldnt calculate the velocities, physics engine does that, all you should do is add forces and torque to bodies, have you checked out any newton implementation demos?
-

JernejL
-
- Posts: 1587
- Joined: Mon Dec 06, 2004 2:00 pm
- Location: Slovenia
-
by Rodion Karimov » Sun Jul 05, 2009 2:49 am
I'm doing so because my engine uses Shadow Volumes, And Why It is wrong -
Time := 0;
Animate - Time + 5,
Animate - Time + 5,
Animate - Time + 5,
Than PhysicsThread.Resume and in it
NewtonUpdate ( NewtonWorld, Time );
So as far as I understand object in Newton must be in the same position as in my engine, and only then forces will be calculated and from them accelerations and velocities, and I retrive from it velocity. Why it is wrong I'm doing so because I do not need such a percsion of calculating forces, accelerations and speeds in my game? And for what purpose than there is those GetVelocity and GetAngularVelocity?
Edit - Somewhy objects in my engine are moving faster, than in Newton, when I only retrive matrix and take from it coordinates for my engine - everything is working fine. So if I multiply Velocity * Time by somewhere 0.265 - everything is also working fine.
If in physics world there is only 2 objects ofcorse Newton is calculating physics over 0.00002430447395 of second, but if there will be over one hundreed objects? I think that this number will be much increased.
Edit - May be Newton by itself is calculating forces, accelerations and velocities not on every physics update? And may be there is no need to separate animation and physics?
-
Rodion Karimov
-
- Posts: 13
- Joined: Sat Jul 04, 2009 6:46 am
- Location: Russia
-
by JernejL » Sun Jul 05, 2009 5:52 am
What do shadow volumes have to do with anything?
Newton is a physics engine, physics engines are used to simulate physics, you give it shapes and bodies, run simulation for 1 frame time with newtonupdate each game frame, input forces during forcetorque callback and get the resulting body locations during transform callback. what you are doing seems like a strange form of inverse kinematics and manual time interpolation.
Your objects are mismatched because newton has by default body dampening which may be what you arent calculating the same way, similar would happen with omega...
Can you just let newton simulate physics every frame like it's intended to be used? evetything should work fine then.
-

JernejL
-
- Posts: 1587
- Joined: Mon Dec 06, 2004 2:00 pm
- Location: Slovenia
-
by Rodion Karimov » Sun Jul 05, 2009 6:48 am
I mean Shadow Volumes because they load CPU very much, so I'm trying to reduce CPU load on physics.
Yes it seems so that there is no ability to use Newton in such way, it faster of all calculates coordinates by some other law. I will try to strict physics calculations only in some radius of player, to reduce the load on CPU. But anyway there is no need to simulate physics and animate every frame, 30 times in second - it will be quite enough.
-
Rodion Karimov
-
- Posts: 13
- Joined: Sat Jul 04, 2009 6:46 am
- Location: Russia
-
by Julio Jerez » Sun Jul 05, 2009 9:32 am
why are you trying to secund guess the library, you shuld let the library runs the way it wants to run.
most games simulate physics at 60 fps, and now a day many games are running at 120 time per secund.
I suggest you try 100 fps or higher to avoid othe problems that I can asure you are much harder to fix.
if it is slow then you start worry abu the physics.
when I simulate physics at 100 fps in my machine, I get the graphics runing 11000 fps, that is not a typo it is 11 tousand frames per secunds.
From what you say it sound you like you only need collision for your game, since you are managing the motion of all objects already.
If this is the case then you can use the collision part of Newton and then will give you the contacts you need.
that way you can probably add tousand of bodies and still run Newton at 100 to 200 fps which I am sure will be sufficient for what your want, since the most of the time in Netwon is consumed
by the solver, the collision is very cheap.
-
Julio Jerez
- Moderator

-
- Posts: 12426
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by Rodion Karimov » Sun Jul 05, 2009 10:46 am
Ok.
I will try to fully load graphics and Newton, and if it will be too slow, I will try to use collisions, so thank you Julio Jerez.
-
Rodion Karimov
-
- Posts: 13
- Joined: Sat Jul 04, 2009 6:46 am
- Location: Russia
-
Return to General Discussion
Who is online
Users browsing this forum: No registered users and 1 guest