SFML atan2功能和减速

时间:2014-11-11 05:07:28

标签: c++ sfml trigonometry acceleration atan2

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



}
基本上,使用这段代码后发生的事情是我的船被直接拉到屏幕的底部,并且表现得像地面有重力,我不明白我做错了什么

1 个答案:

答案 0 :(得分:0)

详细说明我的评论:

您没有正确地将角度转换为度数。它应该是:

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

但是,你在另一个trig计算中使用这个角度,并且C ++ trig函数需要弧度,所以你真的不应该首先将它转换为度数。您的 else if 语句也可能导致问题,因为您正在检查大于0的绝对值。尝试这样的事情:

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