## 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

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.

Posts: 539
Joined: Fri Oct 27, 2006 2:54 pm

### Re: Calculate force and torque to interpolate between 2 mat4s

This would be very useful for IK, i'm curious of this solution myself as well.
Help improving the Newton Game Dynamics WIKI

JernejL

Posts: 1531
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

### Re: Calculate force and torque to interpolate between 2 mat4s

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

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.

Posts: 539
Joined: Fri Oct 27, 2006 2:54 pm

### Re: Calculate force and torque to interpolate between 2 mat4s

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

Posts: 11155
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Calculate force and torque to interpolate between 2 mat4s

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

JernejL

Posts: 1531
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

### Re: Calculate force and torque to interpolate between 2 mat4s

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
OrionX3D Projects & Demos:

Dave Gravel

Posts: 725
Joined: Sat Apr 01, 2006 9:31 pm

### Re: Calculate force and torque to interpolate between 2 mat4s

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.

Posts: 539
Joined: Fri Oct 27, 2006 2:54 pm

### Re: Calculate force and torque to interpolate between 2 mat4s

Julio Jerez
Moderator

Posts: 11155
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Calculate force and torque to interpolate between 2 mat4s

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?:
Last edited by Leadwerks on Tue Jan 13, 2009 3:31 pm, edited 1 time in total.

Posts: 539
Joined: Fri Oct 27, 2006 2:54 pm

### Re: Calculate force and torque to interpolate between 2 mat4s

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
OrionX3D Projects & Demos:

Dave Gravel

Posts: 725
Joined: Sat Apr 01, 2006 9:31 pm

### Re: Calculate force and torque to interpolate between 2 mat4s

Wouldn't a dot product between a vector and itself always be 1.0?

Posts: 539
Joined: Fri Oct 27, 2006 2:54 pm

### Re: Calculate force and torque to interpolate between 2 mat4s

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
OrionX3D Projects & Demos:

Dave Gravel

Posts: 725
Joined: Sat Apr 01, 2006 9:31 pm

### Re: Calculate force and torque to interpolate between 2 mat4s

A dot product using any normalized vector for both arguments will always return 1.0.

Posts: 539
Joined: Fri Oct 27, 2006 2:54 pm

### Re: Calculate force and torque to interpolate between 2 mat4s

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
OrionX3D Projects & Demos:

Dave Gravel

Posts: 725
Joined: Sat Apr 01, 2006 9:31 pm