# How could we use trigonometry for moving around a 3D World?

Well I have a question about geometry and 3-Dimensional player movement in general. I'm using Java, OpenGL and LWJGL.

This is basically what I got:

• currentFrontSpeed: Is a float that contains the speed of the Player.
• player. increasePosition: Just adds the current Position of the Player to the value that is given on the X, Y and Z axis.

//The point we want to go: Forwards(+) Backwards(-). getFrameTimeSeconds
//just adjust the speed depending of the speed of the computer.

float distanceFront = currentFrontSpeed * DisplayManager.getFrameTimeSeconds();

//Distance of Movement
float fx = (float) (distanceFront * Math.sin(Math.toRadians(player.getRotY())));
float fz = (float) (distanceFront * Math.cos(Math.toRadians(player.getRotY())));

//This just increase the position of the player (x,y,z)
player.increasePosition(fx, 0, fz);


Another thing that I didn't explained is that the Player is always looking at the same position as the camera (if the camera turns to the right, the player would also turn to the right. Like Maincraft). That's why I use the Y-Axis to move.

The problem is that my player can only move forwards and backwards (if currentFrontSpeed is positive, my player will move FORWARDS, and if is negative BACKWARDS). And the only form to go to a place that is to the right is to move the Camera to the right and the press UP to go FORWARDS.

I've tried to achieve this doing this:

   //The same as above, the speed of the player this time Right(+) and Left(-)
float distanceSides = currentSideSpeed * DisplayManager.getFrameTimeSeconds();

float sx = (float) (distanceSides * Math.cos(Math.toRadians(player.getRotY())));
float sz = (float) (distanceSides * Math.sin(Math.toRadians(player.getRotY())));

//I just add the "horizontal" speed to the "vertical" one
player.increasePosition(fx + sx, 0, fz + sz);


Of course this doesn't work good: if the player Y-rotation is 0, the player moves to the right and to the left perfectly; but for example, if the player rotation is 90 (looking at the right from the original position), moving up or moving to the right would take us to the same position (because is the "right" from the original position with the Y-axis 0).

I don't know if I should use another trigonometric function or add the sx and the sz to another value in the X, Y and Z axis.

EDITED

I've just edited and gived some information because of the bad explaining I did. Also this is one of my first long-writed post.

Thanks for helping!

• Could you explain more clearly what you're trying to do? This is all about how you're trying to do it, which is quite unclear without the context of a goal. – Anko Jan 23 '16 at 1:01
• Pretty sure this is a duplicate of something, but if your "forward" unit vector is (fx, 0, fz) then the "right" unit vector is either (fz, 0, -fx) or its negation, depending on the handedness/orientation of your coordinate system. – DMGregory Jan 23 '16 at 1:36
• The code you provided is not enough to deduce you problem, you also should try to clarifying your question. Still the first code-snippet should have worked given the angles {0, 90, 180, 270}. So it might have been something wrong with player.increasePosition() or some other code not shown. – Christer Jan 23 '16 at 9:22
• I have edited the question. Sorry if someone hasn't understand me u.u – Daniel Béjar Jan 23 '16 at 10:45

If I understand you correctly you are having problems going left and right. First off currentFrontSpeed should never be negative since it's speed not velocity. Speed is a scalar and as such should not capture any direction and will always be positive.

To move in different directions you should modify the angle instead where for example right is -90 from forward and left is +90 from forward.

Here is one example. You might want to do it differently but hopefully you get the idea.

float distanceFront = currentFrontSpeed * DisplayManager.getFrameTimeSeconds();
float speed;
float angleY;

// A direction key is being pressed
if (KEY_FORWARD) {
angleY = player.getRotY();
speed = distanceFront;
}
if (KEY_RIGHT) {
angleY = player.getRotY() - 90;
speed = distanceFront;
}
if (KEY_LEFT) {
angleY = player.getRotY() + 90;
speed = distanceFront;
}
if (KEY_BACKWARD) {
angleY = player.getRotY() + 180;
speed = distanceFront;
}

float fx = (float) (speed * Math.sin(Math.toRadians(angleY)));
float fz = (float) (speed * Math.cos(Math.toRadians(angleY)));

player.increasePosition(fx, 0, fz);

• The problem of this is that you can only move right, left, up and down, not diagonal... – Daniel Béjar Jan 23 '16 at 11:39
• Ok everything is solutioned, I had to mix what i got after and what you send me now :D – Daniel Béjar Jan 23 '16 at 11:48