简体   繁体   中英

SFML atan2 function and deceleration

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM