Movement and Viewing Vectors

This topic is 4887 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello, I have a simple app that lets you roam around a little world, however right now the movemnet mechanics work in a way that a tank works; the direction you are looking has nothing to do with the direction of travel. I would like to tie these two together so that the direction you look is the direction you move towards. Pardon me if there is a really simple solution, I have been coding for awhile and my brain is mush right now. Thanks for your help in advance.

Share on other sites
You probably already have the elements in place to move your object as you describe, however you'll probably have to give us some more info. You could post the relevant code excerpts, and/or give some details about how your object's orientation is represented and updated.

Share on other sites
Hey,

Sorry if I was a bit vague in my first post. The way I have things set up is I have a vector(well, float array with three elements) that represents the current position in the world, labeled movementVector. I also have another vector for the angle the user is looking, labeled viewVector. Now, viewVector isn't really a vector by definition, its really just a float array with three elements, one element for the angle of rotation about each axis; x, y and z. So basically what I am doing is this:

glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

getMovementVector(); // Check for keys pressed that modify movement
getViewVector(); // Check for keys pressed that modify view

glRotatef(viewVector[0], 1.0f, 0.0f, 0.0f);
glRotatef(viewVector[1], 0.0f, 1.0f, 0.0f);
glTranslatef(-movementVector[0], -movementVector[1], -movementVector[2]);

drawWorld();

Perhaps my whole system for moving about in the world is flawed, but it seems to me it should be easy to tie the angles stored in viewVector to movementVector such that movement vector is modified so that you actually move in the direction your are looking. Seems like I should be able to do it with a bit of trigonometry, I am just unsure how to go about it. Again, thanks in advance.

Share on other sites
Here's a tutorial on this subject: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=10

Share on other sites
The first thing I'd recommend is to change your variable names. 'movementVector' suggests something other than what you intend - something like 'position' would be better. Also, it looks like 'viewVector' is really a set of Euler angles, so you might rename it 'orientation' or 'angles'. A vector usually indicates a direction and magnitude, which really doesn't apply in either of these cases.

Another thing is that in these two lines:

glRotatef(viewVector[0], 1.0f, 0.0f, 0.0f);
glRotatef(viewVector[1], 0.0f, 1.0f, 0.0f);

The angle should probably be negated, as you are setting up a view matrix. If movement still doesn't work after making this change, there may be a problem in these functions:

getMovementVector(); // Check for keys pressed that modify movement
getViewVector(); // Check for keys pressed that modify view

So you might go ahead and post those as well.

Share on other sites
Typically, you'll have:

Quaternion lookOrientation;Vector3 position;Vector3 velocity;float impulse;

You'll implement movement controls something like this:

onLeftTurn() {  lookOrientation *= Quaternion( Up, TurnAngle );}onRightTurn() {  lookOrientation *= Quaternion( Up, -TurnAngle );}onForwardMovement() {  impulse = MovementImpulse;}

You'll implement the player movement (stepping/update) like this:

onPlayerUpdate() {  velocity += lookOrientation.frontVector() * impulse;  velocity *= (1 - Friction);  position += velocity * TimeStepSize;  impulse *= (1 - ImpulseDiedown)}

Initially, I'd try for a Friction of 0.04 and an ImpulseDiedown of 0.1, if your timestep is about one frame (i e, in the range of 0.012-0.017 seconds). The magnitude of MovementImpulse, TurnAngle, and the units for position/velocity depend on your world coordinate system.

In this system, you'll keep moving in the direction you were moving even if you look aside. However, as you start pressing move-forward when you look to the side, you'll start moving in that direction.

If you want the player to start moving in a new direction when they turn, even when they're not pressing forward, you can remove velocity components that are orthogonal to the forward vector. Add this as the second line of the update function:

  velocity -= (lookOrientation.rightVector() / velocity) * CorrectionAmount * lookOrientation.rightVector();

I'm assuming "/" between vectors is dot product. CorrectionAmount varies depending on how snappy adjustment you want; its value usually should be similar to that of the Friction coefficient, or slightly higher.

Share on other sites
Hey,

Well I want to thank everyone for their help, I have got it working. I had to use some trigonometry after all(darn math will get you everytime) but I am sure that was obvious for you guys. Also, you were right about negating the rotation angles in order to simulate the camera moving instead of the world itself moving. So, thanks again everyone!

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
A4L
13
5. 5

• 13
• 26
• 10
• 11
• 44
• Forum Statistics

• Total Topics
633744
• Total Posts
3013658
×