0

I have a on_key function which fires if the button 'w'(forward),'a'(rotate left), 'd'(rotate right) is pressed, if 'w' is pressed I want to calculate a new x,y. I have the x,y coordinates and a degree, depends on which degree the object currently has i want to calculate new coordinates which goes for example one pixel in that direction.

        _ship->x = _ship->speed*cos(_ship->rotation+90)+_ship->x;
        _ship->y = _ship->speed*sin(_ship->rotation+90)+_ship->y;

This is my solution which does not work. note: _ship->rotation+90, +90 because the picture is perpendicular

Example: I pressed w and rotated it.

example

djkobi97
  • 77
  • 6

1 Answers1

1

C++ trig functions cos() and sin() take arguments in radians not degrees.

To convert degrees to radians multiply degrees by pi/180.0

John3136
  • 28,809
  • 4
  • 51
  • 69
  • To convert from degrees to radians, multiply your angle with (PI/180). – RmbRT Mar 14 '19 at 22:11
  • You have to enable the M_PI constant manually: it is described in [this question](https://stackoverflow.com/questions/1727881/how-to-use-the-pi-constant-in-c)'s answer. – RmbRT Mar 14 '19 at 22:13
  • Okay but it still makes strange things: https://media.giphy.com/media/55u2lg9YQYRmkjkEiG/giphy.gif Change: (_ship->rotation+90 * 3.141)/180 – djkobi97 Mar 14 '19 at 22:18
  • @djkobi97 your angle in degrees is `_ship->rotation + 90`, so you need to multiply _that_ by pi/180. Your formula should thus be `(_ship->rotation + 90) * 3.14159 / 180.0`. – alter_igel Mar 14 '19 at 22:30
  • _ship->speed*sin(((90+_ship->rotation) * 3.141)/180)*-1+_ship->y; this is the final solution 1. convert int to radians and 2. multiply y it with -1; thx to everonye – djkobi97 Mar 14 '19 at 22:40