Calculate force and torque to interpolate between 2 mat4s

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Leadwerks » Tue Jan 13, 2009 4:04 pm

That's exactly my point. The code says this:
dirMag2 = omegaDir % omegaDir;

omegaDir % omegaDir will always be 1.0, if the magnitude of omegaDir is 1.0.

What is the dt argument for? Is it to modulate the return value, so 1.0 should be the default dt value, 0.5 would return half the omega, etc?
User avatar
Leadwerks
 
Posts: 539
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 4:11 pm

Yes sorry I don't have read the code posted before, but I think it can deal between 0 or 1 too.

Edited:
Sorry my english between word is wrong, i think it can deal with 0 or 1.
On this way it is like a abs int value from the dir.
Last edited by Dave Gravel on Tue Jan 13, 2009 4:35 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://www.facebook.com/dave.gravel1
http://orionx3d.googlepages.com/
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 725
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 4:22 pm

The quaternions Newton returns from a body also are completely different than the quaternions my code calculates, so there is no way to tell it the rotation I want.



teh quuatenion is represente as and array float

float q[4];
q[0] = cos(a/2)
q[1] = x * sin(a/2)
q[2] = y * sin(a/2)
q[3] = z * sin(a/2)

make sure you map your to tha format
you problby have then as

q[0] = x * sin(a/2)
q[1] = y * sin(a/2)
q[2] = z * sin(a/2)
q[3] = cos(a/2)

just do a rearrenge an dit should work.

teh reason whi is check

teh dot oproduct is because of the two matrices are very close then sin(a/2) is almost zero, and the rotaion axis can not be extracted,
but this is a special case when the angular velocity is zero.
User avatar
Leadwerks
 
Posts: 539
Joined: Fri Oct 27, 2006 2:54 pm

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Leadwerks » Tue Jan 13, 2009 4:52 pm

I got m quaternions to match Newton's, but my body still spins completely out of control. I am pretty sure the error lies in my calculation method, because even if I use two quaternions from Newton bodies, it spins uncontrollably.
User avatar
Leadwerks
 
Posts: 539
Joined: Fri Oct 27, 2006 2:54 pm

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Leadwerks » Tue Jan 13, 2009 5:13 pm

Oka, thanks, I have it working now. I get very frustrated by 3D math because it seems even when you understand the theory, it still involves a lot of trial and error.
User avatar
Leadwerks
 
Posts: 539
Joined: Fri Oct 27, 2006 2:54 pm

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Leadwerks » Tue Jan 13, 2009 5:47 pm

Here is the problem I have now. Once the target object (the visible mesh) crosses a certain angle, the following body (shown in green) takes the longer way to match the mesh's orientation:
http://www.leadwerks.com/post/omega.wmv

Some kind of check is needed to see what the shortest route to the desired rotation is, since there are two possible solutions.
User avatar
Leadwerks
 
Posts: 539
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 6:49 pm

I think there is a bug in the function, try changing this line
Code: Select all
 
dirMag = dirMag2 * dirMagInv;
 omegaMag = dFloat(2.0f) * dAtan2 (dirMag, dq.m_q0) / dt;

to this
Code: Select all
 omegaMag = dFloat(2.0f) * acos (dq.m_q0) / dt;
[/code]

make sure that dq.m_q0 is in the interval
[-1.0, 1.0] sometime because of arithmetic presition it qould be outside that range by a tiny value and acos generate a nans.
Julio Jerez
Moderator
Moderator
 
Posts: 11155
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 7:19 pm

Here is my code now, with the same result. As soon as the mesh goes past 180 degrees, the routine uses the longer solution:
Code: Select all
   Method CalcAverageOmega:TVec3( QB:TQuat, dt:Double=1.0 )
      
      Const REALLYSMALLNUMBER:Double=0.00001
      Local dirMag:Double
      Local dirMag2:Double
      Local omegaMag:Double
      Local dirMagInv:Double
      Local dq:TQuat
      Local omegaDir:TVec4
      Local dq2:TQuat
      
      dq=Inverse().Times(QB)      
      omegaDir=vec4(dq.x,dq.y,dq.z,dq.w)
      dirMag2=omegaDir.dot(omegaDir);
      If dirMag2<REALLYSMALLNUMBER*REALLYSMALLNUMBER Return vec3(0)
      dirMagInv=1.0/Sqr(dirMag2)
      omegaMag=2.0*ACos(dq.w)/dt
      Return (omegaDir.xyz()).Scale(dirMagInv*omegaMag)
   EndMethod
User avatar
Leadwerks
 
Posts: 539
Joined: Fri Oct 27, 2006 2:54 pm

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Leadwerks » Tue Jan 13, 2009 7:43 pm

It seems to work when I use this instead:
Code: Select all
omegaMag=2.0*Asin(dq.w)/dt


I tried some different random rotations on three axes, and it seems to work correctly, always choosing the closest path of rotation. I also tried single-axis rotations. It seems to work!!! :mrgreen:

Thanks!
User avatar
Leadwerks
 
Posts: 539
Joined: Fri Oct 27, 2006 2:54 pm

Re: Calculate force and torque to interpolate between 2 mat4s

Postby Leadwerks » Tue Jan 13, 2009 8:05 pm

Oh man, you won't believe this. Using the ASin() method above, the omega slows down as you get closer to the opposite angle. If I rotate the mesh 180 degrees opposite from the body, I can actually get the body to stop completely:
http://www.leadwerks.com/post/omega2.wmv

:cry:

I seem to have better luck with this:
Code: Select all
omegaMag=2.0*Sgn(Asin(dq.w))/dt
User avatar
Leadwerks
 
Posts: 539
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 8:30 pm

Just a personal question, the body that you use is without gravity or connected by a joint ?
You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://www.facebook.com/dave.gravel1
http://orionx3d.googlepages.com/
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 725
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 8:39 pm

No gravity.
User avatar
Leadwerks
 
Posts: 539
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 9:07 pm

I seem to have better luck with this:
Code: Select all
omegaMag=2.0*Sgn(Asin(dq.w))/dt


it works but you are doing somethong wrong, thsi is hwo it sodul work usieng thsio exprestion
omegaMag = dFloat(2.0f) * acos (dq.m_q0) / dt;

you can try a simpel stest

say the two matrix are equal, eh rotaion for Q1 and Q0, teh proble is find teh rottaion teh will rotate Q0 intop Q1

Q1 = Rr * Q0
Qr = Q1 * inv (Q0)

if the tw matrices are equal, teh Qr is and identity matrix
in quation from and identity matrix is [1, 0, 0, 0]

teh firntion in et hSDK stact teh pin an drotaion angle arint teh pin

thw is form qr

qr[0] = cos(w * dt / 2) = 1

w * dt / 2 = acos(1) = 0
w = 0 * 2 /dt

do the anguler mangniotu of the angula velocity is zero, and any pin vector will do it because the velocity is zero.
you should revisit you funtion because something is wrong.
Julio Jerez
Moderator
Moderator
 
Posts: 11155
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 10:11 pm

Here is my function:
Code: Select all
   Method CalcAverageOmega:TVec3( QB:TQuat, dt:Double=1.0 )
      
      Const REALLYSMALLNUMBER:Double=0.00001
      Local dirMag:Double
      Local dirMag2:Double
      Local omegaMag:Double
      Local dirMagInv:Double
      Local dq:TQuat
      Local omegaDir:TVec4
      Local dq2:TQuat
      Local scale:Float
      
      dq=Inverse().Times(QB)      
      omegaDir=vec4(dq.x,dq.y,dq.z,dq.w)
      dirMag2=omegaDir.dot(omegaDir);
      If dirMag2<REALLYSMALLNUMBER*REALLYSMALLNUMBER Return vec3(0)
      dirMagInv=1.0/Sqr(dirMag2)
      omegaMag=2.0*Sgn(ASin(dq.w))/dt
      omegaDir.x:*-1.0
      omegaDir.y:*-1.0
      omegaDir.z:*-1.0
      omegaDir.x=Degrees(omegaDir.x)
      omegaDir.y=Degrees(omegaDir.y)
      omegaDir.z=Degrees(omegaDir.z)
      scale=Degrees(dirMagInv*omegaMag)
      Return (omegaDir.xyz()).Scale(scale)
   EndMethod


In my quaternions, "m_q0" is "w".

My function appears to work correctly under all the conditions I tested with.

You might want to reconsider my original request. I am one of your most advanced users, and even I can't figure out how to rotate an object the way I want with physics.
User avatar
Leadwerks
 
Posts: 539
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 10:31 pm

what I say is that, it is best to work with reality. right now you are doing this:

w = q0 = cos(a/2)

you are doing
w = asin (cos(a/2)) intead of w = acos(a/2)

that will always be a very small values since sin (a) can only go from 0 to 1.0 (beteenn 50 dgress.)
in essence you are getting a damped velocity that for some reason feel good to you but is is wrong.
you can see it the video by the lag of the body, as you change the orientation.
if you want a damped velocity there are other way to do that,
fo example you can say I want the matrix to catch up in n number of frames, and you will know tah is right.

Remember a while back when you ask for matrix interpolation member by menber, I told you that it is a mistake,
but you did it anyway because you did not see the immediate effects, until later on you start to see unwanted scale effect in you visual objects and it cost you lot of unhappy users



Like I say If is workl it work, but i suggest revisinth teh math,

you need to write the equation in a piece of paper, and see if every turn is used like it is suppurt to be used.
is is what I do each time I do this thongs. Adm it is why I do no even remeber later hwo I didd it, I just write the algebra and go to where it leads me.
Julio Jerez
Moderator
Moderator
 
Posts: 11155
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

PreviousNext

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest