PDA

View Full Version : 3D Movement in space



04-27-2000, 02:26 PM
Hello everybody,

I have been searching for a while, but I couldn't find anything about moving 3D. I have seen a lot of solutions for moving 2D: along the x and z axes with rotation only around the Y-axe.

My problem is about rotating and moving around all three *local* axes. I want to create a demo which involves a space shuttle.

Fox

04-27-2000, 02:32 PM
What I wanted to ask, if somebody knows a solution or information about this problem.

Fox

Humus
04-27-2000, 03:16 PM
Don't know if i understand the problem right, but use glRotate() to rotate around the axis one at a time.

dmy
04-27-2000, 03:21 PM
it's very simple with opengl.

you have to associate a modelview matrix with every object: this matrix will hold the information about heading and position.

you can build a function like this into the object class:





double mv[4][4];

void transform( rx,ry,rz,tx,ty,tz ) {
glMatrixMode(GL_MODELVIEW);
glLoadMatrixd((double *)mv);
glRotatef(rx,1,0,0);
glRotatef(ry,1,0,0);
glRotatef(rz,1,0,0);
glTranslatef(tx,ty,tz);
glGetDoublev(GL_MODELVIEW_MATRIX,(double *)mv);
}



mv is a private member var of your class.

this works by applying incremental rotations and traslations to the object's modelview matrix.

into the application:





object.transform(rx,ry,rz,tx,ty,tz);
object.draw();



...even better, you can make the .draw() method to apply the current rotation and traslation only when it is called, so you can use a more intuitive interface, like this:





void rotate( x,y,z ) {
rot.x=x;
rot.y=y;
rot.z=z;
}

void translate( x,y,z ) {
xlt.x=x;
xlt.y=y;
xlt.z=z;
}

void draw() {

glMatrixMode(GL_MODELVIEW);
glLoadMatrixd((double *)mv);

glRotatef(rot.x,1,0,0);
glRotatef(rot.y,0,1,0);
glRotatef(rot.z,0,0,1);

glTranslatef(
xlt.x,
xlt.y,
xlt.z
);

glGetDoublev(GL_MODELVIEW_MATRIX,(double *)mv);

setmem(&rot,sizeof(rot),0);
setmem(&xlt,sizeof(xlt),0);

<render>

}



again, rot, xlt and mv are private members.

then your application will look something like this:





<handle user input>

object.rotate(
user.rot.x,
user.rot.y,
user.rot.z,
);

object.translate(
user.xlt.x,
user.xlt.y,
user.xlt.z,
);

object.draw();



since you're operating over the same matrix over and over, sometime (to say, every 30 frames) is good to renormalize the matrix.

also, note that translations will always be executed into object space, so if you translate -100 units along the Z axis, whatever will be your object orientation, it will travel forward.

is it good for you?

Dolo/\/\ightY


[This message has been edited by dmy (edited 04-27-2000).]

[This message has been edited by dmy (edited 04-27-2000).]