PDA

View Full Version : rotating object



naisan
12-18-2008, 01:17 PM
Hello friends!

I have the following code to rotate a cube (x&y direction). When I rotate xPosition object rotates on x axis with no problems. When I rotate with yPosition, again object rotates on y axis, remaining stationary with no problems. However, when I combine them together (as the code shows), my x-axis rotation changes to rotate the object around an imaginary line, such that the object comes closer to the screen and further back instead of staying in the same place. What have I done wrong?


fAngle = ((-g_xRotation)/50);
xPosition[0] = 0 + (sin(fAngle));
xPosition[1] = 0;
xPosition[2] = 0 + (cos(fAngle));

Glu::gluLookAt(xPosition[0], 0, xPosition[2], 0, 0, 0, 0, 1, 0);

fAngle = ((-g_yRotation)/50);
yPosition[0] = 0 + (2 * cos(fAngle));
yPosition[1] = 0;
yPosition[2] = 0 + (2 * sin(fAngle));
Glu::gluLookAt(yPosition[2],0,yPosition[0], 0, 0, 0, 1, 0, 0);

dletozeun
12-18-2008, 08:43 PM
I don't really understand what you are trying to achieve with the last code. One advice is to take care of the order of your transformations. I mean, the last transformation that you set with the opengl transformation functions like glTranslate or glRotate and friends is the first that affects objects geometry, as the modelview matrix is modified successively by these transformations (think of transformations as matrices).

naisan
12-18-2008, 08:54 PM
I've spent about 11 hours on this so far and have modified my code to below:


Gl::glTranslatef(f,0.0f,-50.0f);
Glu::gluLookAt(-4.3, 5, 10, -4.3, 0, -50, 0, 1, 0);
Gl::glRotatef(g_xRotation, 0.f, 1.f, 0.f);
Gl::glRotatef(g_yRotation, 1.0, 0.0, 0.0);

I'm using the first line to place my objects next to each other accordingly. Then, I want to set the camera position and angle. Then, I'd like to make my rotations based on the center of all my objects. However, the rotations are along the x axis only. For example, I want to rotate around a line parallel to x axis, but -50 spaces in the z direction.

naisan
12-18-2008, 09:04 PM
I'm putting together the above method because I've simply given up on what I actually want to happen.

I'd want the camera to travel the path of an ovoid (egg shape) while keeping its focus on the center of the ovoid at all times. But I'm guessing the mathematical calculations for something like that would be extremely difficult.

_NK47
12-19-2008, 02:47 AM
its a bit difficult if you just started with 3d. the camera movement you describe can be achieved best and the easiest way through "keyframes". the position and rotation gets interpolation over time and you get a smooth movement.