PDA

View Full Version : Cumulative transformation/rotation

07-07-2003, 09:54 AM
OK,

I'm going to try and explain this better this time. In OpenGL, the glTransform and glRotate are cumulative (presumably because they operate on a homogeneous matrix) in that if you rotate and then transform your axis for the transformation will have been rotated as well:

glTranslatef(v1.x, v1.y, v1.z);
glRotatef(angle, 0.0f, 0.0f, 1.0f);

glBegin();
glEnd();
glFlush();

Vector v1 stores the current location of an object. The Problem:

Every time you want to move an object that has velocity, you base your next position on the screen off of:

x += velocity * cos(angle * (PI/180));
y += velocity * sin(angle * (PI/180));

Well, what if you want to rotate your object and keep the velocity going the same direction? The way I have it now, if you rotate the object, it will rotate the modelview coordinate system, which in turn applies the velocity to the new angle the next time around in the game loop. I don't want this behavior. I want the velocity to be constant in one direction, until you specifically put on thrusters in a different direction. In other words, you could rotate all you want, but you'd still be going the same direction until you hit the thrusters, just like the game Asteroids.

What do I do to eliminate this undesired effect? I'm storing variables X, Y, Z, velocity, deltaVelocity, angle (in degrees) and deltaAngle.

If I were to describe this effect visually, imagine in the real Asteroids applying the thrusters once, then letting go and rotating. In the real game the ship will rotate in place, and move in a constant direction until you thrust somewhere else.

However, in mine if you hold down left or right while moving (but not thrusting) the ship will move in a circle with a diameter directly proportional to the amount of velocity you have. Nice for drawing circles - terrible for cloning Asteroids. How do I keep the velocity from being rotated to another direction unless I specifically apply thrusters?

I hope everyone can understand what I'm saying this time and I appreciate the patience of people who have read my other post and maybe DID understand it. I have tried the one solution suggested so far and while it did make my code more complete, the problem still exists - unless I was simply mistaken in how I implemented it. I'm aware of the fact that the problem is my lack of knowledge, but no book I've read discusses this.

I'm looking forward to any help anyone can provide. This is my first project with OpenGL and I've already learned a lot. Thanks!

starman
07-07-2003, 10:10 AM
Your code doesn't work? Hmmm...looking at figure 3-4 in the Red Book (http://tassadar.physics.auth.gr/~chameleon/OpenGL/The%20Red%20Book/), I would think that it would work.

If you think in terms of the "grand, fixed coordinate system", your ship should be rotated about the Z-axis at the origin, then translated to the point you specify (because in this case, the transformations occur in the order opposite of your code).

If you think in terms of a "local coordinate system", your ship should be translated to the point you specified and then translated about the Z-axis in the local system (because in this case, the transformations occur in the order they appear in your code).

Or am I getting this bass-ackwards?

07-07-2003, 10:30 AM
I must be failing to understand something very simple. My translations and rotations work as intended separately, just not together. I can be at origin x = 0.0 y = 0.0 and rotate. I can rotate fine anywhere as long as I am stationary. I can stop, rotate and then translate and I will move in the direction I'm supposed to be going.

The problem occurs when I try to do both at the same time. If I'm translating, and I try to rotate while I'm translating, I'll move in a circle. Now in one respect this condition is desirable - when I'm applying thrust. But when I'm not applying thrusters, I should just be able to rotate and my rotation should not affect the direction towards which the velocity was originally applied.

Am I making sense here?

07-07-2003, 10:38 AM
For the sake of the people who have not played Asteroids:
http://www.mathcs.sjsu.edu/faculty/rucker/asteroids/asteroidsj1jar.html

Apply thrust, and then let go. Rotate. Notice your direction and velocity will not change while you're rotating unless you apply thrust again.

In mine, the direction will change when you rotate, regardless of whether you are thrusting or not.

starman
07-07-2003, 11:16 AM
I know what you're saying now. I did a quick google search on "asteroids ship movement" and found the thread below. The third message has some BASIC code that the purportedly solves the problem.