# Check my math? (Quaternions)

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

## Recommended Posts

I currently use the following code to convert from a force to a velocity and angular velocity:
        public virtual void ApplyForce(Force force)        {            velocity += force.Vector * InverseMass;            Vector3 crossVector = Vector3.Cross(force.Vector, force.Offset);            crossVector = new Vector3(crossVector.X * InverseInertia.X, crossVector.Y * InverseInertia.Y, crossVector.Z * InverseInertia.Z);            angularVelocity += crossVector;        }

It is, however, (unless I'm missing something big) Eular angles. Eular angles are undesirable due to gimble lock (such as could be expected to happen in a free-roaming spaceflight game (Elite, Privateer or Freespace, for example). If you know Eular angles, you know what I'm talking about.

I could use Matrices. The math would be involved, but likely doable. I could then do a spherical linear interpolation between Matrix.Identity and angular velocity, with an interpolation of Timestep. This would give, effectively, "Rotation = Rotation + AngularVelocity * Timestep" (ignoring that matrices are rotated through multiplying; not actual code, just rough process example).

However, Matrices (the 3x3 or 4x4 type used as a "3D computer gaming standard" have one great weakness - They cannot represent a rotation greater than 180 degrees. That is because they are technically not rotations; the 3x3 part is three normalized vectors pointing in the local (using left-handed coordinates) right, up and forward directions. They intrinsically wrap, in other words.

This brings us to Quaternions. Quaternions are a normalized vector axis and a rotation around that axis, represented in radians. As such, they can represent rotations greater than "360 degrees".

So, the incoming force (a vector representing the direction and magnitude of force) and offset (a vector distance from the objects' local center) need to be converted to a quaternion rotation to avoid Eular gimble lock.

My thought is: Take the vector cross of the force and offset. Cross this third vector with the offset and that should result in a vector perpendicular to it and the offset. Normalize it and make it the quaternions' axis and give it a rotation of 0. Then, "add" it to the quaternion rotation. Or;
        public virtual void ApplyForce(Force force)        {            velocity += force.Vector * InverseMass;            Vector3 crossVector = Vector3.Cross(force.Vector, force.Offset);            crossVector = new Vector3(crossVector.X * InverseInertia.X, crossVector.Y * InverseInertia.Y, crossVector.Z * InverseInertia.Z);            crossVector = Vector3.Cross(Vector3.Normalize(crossVector), Vector3.Normalize(force.Offset));            Quaternion angularVelocity = new Quaternion(Vector3.Normalize(crossVector), 0);        }

And: "rotation = rotation * Quaternion.Slerp(Quaternion.Identity, angularVelocity, timestep);"

Would this work? If not, can it work and what needs to be changed?

##### Share on other sites
1) Gimbal lock doesn't come from Euler angles. You can avoid gimbal lock while using Euler angles and you can have gimbal lock if you use quaternions. What matters is how you combine your rotations and not how your rotations are represented. Euler angles appear to be more prone to gimbal lock because the obvious ways to combine rotations when using Euler angles is not a particularly good one.

2) The first code segment doesn't use Euler angles. It represents angular velocity as an axis of rotation with a variable magnitude that increases as angular velocity increases.

##### Share on other sites
Quote:
 Original post by SiCrane1) Gimbal lock doesn't come from Euler angles. You can avoid gimbal lock while using Euler angles and you can have gimbal lock if you use quaternions. What matters is how you combine your rotations and not how your rotations are represented. Euler angles appear to be more prone to gimbal lock because the obvious ways to combine rotations when using Euler angles is not a particularly good one.2) The first code segment doesn't use Euler angles. It represents angular velocity as an axis of rotation with a variable magnitude that increases as angular velocity increases.

1) Could you explain that one? It's a rather strong contradiction of what I thought I knew.

2) Sounds like a 3-axis quaternion then, correct? Would a modified Quaternion-Matrix conversion be the correct way to handle conversion between it and Matrix rotation?

2a) This also explains why my conversion routines were going poorly.

##### Share on other sites
There seems to be quite a lot of questionable stuff here. Not sure if I can address it all, but here are a few comments:
Quote:
 I currently use the following code to convert from a force to a velocity and angular velocity: public virtual void ApplyForce(Force force) { velocity += force.Vector * InverseMass; Vector3 crossVector = Vector3.Cross(force.Vector, force.Offset); crossVector = new Vector3(crossVector.X * InverseInertia.X, crossVector.Y * InverseInertia.Y, crossVector.Z * InverseInertia.Z); angularVelocity += crossVector; }
Just out of curiosity, are you factoring any sort of time step in anywhere?
Quote:
 It is, however, (unless I'm missing something big) Eular angles.
As SiCrane said, there are no Euler angles in that code.
Quote:
 Eular angles are undesirable due to gimble lock (such as could be expected to happen in a free-roaming spaceflight game (Elite, Privateer or Freespace, for example).
Actually, it's the opposite; you wouldn't expect gimbal lock in those games.
Quote:
 However, Matrices (the 3x3 or 4x4 type used as a "3D computer gaming standard" have one great weakness - They cannot represent a rotation greater than 180 degrees.
Whatever such limitations rotation matrices might have aren't really weaknesses, per se (and are certainly not 'great weaknesses').
Quote:
 That is because they are technically not rotations; the 3x3 part is three normalized vectors pointing in the local (using left-handed coordinates) right, up and forward directions.
Not sure about the 'technically not rotations' part, but left-handed coordinates have absolutely nothing to do with it. A rotation matrix taken by itself has no inherent (spatial) handedness.
Quote:
 Quaternions are a normalized vector axis and a rotation around that axis
Not really. An axis-angle rotation can be encoded in quaternion form, but a quaternion itself isn't as you described.
Quote:
No; there is nothing about a quaternion that specifies in what units the angle is represented (or was originally represented).
Quote:
 So, the incoming force (a vector representing the direction and magnitude of force) and offset (a vector distance from the objects' local center) need to be converted to a quaternion rotation to avoid Eular gimble lock.
I'm not sure any of this has anything to do with gimbal lock.
Quote:
 My thought is: Take the vector cross of the force and offset. Cross this third vector with the offset and that should result in a vector perpendicular to it and the offset. Normalize it and make it the quaternions' axis and give it a rotation of 0. Then, "add" it to the quaternion rotation. Or; public virtual void ApplyForce(Force force) { velocity += force.Vector * InverseMass; Vector3 crossVector = Vector3.Cross(force.Vector, force.Offset); crossVector = new Vector3(crossVector.X * InverseInertia.X, crossVector.Y * InverseInertia.Y, crossVector.Z * InverseInertia.Z); crossVector = Vector3.Cross(Vector3.Normalize(crossVector), Vector3.Normalize(force.Offset)); Quaternion angularVelocity = new Quaternion(Vector3.Normalize(crossVector), 0); }And: "rotation = rotation * Quaternion.Slerp(Quaternion.Identity, angularVelocity, timestep);"Would this work? If not, can it work and what needs to be changed?
That does not sound correct :| Have you read (e.g.) Chris Hecker's and David Baraff's papers on rigid body dynamics? In one of those papers I think it shows how to update a rigid body's orientation using quaternions. (Not sure if this would address your question exactly, but it might get you closer to an answer.)
Quote:
 1) Could you explain that one? It's a rather strong contradiction of what I thought I knew.
Quote:
 2) Sounds like a 3-axis quaternion then, correct?
What's a '3-axis quaternion'?
Quote:
 Would a modified Quaternion-Matrix conversion be the correct way to handle conversion between it and Matrix rotation?
What's a 'modified Quaternion-Matrix conversion'?

You seem to be stating quite a few things as fact that are questionable or simply false, so it might be good to back up a bit and take a fresh run at things. One thing you might consider is forgetting about quaternions for now; they're more or less ancillary to the problem(s) you're trying to solve, and thinking about things in terms of quaternions may just be confusing the matter at this point.

Again, I'd recommend checking out the papers I mentioned, as both sets of papers do a good job of explaining the basics of rigid body simulation.

##### Share on other sites
I'll reply in another post, rather than dealing with all that quote code.

I factor Timestep in when velocity is applied. ApplyForce is meant to be instantaneous, although - I might be wrong on this, too, but I should probably call it ApplyImpulse.

Unfortunately for me, I don't even know what "Axis Angles" are and wasn't even aware they were something.

Quote:
 Actually, it's the opposite; you wouldn't expect gimbal lock in those games.

Well, yes; that's why I wouldn't want to use a Quaternion or Rotation Matrix, not Eular. Apparently Eular doesn't necessarily mean possible Gimbal Lock (as per the other guy's post), but I didn't know that - I wasn't aware Eular could be applied in a way that completely avoided Gimbal Lock.

Quote:
 Whatever such limitations rotation matrices might have aren't really weaknesses, per se (and are certainly not 'great weaknesses').

I disagree; the inability of something that represents rotations, to represent rotations over 360 degrees, is, in my mind, a weakness, perhaps even a great one.

Quote:
 Not sure about the 'technically not rotations' part, but left-handed coordinates have absolutely nothing to do with it. A rotation matrix taken by itself has no inherent (spatial) handedness.

I specified a coordinate system just so I could specify in which direction the rotation matrix vectors pointed, in it. Somewhat recursive, except that it explains the "technically not rotations" part - Although perhaps "not an angular rotation" would work better. They aren't; they're the local axis.

Quote:
 Not really. An axis-angle rotation can be encoded in quaternion form, but a quaternion itself isn't as you described.

Quaternions can be represented as a Vector and Scalar; if not an axis and rotation about it, I don't know how that works.

Quote:
 No; there is nothing about a quaternion that specifies in what units the angle is represented (or was originally represented).

Well, yes, you can represent rotations in, for example, Degrees. However, unless Wikipedia has lied to me, angular math is just easier with Radians. So, while they don't necessarily need radians, I don't see any particular need to use anything else in them, for my purposes.

Quote:
 I'm not sure any of this has anything to do with gimbal lock.

As said, I thought "Axis Angles" was just another word for "Eular Angles". An assumption I shouldn't have made. And, since I didn't know Gimbal Lock could be completely avoided with Eular...

Quote:
 That does not sound correct :| Have you read (e.g.) Chris Hecker's and David Baraff's papers on rigid body dynamics? In one of those papers I think it shows how to update a rigid body's orientation using quaternions. (Not sure if this would address your question exactly, but it might get you closer to an answer.)

I can't say for certain; I've read some papers and websites, certainly. Fully understood them, no, but that's one reason I'm working at this.

Quote:

I thought Gimbal Lock was part of Eular Angles; I also thought Quaternions were immune to it. And, I didn't think there was any way to completely avoid Gimbal Lock using Eular Angles. None of it seems contradictory; it simply contradicted all the listed that I had "learned" about Eular and Gimbal Lock.

Quote:
 What's a '3-axis quaternion'?

Here we go back to my interpreting Quaternions being represented as a Vector and a Scalar to mean an axis and rotation around it. If the vector is a normal vector, then the two could be combined into, singly, a vector. Since this is not the case, I shall seek further actual knowledge. And, on second thought, that thought is false, too - An axis and rotation around it could not be combined into a single vector.

Part of my problem is most of the webpages I find don't explain the basics; they seem to assume you know them. (Wikipedia, I'm looking at you). I'm left trying to
comprehend the basics from the advanced and, often, only expert information. By expert and advanced, I mean things someone without a college education and whos high-school years were more than a decade in the past, wouldn't know or would have difficulty remembering.

Quote:
 What's a 'modified Quaternion-Matrix conversion'?

And after SiCrane's post, I thought he meant a Vector Quaternion. Which doesn't exist, but I believe I've explained that faulty chain of poor logic. :)

Quote:
 You seem to be stating quite a few things as fact that are questionable or simply false, so it might be good to back up a bit and take a fresh run at things. One thing you might consider is forgetting about quaternions for now; they're more or less ancillary to the problem(s) you're trying to solve, and thinking about things in terms of quaternions may just be confusing the matter at this point.Again, I'd recommend checking out the papers I mentioned, as both sets of papers do a good job of explaining the basics of rigid body simulation.

Yes, at this point, learning what "Axis Angles" are would be useful. And this thread is me backing up and taking another run at it; I knew I was wrong somewhere, so I decided to simply ask where.

The basics sounds very good.

##### Share on other sites
Quote:
 I disagree; the inability of something that represents rotations, to represent rotations over 360 degrees, is, in my mind, a weakness, perhaps even a great one.
Well, there's really not much to disagree with. Saying this is a weakness of matrices is kind of like saying it's a weakness of a screwdriver that it's hard to hammer in a nail with it. If you need to represent rotations in some other way, use another representation. Obviously matrices are widely used to represent orientations and rotations, and this 'weakness' you mention doesn't seem to cause too many problems in general.

But, I guess it's just a matter of opinion, so I'll move on to some things that are more fact-oriented:
Quote:
 Quaternions can be represented as a Vector and Scalar; if not an axis and rotation about it, I don't know how that works.
A vector and a scalar doesn't have to be an axis and a rotation around it. In fact, even when a quaternion encodes a rotation, the vector and scalar parts *still* aren't a normalized axis and a rotation around it. So in short, no, no matter how you state it, the vector and scalar parts of a unit-length quaternion are not a unit-length axis and an angle of rotation about that axis.
Quote:
 Well, yes, you can represent rotations in, for example, Degrees. However, unless Wikipedia has lied to me, angular math is just easier with Radians. So, while they don't necessarily need radians, I don't see any particular need to use anything else in them, for my purposes.
Ok, but that's different than what you said initially, which was:
Quote:
 Quaternions are a normalized vector axis and a rotation around that axis, represented in radians
Which is wrong on both counts (the vector and scalar parts of a quaternion are not a normalized axis and a rotation around that axis, and there's nothing about a quaternion that specifies whether radians or degrees are being used).

The reason I'm emphasizing these points is that this material is complicated enough already without further confusing things with inaccurate statements or fuzzy terminology. Thinking that the vector and scalar parts of a quaternion that represents a rotation are a unit-length axis and an angle of rotation is incorrect, and is just going to cause you further confusion down the road.
Quote:
 I also thought Quaternions were immune to it [gimbal lock].
Nope. A common misconception, but completely false.

##### Share on other sites
Right; whatever the vector part is, it isn't an axis and a scalar (which I thought was in radians. Before your first post in this thread).

Part of my communications problem is I generally and naturally think in concepts, not words. Or at least, that's my understanding. I can't exactly compare thinking patterns with other people, but I don't think things like "get the peanut butter" in words. Nor have I thought about any of this thread in terms of words, except to "translate" to and from them. I understand that's different from most people, but how am I supposed to know if it actually is or how? All I know is, whenever I mention that, no-one's ever said "well, that's how most people think". For all I know, I may just have normal poor communication skills. Which would be ironic and annoying in no-one having provided the necessary information for me to discern that.

But enough about that. I've bought Algebra I and II of the "For Dummies" books to brush up and as a quick-reference; after I've done that, I'll get the pre-calculus and then calculus books. After that, Chris' Heckers .pdfs. After that? Well, no point in planning that far.

Edit: Correction. Tried to buy; the purchase doesn't seem to have gone through. I've sent them off an e-mail.

Further Edit: Axis Angles, according to Wikipedia, are a vector and a rotation around that vector. Which leads me to wonder if I hadn't gotten wires crossed on them somewhere before. If so, I don't recall and didn't recognize the term anew.

[Edited by - Narf the Mouse on October 23, 2010 3:09:29 PM]

##### Share on other sites
Oh, how Hamilton has turned in his grave...

##### Share on other sites
Quote:
 Original post by FenrisulvurOh, how Hamilton has turned in his grave...

...But the rooster crows at midnight. You gonna explain that, or do I have to guess?

Hmm...I know! You're a grave-robber! :D

##### Share on other sites
Thanks for the help; I think I'm further along. That being said;

Ok, gotten confused on something in Chris' Heckers Rigid Body papers: http://chrishecker.com/Rigid_Body_Dynamics
Specifically, #4, the angular velocity part. I *Think* I've got everything else well enough, but that part is making my head hurt.

What I've got: We use an Axis Angle for Angular Velocity. We do a matrix cross-product and a timestep multiplication to rotate the rigid body (change orientation). We use the angular velocity and some cross-product to find the speed of a rotating point.

What I'm confused on that I need to work on right now: How we go from a force (vector force, vector offset) to an angular velocity axis angle.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 12
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633314
• Total Posts
3011326
• ### Who's Online (See full list)

There are no registered users currently online

×