Networking physics simulation

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Networking physics simulation

Postby JoeJ » Mon Mar 12, 2018 9:18 am

JernejL wrote:if you can explain a bit further


Code: Select all

float duration = timestep * 10; // duration until we hit the target

vec targetLinvel = (targetPosition - currentPosition) / duration;

vec curLinvel = BodyGetVelocity (body);

// may be still too aggressive - eventually try to smooth it:  targetLinvel = targetLinvel * 0.1 + curLinvel  * 0.9;

vec force = (targetLinvel - curLinvel) * mass; // this is important: if targetV already equals curV, nothing will happen

// add and adjust this if it seems to work til here: force.SetMaxLength(someConstant);



Angular part would be the same, but i'd focus on linear first.
One problem is that this ignores friction with ground. It might be a interesting option to fake contact velocity in contact callback instead forces in force callback, or use both approaches depending on contact situation.

Shaderman wrote:What about the good old client/server model?


Would this really work for realtime action games? I don't believe, but i totally don't know.
Liked this article: https://www.gamedev.net/articles/programming/networking-and-multiplayer/the-poor-mans-netcode-r4851/
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Networking physics simulation

Postby JernejL » Tue Mar 13, 2018 6:31 pm

JoeJ: Thank you!!

The result is amazing, i didn't even realise that "vec targetLinvel = (targetPosition - currentPosition) / duration;" is enough for a basic calculation, and while rotations are still quirky (only did translation for now), the result is really good (i hope to make a video tomorrow) - I guess the fact that my cars are shapes on ground, which has friction between materials set to zero also helps (no friction makes it easier, i calculate my own wheel rolling resistance and other forces to have cars move nicely)

So, now i just calculate force needed based on your suggestion and after addforce i override the final velocity via setvelocity to what was on other client who sent the position data (and that mostly nicely fills / extrapolates further movements until a next, proper update comes).

Result is pretty good, collision responses are much better, but ofcourse - provided that clients send & receive enough update frames - either way, this looks like a big step in the correct direction.

Maybe together we can write something that will calculate a given torque to reach a specific matrix orientation too? It would serve as a good example for others, i'm not sure how to approach this, afaik torque is in global space aswell, and i'm not very familiar with calculations on non-euler - matrix rotations, i could use some help here too.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1451
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby JoeJ » Wed Mar 14, 2018 3:42 am

Code: Select all
float duration = timestep * 10; // duration until we hit the target

//// targetLinvel = (targetPosition - currentPosition) / duration;
// Doing this for orientation we first calculate the rotation from current to target orientation:

quat rotation;
{
      quat qA = currentMatrix.ToQuaternion();
      quat qB = targetMatrix.ToQuaternion();
      quat q;
      
      if (qA.Dot(qB) < 0) // make sure we choose the shortest arc (usually people use inverse() to make this more intuitive)
      {
         q[0] = qA[0]; q[1] = qA[1]; q[2] = qA[2];
         q[3] = -qA[3]; // i store W in the 4th element (xyzw order); Julio in the first (wxyz) !
      }
      else
      {
         q[0] = -qA[0]; q[1] = -qA[1]; q[2] = -qA[2];
         q[3] = qA[3];
      }
         
      rotation = qB * q; // with Julios mathlib you'd need to reverse this: rotation = q * qB !
}

// convert the resulting rotation to axis and angle:
vec axis; float angle;
rotation.ToAxisAndAngle (&axis, &angle, rotation);

// convert axis and angle to rotation vector, which encodes angle in the vector length:

vec targetAngvel = axis * angle;



vec curAngvel = BodyGetOmega (body);

// may be still too aggressive - eventually try to smooth it:  targetAngvel = targetAngvel * 0.1 + curAngvel  * 0.9;

//// vec force = (targetLinvel - curLinvel) * mass;
// instead of multiplying by uniform mass, we need to do a nonuniform scale
// in local space where inertia is defined:

vec torque = (targetAngvel - curAngvel);// / duration;

torque = currentMatrix.Unrotate (torque);
torque[0] *= Ixx;
torque[1] *= Iyy;
torque[2] *= Izz;
torque = currentMatrix.Rotate (torque);

// add and adjust this if it seems to work til here: torque.SetMaxLength(someConstant);


...hope this works and there is no bug.
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Networking physics simulation

Postby JoeJ » Wed Mar 14, 2018 4:01 am

JernejL wrote:afaik torque is in global space as well, and i'm not very familiar with calculations on non-euler - matrix rotations, i could use some help here too.


Yes. To think about 3D rotations, you should imagine them as a single rotation around a certain axis.
Euler angles means 3 rotations about 3 orthogonal axis done in specified order, which is harder to imagine and unnecessarily complex.

So, thinking of Axis and Angle, we get a nice link between various rotation formats as a bonus:

It can be easily converted to and from quaternions (by multiplying directional and angular components with sin and cos of half angle), so you can think of quaterenions as just an 'optimized' axis and angle encoding.

It can be easily converted to and from Rotation Vector format used for angular velocity and torque, by multiplying axis with angle.
Advantage of Rotation Vectors is that you can add and subtract them, without the order dependency we see when multiplying matrices or quaternions.

Conclusion is that quat / axis and angle / rotation vectors are fine to think about rotations,
while matrix is fine to think about orientation.

Distinguishing between 'rotation' and 'orientation' helps a lot when thinking about this stuff, similar to distinguishing between displacement (vector) and position (point) would do (it's just not necessary there because position is easy.)

Above code is probably the best example to deepen understanding of all of this, so take time to understand it completely ;)
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Networking physics simulation

Postby JoeJ » Wed Mar 14, 2018 4:06 am

JernejL wrote:So, now i just calculate force needed based on your suggestion and after addforce i override the final velocity via setvelocity to what was on other client who sent the position data (and that mostly nicely fills / extrapolates further movements until a next, proper update comes).


I do not understand the need to set velocity directly. (although i'm surprised it all works well so quickly)
You should try to get rid of modifying velocity, only Newton should set it as a result of it's solving process.
Example: Box at wall, user sets velocity towards the wall, Newton can't resolve penetration.
If the user only sets force towards the wall, Newton can avoid penetration by calculating contact forces.
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Networking physics simulation

Postby JernejL » Wed Mar 14, 2018 7:37 am

Velocity is set only to keep bodies moving on as they would normally:

I might use this code to calculate forces to move a body 0.5 units to the left in order to get it to proper position, but the body was in reality moving right, and the counter-intiutive strange move was done as a correction due to lag (because the vehicle was actually slowing down and previous extrapolation overshot the correct movement).

In that case, i have to use forces to move it left, but want to have it continue behaving as if it was moving right (as it really was on other client) - that's why i override the velocity as a final step.

If i would not do this, the body would keep moving left until the next sync packet, and look very jittery.

I hope to make a few videos to explain this today :)
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1451
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby JernejL » Wed Mar 14, 2018 5:06 pm

New result, notice how better collisions are, i didn't use your latest code (to correctly handle rotations), so this still uses sematrix to set correct heading - i need to translate it to freepascal first:

Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1451
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby JoeJ » Wed Mar 14, 2018 6:05 pm

Looks good - I can't see anything jaggy and don't know which sides are server / client :)
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Networking physics simulation

Postby JernejL » Sat Mar 17, 2018 8:32 am

This has yielded pretty good results, i will update progress with my findings when i do more testing and write a full matrix -> force/torque code
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1451
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby JernejL » Mon Oct 08, 2018 5:12 am

JoeJ wrote:
Code: Select all
float duration = timestep * 10; // duration until we hit the target

//// targetLinvel = (targetPosition - currentPosition) / duration;
// Doing this for orientation we first calculate the rotation from current to target orientation:

quat rotation;
{
      quat qA = currentMatrix.ToQuaternion();
      quat qB = targetMatrix.ToQuaternion();
      quat q;
      
      if (qA.Dot(qB) < 0) // make sure we choose the shortest arc (usually people use inverse() to make this more intuitive)
      {
         q[0] = qA[0]; q[1] = qA[1]; q[2] = qA[2];
         q[3] = -qA[3]; // i store W in the 4th element (xyzw order); Julio in the first (wxyz) !
      }
      else
      {
         q[0] = -qA[0]; q[1] = -qA[1]; q[2] = -qA[2];
         q[3] = qA[3];
      }
         
      rotation = qB * q; // with Julios mathlib you'd need to reverse this: rotation = q * qB !
}

// convert the resulting rotation to axis and angle:
vec axis; float angle;
rotation.ToAxisAndAngle (&axis, &angle, rotation);

// convert axis and angle to rotation vector, which encodes angle in the vector length:

vec targetAngvel = axis * angle;



vec curAngvel = BodyGetOmega (body);

// may be still too aggressive - eventually try to smooth it:  targetAngvel = targetAngvel * 0.1 + curAngvel  * 0.9;

//// vec force = (targetLinvel - curLinvel) * mass;
// instead of multiplying by uniform mass, we need to do a nonuniform scale
// in local space where inertia is defined:

vec torque = (targetAngvel - curAngvel);// / duration;

torque = currentMatrix.Unrotate (torque);
torque[0] *= Ixx;
torque[1] *= Iyy;
torque[2] *= Izz;
torque = currentMatrix.Rotate (torque);

// add and adjust this if it seems to work til here: torque.SetMaxLength(someConstant);


...hope this works and there is no bug.


I've finally went to implement this, and i'm wondering what should Ixx, Iyy and Izz be?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1451
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby JoeJ » Mon Oct 08, 2018 3:48 pm

JernejL wrote:I've finally went to implement this, and i'm wondering what should Ixx, Iyy and Izz be?


The inertia values you get from the the same function that returns mass. (NewtonGetMassMatrix ?)
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Networking physics simulation

Postby JernejL » Mon Oct 08, 2018 6:22 pm

Ok, thanks!! i will make a few tests tomorrow with this and see how it behaves - if i did it correctly :)
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1451
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby JoeJ » Tue Oct 09, 2018 3:07 am

JernejL wrote:if i did it correctly :)


Might be hard to know. I suggest for a proper test you use two bodies without gravity, set constant angular velocity to left body and replicate the same behavior with calculated torque on the right body.
Or something like that to proof the math.
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Networking physics simulation

Postby JernejL » Tue Oct 30, 2018 5:07 am

JoeJ wrote:
JernejL wrote:if i did it correctly :)


Might be hard to know. I suggest for a proper test you use two bodies without gravity, set constant angular velocity to left body and replicate the same behavior with calculated torque on the right body.
Or something like that to proof the math.


Thank you for your help here so far :) It took a few weeks to find time, but i'm happy i can say your math is sound, and the idea works, but the car takes more time to rotate, and rotates slowly, probably due to floor friction and incomplete part of code here:

Code: Select all
// instead of multiplying by uniform mass, we need to do a nonuniform scale
// in local space where inertia is defined:

vec torque = (targetAngvel - curAngvel);// / duration;


I assume i need to multiply the target torque by something in order to take duration and timestep into account - so far i've multiplied the value by 100, and i can get a car to slowly move to a point, i will need something that will do it much faster, at least 90% or target rotation in 1 frame, what should i go and change?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1451
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby JoeJ » Sat Nov 24, 2018 4:08 pm

Oh, i have missed this final post. Did you get it working meanwhile?

Can't remember anything or why i have commented out the division at the moment.
You could post your own code so i can verify...

I can confirm however controlling with torque works as good as with force. I do not think ground friction is the problem...
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

PreviousNext

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests