View Full Version : Sideways movement

Hitokage

03-17-2013, 06:45 AM

Hi.

Have some problems with geometry and imagining the 3D movement. I have a game where you can move according to camera angle forward or backward. My problem is when I want to move the character sideways. You know like step to side.

Here is code I use for movement forward and backward.

CharXpos += float(cos(mouse.y*piover180)*sin(mouse.x*piover180 )) * 0.1f;

CharZpos -= float(cos(mouse.y*piover180)*cos(mouse.x*piover180 )) * 0.1f;

I use mouse position as an angle for rotation the scene. My idea was that if I half that angle it should return the angle to side but it is not working that way as I found out :D.

tonyo_au

03-17-2013, 06:24 PM

You would do best to handle movement with the concept of vectors rather than angles. You can apply rotations and translations to vectors. For example if F is the foward directions (vector) and U is the up vector; right the cross product of U and F. If this makes no sense to you try reading something like http://www.wildbunny.co.uk/blog/vector-maths-a-primer-for-games-programmers/

Hitokage

03-18-2013, 08:31 AM

You would do best to handle movement with the concept of vectors rather than angles. You can apply rotations and translations to vectors. For example if F is the foward directions (vector) and U is the up vector; right the cross product of U and F. If this makes no sense to you try reading something like http://www.wildbunny.co.uk/blog/vector-maths-a-primer-for-games-programmers/

alright thanks I will check that out

Hitokage

03-18-2013, 12:35 PM

Ok I know about vectors and matrices, used them in opengl sometimes but now have problem with that.

Let's say I have vector and matrix.

I edit the matrix every time the scene rotate like this

matrix[0] = cos(static_cast<float>(mouse.x));

matrix[8] = -sin(static_cast<float>(mouse.x));

matrix[5] = 1;

matrix[2] = sin(static_cast<float>(mouse.x));

matrix[10] = cos(static_cast<float>(mouse.x));

matrix[15] = 1;

And then I multiply the vector like this:

tempVector[0] = vector[0] * matrix[0] + vector[1] * matrix[4] + vector[2] * matrix[8] + matrix[12];

tempVector[1] = vector[0] * matrix[1] + vector[1] * matrix[5] + vector[2] * matrix[9] + matrix[13];

tempVector[2] = vector[0] * matrix[2] + vector[1] * matrix[6] + vector[2] * matrix[10] + matrix[14];

vector[0] = tempVector[0];

vector[1] = tempVector[1];

vector[2] = tempVector[2];

Then I do this with character coordinates:

CharZpos += vector[1];

CharXpos += vector[0];

Where is the mistake? :D Sry to disturb you but really have no idea right now.

tonyo_au

03-18-2013, 05:13 PM

This is a rotation matrix not a displacement/translation matrix. When you rotate you don't move from the spot you are on.

This is some pseudo code for stepping right, but staying facing forward

right = cross_product(forward,up)

right = normailize(right)

character_pos = character_pos + right * side_step_size

you code is more like

forward = rotate(angle) * forward

character_pos = character_pos + forward

You may like to look at the glm library for vector and matrix maths functions

Hitokage

03-18-2013, 11:56 PM

This is a rotation matrix not a displacement/translation matrix. When you rotate you don't move from the spot you are on.

This is some pseudo code for stepping right, but staying facing forward

right = cross_product(forward,up)

right = normailize(right)

character_pos = character_pos + right * side_step_size

you code is more like

forward = rotate(angle) * forward

character_pos = character_pos + forward

You may like to look at the glm library for vector and matrix maths functions

Yea I know this is not side step I just wanted to keep the same movement direction after rotating the scene. Letš say I just want to move to one direction by the vector I have above. I Multiply it with rotation matrix as I did and move then. The problem is that even this is not working.

To be exact could you tell me how to keep the direction of movement with direction vector and rotation matrix?

I did it with that sin,cos thing before.

tonyo_au

03-19-2013, 06:13 PM

First off I would swap to a proper vector library like glm. It makes it easier to see matching components.

You coder is mostly right but you are adding the y component to the z component in this code

CharZpos += vector[1];

You have to decide what is "up" - in OpenGL it is by convention the positive y axis (but it doesn't have to be).

Your matrix multiply is rotating around the positive y axis but the character displacement implies a rotation around the z axis.

CharZpos += vector[2];

should work or using a rotation around the z-axis

Also the way you are using the mouse x as the angle which feels wrong. The angle paramenter to the sin/cos function is in radians ~0-6.28318 for 360 degrees.

Your mouse x is likely to be 0 - 1000.

I would use a mouse delta (ie current mouse x - last mouse x) as the change in angle in degrees.

If you do this you have to test for the angle being less that 0 and reset it to (360-angle) and greater than 360 and reset it to (angle-360) to keep it in the range 0-360.

Hitokage

03-20-2013, 06:17 AM

First off I would swap to a proper vector library like glm. It makes it easier to see matching components.

You coder is mostly right but you are adding the y component to the z component in this code

CharZpos += vector[1];

You have to decide what is "up" - in OpenGL it is by convention the positive y axis (but it doesn't have to be).

Your matrix multiply is rotating around the positive y axis but the character displacement implies a rotation around the z axis.

CharZpos += vector[2];

should work or using a rotation around the z-axis

Also the way you are using the mouse x as the angle which feels wrong. The angle paramenter to the sin/cos function is in radians ~0-6.28318 for 360 degrees.

Your mouse x is likely to be 0 - 1000.

I would use a mouse delta (ie current mouse x - last mouse x) as the change in angle in degrees.

If you do this you have to test for the angle being less that 0 and reset it to (360-angle) and greater than 360 and reset it to (angle-360) to keep it in the range 0-360.

Thank you very much for help.

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.