PDA

View Full Version : Rotate object around its local coordinate system



Dannjell
02-22-2014, 01:08 PM
Hi! I'm trying to rotate a 3d object (just a square plane) around ITS coordinate system, and not Processings global coordinate system. When using rotateX(), rotateY(), and rotateZ(), the rotation is always around Processings origo. I want to do a rotation around x at first, which is fine to use rotateX since it's the first rotation, and then rotate around the objects new y-axis. In other words, I want the rotation to always be around the object itself. So imagine there being a coordinate system on the square plane with z-axis orthogonal against the plane, and the x- and y-axes in the plane (parallel).

I tried rotating around x first with rotateX, and then calculate a new y-axis (variable called new_vy) by multiplying the vector vy = {0, 1, 0} by x's rotational matrix, and then use rotate(angle, new_vy[0], new_vy[1], new_vy[2]);

Some semi-psuedocode: //Code begin

rotateX(angleX);

new_vy = Rx(angleX)*vy; //Rx is the rotational matrix for x-rotation. Note that this is a matrix operation,
//and the actual code doesn't look like this.
//vy is just the unit vector for the y axis, vy = {0, 1, 0}

rotate(angleY, new_vy[0], new_vy[1], new_vy[2]); //this function is pretty much the same as glRotatef(angle, x, y, z);

draw3dobject();

//Code end

This should be done for z aswell, but x and y is enough for starters.

So, any suggestions? Doesn't have to be a modification to my code, can be an entirely different method.

I've found something about quaternions, but I would prefer not to use them, since I don't entirely understand them, and I think that this should work with out them.

The purpose of this is to simulate a quadcopter, so therefor I need to input three separate angles, pitch, roll yaw, (euler angles).

/Daniel

Dannjell
02-22-2014, 01:10 PM
Wasn't allowed to post a link, so I'll just post it in a comment. A picture to describe my problem: postimg.org/image/5blmdo6bn/

wmelgaard
02-22-2014, 07:41 PM
glLoadIdentity();
glTranslatef(ctrx,ctry, ctrz);
glRotatef((pitch, 0, 1, 0);
glRotatef((roll, 1, 0, 0);
glRotate(yaw, 0,0,1);

model();

glRotate(-yaw),0,0,1);
glRotate(-(roll,1,0,0);
glRotate(-(pitch,0,1,0);
glTranslate(-ctrx, -ctry, -ctrz);

where crx, ctry, ctrz are the coordinates that you want to rotate around
Depending on how you are rotating your widget, you may want to change the order of roll, pitch & yaw. Just step out in the reverse order that you stepped in.
What you are doing is moving the center of the world to where you are building your widget; building your widget; and then moving the world back where it started from while taking your widget along.
This works between glBegin() and glEnd(). Unless somebody knows something that I don't know, changing ctrz has no effect whatsoever on a VBO. As a kludge, subtract ctrz from each vertex while building the VBO.

Dannjell
02-23-2014, 06:58 AM
Yeah, heard somewhere that you should load the identity matrix. I did get another answer on another forum though, using quaternions. But thanks for the help wmelgaard!

Carmine
02-25-2014, 06:22 PM
Have you found a solution to your problem yet?