SFML atan2 function and deceleration

937 views Asked by At
if(wIsPressed){
    movement.x += sin((player.getRotation() * 3.141592654)/ 180) * .5; //accelerates ship at a rate of 0.5 ms^2
    movement.y -= cos((player.getRotation() * 3.141592654)/ 180) * .5; //accelerates ship at a rate of 0.5 ms^2

}
else if(abs(movement.x) > 0 || abs(movement.y) > 0){
    double angle = (atan2(movement.x, movement.y) * 3.141592654) / 180; //finds angle of current movement vector and converts fro radians to degrees


    movement.x -= sin((angle)) * 0.5; //slows down ship by 0.5 using current vector angle
    movement.y += cos((angle)) * 0.5; //slows down ship by 0.5 using current vector angle



}

basically, what happens after using this code is that my ship is pulled directly down to the bottom of the screen, and acts like the ground has gravity, and i dont understand what i am doing incorrectly

1

There are 1 answers

2
Julian On

To elaborate on my comment:

You're not converting your angle to degrees properly. It should be:

double angle = atan2(movement.x, movement.y) * 180 / 3.141592654;

However, you're using this angle in another trig calculation, and the C++ trig functions expect radians, so you really shouldn't be converting this to degrees in the first place. Your else if statement could also be causing problems, because you're checking for an absolute value greater than 0. Try something like this:

float angle = atan2(movement.x, movement.y);
const float EPSILON = 0.01f;

if(!wIsPressed && abs(movement.x) > EPSILON) {
    movement.x -= sin(angle) * 0.5;
}
else {
    movement.x = 0;
}

if(!wIsPressed && abs(movement.y) > EPSILON) {
    movement.y += cos(angle) * 0.5;
}
else {
    movement.y = 0;
}