#### Archived

This topic is now archived and is closed to further replies.

# Converting integration to quaternion

This topic is 5821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have the book "Physics for Game Developers" and have learned a goodly amount from it. Right now, I'm adding correct projectile physics to my game. the problem is, the sample the book provides (which I can understand quite well) works from 3 Euler angles. I would prefer to work from the Quaternion I'm already using to define the projectile's rotation. Can anyone give me an idea of how to cnvert an equation that uses the Euler angles to one that uses a Quat? alternatively does anyone have some sample code for doing particle movement (based on velocity, rotation and gravity) using a Quat for the rotation angle? Final question: The book's example completes the equation from the time = 0 every iteration. This works fine as long as all of the inputs are constant with time. It would run in to problems with wind that varied over the course of the calculations, however. Is there any way to do my integration, always from the last integration (rather than from time = 0) without having to handle the particle as a solid body? Thanks for the help and apologies if this sort of question has already been asked, but searching is down right now. :-( Thanks! Onnel [edited by - onnel on September 10, 2002 7:11:22 AM]

##### Share on other sites
To convert from Euler angles to quats simply replace one with the other. E.g. everywhere you see Euler angles repace them with quaternions and quaternion methods.

Mostly this is straightforward. One of the least obvious things is integration, i.e. updating the quaternion over time as the object spins.

The formula for this is

dq/dt = 1/2 w * q

''dq/dt'' is the rate of change of the quaternion, q it''s current value and ''w'' the angular velocity vector treated as a quaternion. w may be constant, such as for a wheel spinning at a fixed rate about an axis, or it may also be varying with time.

To use this and similar differetial equations use the fact that

Dq = dq/dt Dt.

I.e. the change in q, Dq, is just the time step, Dt, multiplied by the rate of change. Each frame calculate Dq and add it to the quaternion, renormalising if necessary, to update the object''s orientation.

##### Share on other sites
Alright, that definitely helps. For a more concreate example:

Simplified from the sample code in the book:

          //Alpha and Gamma are currently the rotation of the gun vertically and horizontally in degrees (around the x and y axis       //respectively).  Here we are determining the sin and cos of the angles     // and using them for the integration.  If instead of Alpha and Gamme (x rot and y rot)     // I had a quat with x,y,z,w, how would I change the code to use the quat?        // First calculate the direction cosines for the cannon orientation.        // In a real game you would not want to put this calculation in this        // function since it is a waste of CPU time to calculate these values        // at each time step as they never change during the sim.  I only put them here in        // this case so you can see all the calculation steps in a single function.        b = cos((90-Alpha) *3.14/180);  // projection of barrel onto x-z plane        Lx = b * cos(Gamma * 3.14/180);         // x-component of barrel length        Ly = cos(Alpha * 3.14/180);         // y-component of barrel length        Lz = b  * sin(Gamma * 3.14/180);        // z-component of barrel length        cosX = Lx;        cosY = Ly;        cosZ = Lz;        // Now we can calculate the position vector at this time        // Vm is initial velocity, time is the total time elpased since       // the beginning.        s.x = Vm * cosX * time;        s.y = (cos(Alpha*3.14/180)) + (Vm * cosY * time) - (0.5 * g * time * time);        s.z = Vm * cosZ * time;

I realise I'm being an idiot and need it spelled out for me, but I don't see how I'm supposed to simply replace the euler angles with the quat.

Thanks for the assistance!

[edited by - onnel on September 10, 2002 12:57:50 PM]

##### Share on other sites
Anyone able to lend a hand or give me an alternate formula for a projectile where I can use a quaternion for the initial launch angle? I''m sort of stuck on this right now. :-(

##### Share on other sites
Quaternions aren''t exactly the ideal way to represent a trajectory. And, I''m not exactly sure if this is what you want, but here goes. Say our initial barrel vector is (0,0,1). This is the Lx, Ly, Lz in the code snippet. Then, the Lx, Ly, Lz for the rotated barrel is simply the original vector rotated by the quaternion (I''m assuming that you know how to do this..) The key to understanding this is that Lx,Ly,Lz is simply a unit vector pointing in the direction of the barrel.

##### Share on other sites
DOH..of course! I'm an idiot! I didn't even think of just taking the initial vector and rotating it by the Quat! I'll get to trying it right away! Thanks..Sometimes the easiest things aren't obvious!

UPDATE: This worked like a charm...thanks for pointing out the obvious!!!

Onnel

[edited by - onnel on September 12, 2002 3:44:24 AM]

1. 1
2. 2
3. 3
JoeJ
12
4. 4
5. 5
Rutin
11

• 12
• 16
• 13
• 20
• 12
• ### Forum Statistics

• Total Topics
632178
• Total Posts
3004597

×