Move in the rotation direction in OpenGL

I am new to OpenGL, but I have a good grasp of basic trigonometry (I have forgotten a lot since school!) but I am in trouble.

I have a character that moves forward and backward on the Z axis. To move left and right instead of strafing, I want them to rotate (press the left and right arrow keys respectively), and then when they move towards When pressing forward/backward, they move in the direction they are facing.

So what I did is let the left/right function add/decrease the amount on an angle variable, which is used to draw the rotation of the character .Then the forward/backward function will add/subtract a small amount to the x and z axis variables. They are as follows (backward):

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

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

I think this is feasible, Because when the player advances directly, the title is 0, so cos(0) = 1 and sin(0) = 0, which means they move nowhere on X but forward the full 0.005 amount on Z. I guess mine Basic triangle knowledge is not completely reasonable, because if I turn a bit and then move forward they will move in that direction but if I move forward a bit and then move forward they will move in the same line, if it rotates 90 degrees , And continue and then again it seems like it is 180 degrees and then 270 degrees and so on.

Edit: I will try to explain better, basically if I turn left and press forward it will move towards me Go in the desired direction, but if I let go, turn forward a little bit and then push forward, the angle has increased as it should, but the direction is 90 degrees from the direction it should go. Sorry, I can’t explain this Question.

Edit: Okay, I got some weird problems, which I think may cause weird “90 degree” problems, when I make the character look 90 degrees (by hard-coded heading=90) left /Right, cos (title) should be 0, right? But for some reason, it appears as -0.44, if I cos-1(-0.44) I get 116.1, is it related to Math.cos(), want radians or other angles? I am totally lost here.

Is this the correct way to solve this problem? I am totally stuck in the trial and error flag…

Any response is appreciated,

Thank you

InfinitiFizz

(In addition, I Know that I should use deltaTime to represent the speed/rotation value of the character, instead of the hard-coded 0.005f, but I hope to solve this problem before sorting).

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 this is what you want to pursue? I must admit that I do not fully understand your description of the actual situation.

Edit: You may want to use a “radius” larger than “0.005”, depending on how the coordinate system works.< /p>

I am new to OpenGL, but have a good grasp of basic trigonometry (I have forgotten a lot since school!) But I am in trouble.

I have a character that moves forward and backward on the Z axis. To move left and right, instead of shooting, I want them to rotate (press the left and right arrow keys respectively), and then when When they press forward/backward again, they move in the direction they are facing.

So what I did is to make the left/right function add/decrease the amount on an angle variable for drawing The rotation of the character. Then the forward/backward function will add/subtract a small amount to the x and z axis variables. They are as follows (backward):

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

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

I think This works because when the player goes straight forward, the title is 0, so cos(0) = 1 and sin(0) = 0, which means they move nowhere on X but forward the full 0.005 amount on Z. I I think my basic knowledge of triangles is not completely reasonable, because if I turn a bit and then move forward they will move in that direction but if I move forward a bit and then move forward they will move in the same line, if it Rotate by 90 degrees, and continue and then again it seems like it is 180 degrees and then 270 degrees and so on.

Edit: I will try to explain better, basically if I turn to the left and press it forward Go in the direction I want, but if I let go, turn forward a bit and then push forward, the angle has increased as it should, but the direction is 90 degrees from the direction it should go. Sorry, I Can’t explain the problem.

Edit: Okay, I got some weird problems, which I think may cause strange “90 degree” problems, when I make the character look 90 degrees (by hard-coded heading = 90) Left/right, cos (title) should be 0, right? But for some reason, it appears as -0.44, if I cos-1(-0.44) I get 116.1, is it related to Math.cos(), want radians or other angles? I am totally lost here.

Is this the correct way to solve this problem? I am totally stuck in the trial and error flag…

Any response is appreciated,

Thank you

InfinitiFizz

(In addition, I Know that I should use deltaTime to represent the speed/rotation value of the character, instead of the hard-coded 0.005f, but I hope to solve this problem before sorting).

< p>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 this is what you want to pursue? I must admit that I do not fully understand your description of the actual situation.

Edit: You may wish to use a “radius” larger than “0.005”, depending on how the coordinate system works.< /p>

Leave a Comment

Your email address will not be published.