Search code examples
opengl3drotationtrigonometrypath-finding

Move in direction of rotation in opengl


I'm really new to OpenGL but have quite a good grasp of basic trigonometry (forgotten quite a bit since school!) but I'm having trouble with this.

I have a character that moves forwards and backwards on the Z axis. To move left and right, rather than strafing I want them to rotate (when pressing left and right arrow keys respectively) and then when they press forwards/backwards again they move in the direction they're facing.

So what I did was have the left/right functions add/subtract small amounts onto an angle variable that is used to draw the character's rotation. Then the forward/backward functions add/subtract small amounts to the x and z axes variables. They are as follows (for the backwards):

z -= 0.005f * Math.cos(heading);
x -= 0.005f * Math.sin(heading)

The heading variable is the angle which is manipulated by the left and right arrow keys.

I thought this would work because when the player is going straight forward the heading is 0 and so cos(0) = 1 and sin(0) = 0 which means they move nowhere on the X but forwards the full 0.005 amount on the Z. I guess my rudimentary trigonometry knowledge wasn't completely sound because if I turn a bit then move forwards they go in that direction but if I move a bit more then move forwards they go in that same line if it was rotated 90 degrees, and continues then again as if it was 180 degrees then 270 degrees etc.

EDIT: I'll try and explain that better, basically if I press forwards after turning to the left it will go in the direction I want, but if I let go of forward, turn a bit more and then press forward again, the angle has increased as it should have but the direction is like 90 degrees around from the direction it should be going. Sorry I can't really explain this well.

EDIT: Okay, I'm getting some weird problems that I think might be causing the strange "90 degrees" problem, When I get the character to look 90 degrees (through hard-coding heading=90) left/right, cos(heading) should be 0 right? But for some reason it is coming out as -0.44, and If I cos-1(-0.44) I get 116.1, is it something to do with Math.cos() wanting the angle in radians or something? I'm completely lost here.

Is this the right way of going about this problem? I'm completely stuck just trial and erroring around with minus signs...

Any reply is appreciated,

Thanks

InfinitiFizz

(Also, I know I should use a deltaTime for the speed/rotation values of the character not a hard-coded 0.005f but I want to get this problem out of the way first before I sort that).


Solution

  • It should be * not +

    based on:

    x = r cos (theta)
    y = r sin (theta)
    

    you will want:

    z -= 0.005f * Math.cos(heading);
    x -= 0.005f * Math.sin(heading);
    

    If that's what you're after? I must admit, I didn't fully understand your description of what actually happens.

    EDIT: you'll probably want to use a larger "radius" than 0.005, depending on how your coordinate system is working.