PDA

View Full Version : How to rotate a vector with quaternion?(graphics, openGL)



Sabzi
12-04-2012, 11:42 AM
So it's not really a math problem, well, kinda ...

I have a little problem with my openGL homework.
I don't want to go into details so I go straight to the point.
We want to simulate the movement of a helicopter which only moves towards it's nose. (yep, fuck physics we got enough of that part in the homework anyway)
So first of course the helicopter is going forward and it's movement vector is (1, 0, 0).
Now we use quaternions to roll/pitch/yaw the helicopter so we got the helicopters quaternion. R for example rolls it with 20°.
The problem is that the formula we use to rotate the movement vector with the quaternion is not working/buggy.

But here it is anyway:
Quaternion temp(0, heli);
//heliQ.Normalize(); //not sure if this is needed
Quaternion a = heliQ.Inverse();
Quaternion b = heliQ%temp;
temp = b%a;
heli = temp.GetAxis();
glutPostRedisplay();


heliQ being the quaternion, heli being the movement vector. We were kinda trying things so maybe it's not pretty. % operator is quaternionXquaternion.
We wrote the inverse part too, we are quiet sure it's okay, but I post it too just in case:

float abs = sqrtf(s*s+d.x*d.x+d.y*d.y+d.z*d.z);
float abs2 = abs*abs;
Vector c = d*-1;
Quaternion temp(s, c);
return temp*(1/abs2);

d is the vector.
We were also debugging this so it's split apart too.

Thanks!

p.s.: How was the code highlight? I remember there was a way to for example c++ highlight, but I don't remember how.

p.s.s: And if you are really curious the physics part of the homework is to follow the helicopter with a camera that is bound to the helicopter by a rubber rope while it's also on a ballon with a normal rope. Balloon is affected by gravity, lift force(whatever in english), and air resistance. Good luck us ...

Justin
12-04-2012, 11:45 AM
*Waits for Brandons post*

Sabzi
12-04-2012, 11:46 AM
ustin;1136555']*Waits for Brandons post*

Well yea, I am also waiting for him, but who knows :D.

NKN
12-04-2012, 12:01 PM
Came here, was ready to flip a vector, thought you just needed to reverse the elements in it.

Read post http://i0.kym-cdn.com/photos/images/newsfeed/000/201/508/221435NothingtodoHereGifMadethiscuzIwas2479d828227 23.gif?1321553488

Kasi
12-04-2012, 12:54 PM
Brandon taught me quite a bit about vectors as such. if you wanna move a vector in 3d space you have to apply a matrix to it.

http://songho.ca/opengl/index.html

that site will help you out alot. look into it.

Sabzi
12-04-2012, 02:19 PM
What we are doing(trying) in the code I posted is what you say, but a bit simpler.
Because you can transform the quaternion into a matrix.
I will look into that link.

masterBB
12-04-2012, 02:31 PM
multiply the vector with this.

http://help.adobe.com/nl_NL/FlashPlatform/reference/actionscript/3/images/matrix_rotate.jpg

I can also explain a 3D vector?

also c++ tag: [highlight=c++].

Sabzi
12-05-2012, 06:17 PM
Well, I think our idea was good(maybe even the code too) and I start to think that I make something horribly wrong(because I don't understand openGL all that much) at another place.
I suspect this to be wrong: (horribly)

Vector axis = heliQ.GetAxis(); //this is in onDisplay btw
glPushMatrix();
glTranslatef(push.x, push.y, push.z);
glPushMatrix();
glRotatef(heliQ.GetRotationAngle(), axis.x, axis.y, axis.z);
drawChopper();
glPopMatrix();
glPopMatrix();


Push being the vector I want to move the helicopter with. Calculated like so: push = push + movement vector. In simulateWorld(called by onIdle) with the time difference and all, but it's not yet implemented.(easiest part)
I do realize it would be easier to draw the helicopter to a given x, y, z coordinate, but the whole modelling would fell apart then :S.

Sabzi
12-09-2012, 03:39 PM
If anyone is interested here is the final "product".
It was made in a rush because of tight deadlines and other finals to worry about hence I would not call it well-made.