Movement in the direction of rotation in opengl

I'm really new to OpenGL, but I have a pretty good idea of โ€‹โ€‹basic trigonometry (I forgot quite a bit after school!), But I have problems with that.

I have a symbol that moves back and forth along the Z axis. To move left and right, instead of firing, I want them to rotate (by pressing the left and right arrow keys respectively), and then when they again press forward / backward, they move in the direction in which they are.

So, I have the left / right functions add / subtract small amounts to the angle variable, which is used to draw the character. The forward / back functions then add / subtract small amounts to the x and z axis variables. They are the following (for back):

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

The header variable is the angle that is controlled by the left and right arrow keys.

I thought this would work, because when the player moves straight ahead, the header is 0 and therefore cos (0) = 1 and sin (0) = 0, which means that they do not move anywhere on X, but send the full amount 0.005 on Z. I assume that my rudimentary knowledge of trigonometry was not entirely justified, because if I turn a little and then move forward, they go in this direction, but if I move a little and then move forward, they will go to the same line if it is rotated 90 degrees, and continues again, as if it were 180 degrees, and then 270 degrees, etc.

EDIT: I will try to explain this better, basically, if I push forward by turning left, it will go in the direction I want, but if I let go forward, turn a little more and then press forward again, the angle increased, as did should be, but the direction is like 90 degrees around the direction in which it should go. Sorry, I canโ€™t explain it.

EDIT: Well, I am having some strange problems that I think can cause the strange "90 degrees" problem. When I get the character 90 degrees (using hard coding) = 90) left / right, cos (title) should be 0 correct? But for some reason it comes out as -0.44, and if I cos-1 (-0.44), I get 116.1, is it somehow related to Math.cos (), requiring an angle in radians or something else? I'm completely lost here.

Is this the right way to solve this problem? I am completely stuck in traffic jam and negative signs ...

Any answer is welcome,

thanks

Infinitifizz

(Also, I know that I should use deltaTime for the symbol speed / rotation values, not hardcoded 0.005f, but I want this problem to be the first before I sort it).

+4
source share
1 answer

It must be * not +

based:

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

you need:

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

If this is what you need? I must admit, I did not fully understand your description of what is actually happening.

EDIT: You probably want to use a larger โ€œradiusโ€ than 0.005, depending on how your coordinate system works.

+4
source

Source: https://habr.com/ru/post/1339507/


All Articles