Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Quaternion Math

Name: Anonymous 2010-10-21 18:44

I'm making a tetris type game as part of my learning process for Unity3D. I'm handling piece rotation by:
-have a variable for desired angle
-the player changes this in steps of 90deg with keypresses
-every frame, rotate 1/10th of the way there.

Code is:
//set a var for the (req)uired angle
  var anglereq : Vector3 = Vector3(0,0,0);
//put the difference between the current angle and the anglereq into a temp
  var temp : Vector3 = anglereq - rigidbody.rotation.eulerAngles;
//add 1/10th of temp to the current angle (as im dealing with Vec3's, need to convert the rigidbody.rotation into eulers,
//add the temp/10, then back into Quats), this new quat goes into newrot
  var newrot : Quaternion = Quaternion.Euler(rigidbody.rotation.eulerAngles + temp/10);
//Set the rotation to newrot
  rigidbody.MoveRotation(newrot);

This works fine, until the angle required goes over 0,0,360, or drops into negatives.

Name: Anonymous 2010-10-21 20:01

>>6
Goddamn, this is so easy and it's the whole point of using quaternions in the first place.  Learn geometric algebra.  It's easier that way.

First of all, you're really working with versors, which in 3-dimensional euclidean space we identify with the unit-length quaternions.  And you don't add or subtract them, you multiply them.  Remember that multiplication is NOT commutative in this algebra.

Suppose you have two orientations represented by versors q1 and q2.  You want to find qd, which is the versor that will get you from orientation q1 to q2.

In other words,

    q[sub]2[/sub] = q[sub]1[/sub] * q[sub]d[/sub]


So how do you compute qd?  Solve!  Note that for unit quaternions, q-1 = q*.  Quaternions also have the same right and left inverses, q-1 exists and is unique unless q = 0.

    q[sub]d[/sub] = q[sub]1[/sub][sup]*[/sup] * q[sub]2[/sub]


Now of course, you don't divide qd into 10 pieces, just like you don't subtract versors from each other.  You are instead trying to find the 10th root of qd.  Or, more generally, just compute qdt where t goes from 0 to 1.

It looks like this is all way over your head anyway so suffice it to say you can look up a formula to compute powers of quaternions.

(Seriously people, go learn geometric algebra if you want to use it.  It won't take long.)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List