【问题标题】:SFML atan2 function and decelerationSFML atan2 功能和减速
【发布时间】:2014-11-11 05:07:28
【问题描述】:
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



}

基本上,使用此代码后发生的情况是我的船被直接拉到屏幕底部,并且就像地面有重力一样,我不明白我做错了什么

【问题讨论】:

  • 您没有正确地将角度转换为度数。应该是:双角 = atan2(movement.x, motion.y) * 180 / 3.141592654;
  • atan2 返回弧度还是度数?
  • 所有三角函数都期望并返回弧度。
  • 因为我的加速代码正常,但我的减速代码没有
  • 双角 = (atan2(movement.x, motion.y) * 180) / 3.141592654;

标签: c++ sfml trigonometry acceleration atan2


【解决方案1】:

详细说明我的评论:

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

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

但是,您在另一个三角计算中使用了这个角度,并且 C++ 三角函数需要弧度,所以您真的不应该首先将其转换为度数。您的 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;
}

【讨论】:

  • 当我做出这个改变时,飞船移动到屏幕底部,然后又飘回顶部
  • 成功了!我唯一需要改变的是:motion.y += cos(angle) * 0.5; to move.y -= cos(angle) * 0.5;和 epsilon 值为 0.4f
猜你喜欢
  • 1970-01-01
  • 2022-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 2020-11-01
相关资源
最近更新 更多