SFML atan2功能和减速
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
}
基本上,使用此代码后发生的情况是,我的飞船直接被拉到屏幕底部,就像地面有重力一样,我不明白我做错了什么
详细说明我的评论:
您没有正确地将角度转换为度数。它应该是:
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;
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 对可变参数使用声明.如何选择正确的功能
- 询问在设计我的手臂模拟器功能表示格式1
- 功能原型的目的
- SFML atan2功能和减速