Newton and FireMonkey

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Newton and FireMonkey

Postby refusion85 » Mon May 28, 2012 9:28 am

Hello!

I like to use Newton with FireMonkey,
but i have problems with RotationAngle:

Code: Select all

NewtonBodyGetMatrix(NewtonBox[1].NewtonBody, @NewtonBox[1].Matrix[0,0]);
NewtonGetEulerAngle(@NewtonBox[1].Matrix[0,0], @Vector);

Cube1.Position.X := NewtonBox[1].Matrix[3,0];  //X
Cube1.Position.Y := -NewtonBox[1].Matrix[3,1]; //-Y
Cube1.Position.Z := NewtonBox[1].Matrix[3,2];  //Z

Cube1.RotationAngle.X := Vector.X * 180 / Pi; //?
Cube1.RotationAngle.Y := Vector.Y * 180 / Pi; //?
Cube1.RotationAngle.Z := Vector.Z * 180 / Pi; //?

NewtonUpdate(NewtonWorld, 0.05);



Cube1 position works, but NewtonGetEulerAngle and RotationAngle are incompatible.

Can someone help me?
Attachments
NewtonTest.zip
Delphi XE2 FireMonkey project
(17.51 KiB) Downloaded 296 times
refusion85
 
Posts: 5
Joined: Mon May 28, 2012 9:13 am

Re: Newton and FireMonkey

Postby Julio Jerez » Mon May 28, 2012 9:41 am

what version of newton are you linking too?
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton and FireMonkey

Postby JoeJ » Mon May 28, 2012 11:14 am

Most probably Newton and your Engine use different order with euler angles.
You can use following code to extract the angles from a matrix in any order you want, but i never tested with somethig unusual like xyx or zyz...
Code: Select all
static v3f ToEulerAngles (matrix4f &m, int order = 0x012)  // use matrix from NewtonBodyGetMatrix() // order: 0x012 = xyz, 0x201 = zxy and so on...
   {
      
      int a0 = (order>>8)&3;
      int a1 = (order>>4)&3;
      int a2 = (order>>0)&3;

      v3f euler; // array of the 3 euler angles
      // Assuming the angles are in radians.
      if (m.m[a0][a2] > 0.9999)
      { // singularity at north pole
         euler[a0] = -atan2(m.m[a2][a1], m.m[a1][a1]);
         euler[a1] = -PI/2;
         euler[a2] = 0;
         return euler;
      }
      if (m.m[a0][a2] < -0.9999)
      { // singularity at south pole
         euler[a0] = -atan2(m.m[a2][a1], m.m[a1][a1]);
         euler[a1] = PI/2;
         euler[a2] = 0;
         return euler;
      }
      euler[a0] =   -atan2(-m.m[a1][a2], m.m[a2][a2]);
      euler[a1] =   -asin ( m.m[a0][a2]);
      euler[a2] =   -atan2(-m.m[a0][a1], m.m[a0][a0]);
      return euler;
}


can I use that function to replace the old euler conversion in newton 2.00?
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Newton and FireMonkey

Postby JoeJ » Mon May 28, 2012 2:39 pm

JoeJ wrote:can I use that function to replace the old euler conversion in newton 2.00?


I've not written that line, someone asking?
I found that function here: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToEuler/index.htm
I added just the order shuffling.
From my side it's ok for everyone to use it :)
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Newton and FireMonkey

Postby Julio Jerez » Mon May 28, 2012 3:12 pm

That look like a nice function to have in a Matrix and quaternion class, so I will add it to the dMatrix class.
In will remove NewtonGetEulerAngle all together from the engione, and use the dMatic to do the convertion.
function NewtonGetEulerAngle is a legacy function I put in the engine when we did not have the dMatrix class and engines like Game Studio and Irrrlich
which still use angles to represent rotation, were using the engine, but that function has not place in a physics engine.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton and FireMonkey

Postby Julio Jerez » Wed May 30, 2012 8:44 am

Ok I added that general matrix to euler funtion to teh dMath class
you can use that funtion instead of the Newton version
here is how is set up

Code: Select all
class dMatrix
{
   public:
   enum dEulerOrder
   {
      m_pitchYawRoll = (0 << 8) + (1 << 4) + (2 << 0),
      m_rollYawpitch = (2 << 8) + (1 << 4) + (0 << 0),
   };

   dMatrix ();
   dMatrix (const dFloat* const array);
   dMatrix (const dVector &front, const dVector &up, const dVector &right, const dVector &posit);
   dMatrix (const dQuaternion &rotation, const dVector &position);
   dMatrix (dFloat pitch, dFloat yaw, dFloat roll, const dVector& location);

   dVector GetEulerAngles(dEulerOrder order = m_pitchYawRoll) const;

...
}


I did not see in your test where you are using the conversion to angles, but it is veryu eassy to use.
also I see you are using Newton 2.00
you should be using 3.00, it is easier and few bugs.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton and FireMonkey

Postby refusion85 » Thu May 31, 2012 6:29 am

Thank you the replies. I tested with r2050, but FireMonkey uses different coordinate and rotation system, so i still have problems with rotation.
Hmmm. Maybe I should be using OpenGL 3 + Newton 3 insted of FireMonkey.
refusion85
 
Posts: 5
Joined: Mon May 28, 2012 9:13 am

Re: Newton and FireMonkey

Postby Julio Jerez » Thu May 31, 2012 7:53 am

that's a very eassey problem to fix.
can you tell my what is the order of rotation?
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton and FireMonkey

Postby refusion85 » Thu May 31, 2012 4:57 pm

My problem is rotation around Y axis.

Blitz3D/Xors3D/Firemonkey:
X: -180...180
Y: -180...180
Z: -180...180

Newton:
X: -180...180
Y: -90...90
Z: -180...180

If I turning a cube and Y > 90 or Y < -90 then the cube turns with 180 degrees.
Is Newton's 45 degrees in Blitz3D/Xors3D/Firemonkey = 45 or -135?
How can i calculate the Y rotation?
refusion85
 
Posts: 5
Joined: Mon May 28, 2012 9:13 am

Re: Newton and FireMonkey

Postby Julio Jerez » Thu May 31, 2012 6:49 pm

There are always tow different set of three Euler angles that yield the same rotation matrix
It does no matter what convection is used there will always be a difference.
If you do this :
Code: Select all
dVector euler (30.0f * 3.1416f/ 180.0f, 145.0f * 3.1416f/ 180.0f, 145.0f * 3.1416f/ 180.0f, 0.0f);
dMatrix m (dPitchMatrix(euler.m_x) * dYawMatrix(euler.m_y) * dRollMatrix(euler.m_z));
dVector euler1 (m.GetEulerAngles());
dMatrix m1 (dPitchMatrix(euler1.m_x) * dYawMatrix(euler1.m_y) * dRollMatrix(euler1.m_z));

the result is this

Code: Select all
euler   {m_x=0.52359998 m_y=2.5307333 m_z=2.5307333}
euler1 {m_x=-2.6179926 m_y=0.61085927 m_z=-0.61085927}

but if yo umak eteh substitution

Code: Select all
pitchangle = pitchangle - pi
yawangle = pi - yawangle 
rollchangle = rollchangle – pi

you will see that
euler = euler1

but not knowing that yaw was in some other quadrant it is impossible to determine the desired solution.
A trick that you can do is using a memory the save the last value of the angles, decompose the delta matrix not the absolute matrix, then by using trig you can actually have the derided values all the time.

Basically what you do is that you save the previous matrix.
Then you the new matrix is compose uf this
M1 = M * M0
M0 is the previus matrix,
Ml is some local matrix
M is the new matrix,

The you do this
M = M1 * inv(M0)

Now you calculate the euler angle of M
Tehi will be the small angle thet will be in the firs quadrant every time so you know that they is not ambiguity.


Now to recostun the eler angles

You do this


Yaw = Ya0 + deltYaw

And by using equivalence n

Cos (Ya0 + deltYaw) and Sin (Ya0 + deltYaw) and

Yaw = atan2 (Cos (Ya0 + deltYaw), Sin (Ya0 + deltYaw))

And similar for Roll and pitch

You can have the angles all in an unlimited space, not just form [-180 < angle < 180]

If it is confusing to you, I can write you a function to do that if you and add it to the dMatrix class. Are you using the dMatrix class?
It will be a function like this,
dVector GetEulerAngles(dVector& initalEulerAngles)
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton and FireMonkey

Postby refusion85 » Fri Jun 01, 2012 1:19 pm

No dMatrix class, i'm in Delphi and i use these samples:
http://www.saschawillems.de/?page_id=82
refusion85
 
Posts: 5
Joined: Mon May 28, 2012 9:13 am

Re: Newton and FireMonkey

Postby refusion85 » Fri Jun 01, 2012 4:15 pm

I still tried to solve it but i failed.
I must recognise that C++ is better than Delphi.
Greather examples, bigger community, better support...
I will learn C++ from tomorrow.
refusion85
 
Posts: 5
Joined: Mon May 28, 2012 9:13 am

Re: Newton and FireMonkey

Postby armenha » Wed Jul 04, 2012 3:03 pm

try this

cube1.ResetRotationAngle;
cube1.RotationAngle.z:=radtodeg( angls[2]);
cube1.RotationAngle.y:=radtodeg( angls[1]);
cube1.RotationAngle.x:=radtodeg( angls[0]);

I have no problem with this!
armenha
 
Posts: 2
Joined: Sat Jun 30, 2012 2:33 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests