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 18:46

import bbcode.tags.code

Name: Anonymous 2010-10-21 18:47

Try % 360

Name: Anonymous 2010-10-21 18:48


//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);


cocks

Name: Anonymous 2010-10-21 18:52

>>3
Tried this
Works fine for positive (CCW) rotations over 360, negative (CW) below zero

but CW rotations over 360 (eg 450->360) and CCW negatives (-180 -> -90) make the piece rotate 270 degrees in the wrong direction

lolwat

Name: Anonymous 2010-10-21 19:02

>>1930578

It would be so simple if i could just get the difference between 2 quats (q3 = q1 - q2) then divided by 10 (q3/10) but you cant 'divide' quats liek that

unless you can, inwhich case i dont know how

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.)

Name: Anonymous 2010-10-21 20:26

>>7
Is it the HELPING HIM day today? I find it enjoyable.

Name: Anonymous 2010-10-21 20:34

>>8
It's not specifically the day of help. The problem is presented in a /prog/ approved manner and is interesting enough to warrant such assistance.

Name: Anonymous 2010-10-21 21:47

>>7
Is it the BBCODE EXPERT day today? I find it enjoyable.

Name: Anonymous 2010-10-21 23:19

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

Fuck that, learn it if you get the opportunity, even if you don't want to use it. Someday you will put it to use.

Name: Anonymous 2010-10-22 0:12

Testris
3D rotations
Quarternoins

Wrong solution to the wrong problem.

Name: Anonymous 2010-10-22 5:01

>>12
Agreed, I would have written code to handle each possible 90deg rotation

Name: Anonymous 2010-10-22 10:29

Quaternions solve the "problem" of gimbal lock, but it's stupid to think that a rotation can be un-done by rotating back in the same planes but in a different order.

Name: Anonymous 2010-10-22 12:57

it's stupid to think that a rotation can be un-done by rotating back in the same planes but in a different order
Is that really true of quaternions? It's certainly not true in general.

Name: Anonymous 2010-10-22 16:39

>>15
Turn an object 90 degrees in the XY plane.  Then turn it 90 degrees in the XZ plane.  Now try to turn it back by turning it first in the XY plane, and then in the XZ plane.  It's not possible.

Name: Anonymous 2010-10-22 21:32

>>16
rotating back in the same planes but in a different order
Great, now try reading all the words.

Name: Anonymous 2011-02-04 14:55


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