Jump to content
  • Advertisement
Sign in to follow this  
Isolier

Movement and Viewing Vectors

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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);
glLoadIdentity();

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!