# Sideways movement

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.

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

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 = cos(static_cast<float>(mouse.x));
matrix = -sin(static_cast<float>(mouse.x));
matrix = 1;
matrix = sin(static_cast<float>(mouse.x));
matrix = cos(static_cast<float>(mouse.x));
matrix = 1;

``````

And then I multiply the vector like this:

``````
tempVector = vector * matrix + vector * matrix + vector * matrix + matrix;
tempVector = vector * matrix + vector * matrix + vector * matrix + matrix;
tempVector = vector * matrix + vector * matrix + vector * matrix + matrix;
vector = tempVector;
vector = tempVector;
vector = tempVector;

``````

Then I do this with character coordinates:

``````
CharZpos += vector;
CharXpos += vector;

``````

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

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

[QUOTE=tonyo_au;1249093]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[/QUOTE]

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.

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;

``````

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;

``````

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.

[QUOTE=tonyo_au;1249113]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;

``````

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;

``````

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.[/QUOTE]

Thank you very much for help.