Updating glm rotation to match newton rotation [Solved]

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Updating glm rotation to match newton rotation [Solved]

Postby rystills » Wed Jun 12, 2019 10:59 pm

I'm having trouble syncing objects' rotations in my engine with their newton body rotation. With the help of debug rendering I can see that the newton bodies' initial rotations are being set correctly, and that those bodies are interacting physically as expected, but when I try to retrieve their rotations and assign them to the glm matrix that I use for rendering, the numbers don't come out right.

Currently I'm using NewtonBodyGetRotation to retrieve each body's rotation matrix, followed by GetEulerAngles to get the rotation in the form of euler angles to pass into glm::quat. However, GetEulerAngles returns two dVectors rather than just one as I would expect, and I'm not sure how to combine them to get a single euler angles vector.

Alternatively, if there's a simpler way to keep my rotations in sync that I missed, please let me know.

EDIT: I got so caught up in using euler angles, that I did not try simply converting the rotation matrix returned by NewtonBodyGetRotation directly into a glm::quat. Syncing rotation this way works as expected, and is more performant too. I am still curious about the fact that GetEulerAngles returns two dVectors instead of 1, but regardless my question is solved. Here's the code I used:

Code: Select all
dMatrix rot;
NewtonBodyGetRotation(body, &rot[0][0]);
glm::quat q(rot[0].m_w, rot[0].m_x, rot[0].m_y, rot[0].m_z);
GO->rotation = glm::toMat4(q);
rystills
 
Posts: 17
Joined: Wed Jun 12, 2019 2:01 pm

Re: Updating glm rotation to match newton rotation [Solved]

Postby Sweenie » Thu Jun 13, 2019 2:54 am

NewtonBodyGetRotation actually returns a quaternion with the order x,y,z,w.

Code: Select all
dQuaternion rot;
NewtonBodyGetRotation(body, &rot.m_x);


NewtonBodyGetMatrix returns the full transform matrix though.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Updating glm rotation to match newton rotation [Solved]

Postby rystills » Thu Jun 13, 2019 4:13 am

Ah, I had assumed the order was w,x,y,z. That likely explains why some of my earlier attempts to directly copy the memory were resulting in unexpected values. Thanks for the heads up; I'm happy to say rotations are now properly synced between my engine and Newton, but I'll be sure to keep this information in mind going forward.
rystills
 
Posts: 17
Joined: Wed Jun 12, 2019 2:01 pm

Re: Updating glm rotation to match newton rotation [Solved]

Postby Julio Jerez » Thu Jun 13, 2019 10:22 am

rystills wrote:I am still curious about the fact that GetEulerAngles returns two dVectors instead of

because many inverse functions have more than one solution.
for example evert positive number has number has two square roots,
every rotation matrix also has two sequence or euler angles factorization.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Updating glm rotation to match newton rotation [Solved]

Postby rystills » Thu Jun 13, 2019 12:50 pm

No kidding, math libraries like glm must have been making some assumptions then by returning just one such EA factorization. I'll have to do some more reading on this topic in the near future. Thanks for clearing that up!
rystills
 
Posts: 17
Joined: Wed Jun 12, 2019 2:01 pm

Re: Updating glm rotation to match newton rotation [Solved]

Postby Julio Jerez » Fri Jun 14, 2019 9:38 am

is very eassy to see, when you factor a matrix into euler, the middle angle is calculate by the funtion

yaw = asin (m[2][2])

but since sin (x) = sin (180 - x) them there are two possible yaw angles that can solve the factorization.
from since the other two angles are calculated using the angle = atan(x) which only have one solution
them possible permutations are 1 angle for pitch, 2 angles for yaw, and one angle for roll
equal 2 possible permutations of pitch yaw role
must people simply ignore the yaw = 180 - asin (m[2][2]) and use only yaw = asin (m[2][2])
is not wrong is just incomplete.

this is similart to roots of algebraic equations for example

4 = x ^ 2

if you get x = 2 you will be right, but you can also get x = -2 and that also be right
Newton provides 2, -2 and let the use chose
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Updating glm rotation to match newton rotation [Solved]

Postby JernejL » Fri Jun 14, 2019 12:09 pm

I have cleared up info on wiki, to be clear on order of quaternion values.

http://www.newtondynamics.com/wiki/inde ... etRotation
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 12 guests

cron