Calculate force and torque to interpolate between 2 mat4s

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Calculate force and torque to interpolate between 2 mat4s

Postby Leadwerks » Sat Jan 10, 2009 10:23 pm

It would be very helpful to have a function that calculated the force and torque needed to go from one matrix to another (assuming zero resistance). Something like this:

NewtonCalculateForceAndTorque( NewtonBody body, Mat4 currentmat, Mat4 desiredmat, Vec3* force, Vec3* torque )

The force is obviously easy to calculate, but I have trouble trying to rotate an object with torque to bring it to a certain orientation.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Calculate force and torque to interpolate between 2 mat4s

Postby JernejL » Sun Jan 11, 2009 12:12 pm

This would be very useful for IK, i'm curious of this solution myself as well.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Calculate force and torque to interpolate between 2 mat4s

Postby agi_shi » Sun Jan 11, 2009 2:39 pm

Why not calculate this using quaternions? 4x4 matrices aren't meant for this task.
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Leadwerks » Sun Jan 11, 2009 3:02 pm

Why not? You can get a quaternion from a matrix.

Newton doesn't expose quaternions anywhere else, and I don't know if my implementation is exactly the same. But I do know that a 4x4 matrix has no room for creative interpretation.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Julio Jerez » Sun Jan 11, 2009 3:49 pm

Leadwerk I posted that code a few times, but it is also include in the SDK, maybe different name
Plus Newton do exposes the rotations in form of quaternion precisely for people who run the physics and the graphics at different rates. The function name is

NewtonBodyGetRotation(const NewtonBody* body, dFloat* rotation);

it return the rotation in for of a quaternion as:
q0 = cos(a/2)
q1 = sin(a/2) * x
q2 = sin(a/2) * y
q3 = sin(a/2) * z

plus if you look at the dMath library of the SDK
there is a function

dVector dQuaternion::CalcAverageOmega (const dQuaternion &QB, dFloat dt) const

that function will give you the angular velocity the will rotation quaternion q0 to q1
Plus if you have matrices, you can also use the dMath class to convert the matrices to quaternions

all in all all you need is in the SDK already.
all those function has been very tested by many a grup of people that made a ship simulator with Newton.
I also tested and use then myself in teh engine.

do you want this for implementing smooth vitalisation?

if so all you need to do is somethon l;ik this.

in you graphics body you keep position


myVisualObjcet
{
....
...
Vector posit, prevPosit
Quaternion rot, pevRot
}


on initilization you copy teh current rotaion and position to be the same value

in the Transformcallback yo do this

TransformCallback(body, maytrix)
{
prevPos = pos;
prevRot = rot;

pos = matrix.posit
NewtonBodyGetRotation( body, &rot.q0)
}

in you graphic loop you do this
normalize the time fraction and calcule the new position by lerp and teh rotation by slerp




interPos;
interPos = Lerp (prevPos, Pos, tiem fration)
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Calculate force and torque to interpolate between 2 mat4s

Postby JernejL » Mon Jan 12, 2009 4:37 am

Julio Jerez wrote:NewtonBodyGetRotation(const NewtonBody* body, dFloat* rotation);


Seems like newton 2.0 feature, i did not know this existed until now, i dont think i seen it mentioned it in changelogs, will check this out.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Dave Gravel » Mon Jan 12, 2009 11:13 am

It is pretty old, I have it in my old beta import.
You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://orionx3d.sytes.net
https://www.facebook.com/dave.gravel1
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 800
Joined: Sat Apr 01, 2006 9:31 pm
Location: Quebec in Canada.

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Leadwerks » Tue Jan 13, 2009 1:33 am

I am already using my own quaternion code for smooth interpolation. That is not a problem.

The problem I have is when I have an object, like something I pick up, and I know I want it to be a certain rotation, but I don't know how to apply torque to make it reach that rotation. Like in Half-Life 2, when you pick up an object, it maintains its rotation relative to you, as your character turns and moves.

Let's say the current rotation is A and I want the rotation to be B. I don't know how to calculate a torque to apply to rotate the object in the right direction, even though I can calculate the euler angles, quaternion, and 4x4 matrix for both A and B. I think CalcAverageOmega is what I am looking for.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Julio Jerez » Tue Jan 13, 2009 2:28 am

CalcAverageOmega is your function.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Leadwerks » Tue Jan 13, 2009 3:21 pm

Okay, here is the code:
Code: Select all
dVector dQuaternion::CalcAverageOmega (const dQuaternion &QB, dFloat dt) const
{
   dFloat dirMag;
   dFloat dirMag2;
   dFloat omegaMag;
   dFloat dirMagInv;
   
   dQuaternion dq (Inverse() * QB);
   dVector omegaDir (dq.m_q1, dq.m_q2, dq.m_q3);

   dirMag2 = omegaDir % omegaDir;
   if (dirMag2   < dFloat(dFloat (1.0e-5f) * dFloat (1.0e-5f))) {
      return dVector (dFloat(0.0f), dFloat(0.0f), dFloat(0.0f), dFloat(0.0f));
   }

   dirMagInv = dFloat (1.0f) / dSqrt (dirMag2);
   dirMag = dirMag2 * dirMagInv;

   omegaMag = dFloat(2.0f) * dAtan2 (dirMag, dq.m_q0) / dt;

   return omegaDir.Scale (dirMagInv * omegaMag);
}


What does the % sign mean on this line?:
dirMag2 = omegaDir % omegaDir;
Last edited by Leadwerks on Tue Jan 13, 2009 3:31 pm, edited 1 time in total.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Dave Gravel » Tue Jan 13, 2009 3:30 pm

the % is a dot product returning a dFloat.
the Inverse is surely a way to reduce the rotation effect by self inverted value.
You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://orionx3d.sytes.net
https://www.facebook.com/dave.gravel1
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 800
Joined: Sat Apr 01, 2006 9:31 pm
Location: Quebec in Canada.

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Leadwerks » Tue Jan 13, 2009 3:34 pm

Wouldn't a dot product between a vector and itself always be 1.0?
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Dave Gravel » Tue Jan 13, 2009 3:41 pm

Correction:
I don't think, it is more like -1.0f, 1.0f and 0.0f.
representation: V1[0]*V2[0]+V1[1]*V2[1]+V1[2]*V2[2];
Last edited by Dave Gravel on Tue Jan 13, 2009 3:54 pm, edited 2 times in total.
You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://orionx3d.sytes.net
https://www.facebook.com/dave.gravel1
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 800
Joined: Sat Apr 01, 2006 9:31 pm
Location: Quebec in Canada.

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Leadwerks » Tue Jan 13, 2009 3:48 pm

A dot product using any normalized vector for both arguments will always return 1.0.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Dave Gravel » Tue Jan 13, 2009 3:50 pm

What is the meaning of the value returned by the dot product?
The value is the cosine of the angle between the two input vectors, multiplied by the lengths of those vectors. So, you can easily calculate the cosine of the angle by either, making sure that your two vectors are both of length 1, or dividing the dot product by the lengths.

Values range from 1 to -1. If the two input vectors are pointing in the same direction, then the return value will be 1. If the two input vectors are pointing in opposite directions, then the return value will be -1. If the two input vectors are at right angles, then the return value will be 0. So, in effect, it is telling you how similar the two vectors are.

Exemple you get speed from velocity dot matrix.front;
The speed can have 0 and a negative value or positive value.
You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://orionx3d.sytes.net
https://www.facebook.com/dave.gravel1
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 800
Joined: Sat Apr 01, 2006 9:31 pm
Location: Quebec in Canada.

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 9 guests