How to get coordinates based on direction in c++ math

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.


- - Source


answered 1 week ago John3136 #1

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

To convert degrees to radians multiply degrees by pi/180.0

comments powered by Disqus