圆周运动计算的问题
problems with circular movement calculations
问题被原海报撤销
嘿,所以当运行下面的代码时,我的正方形应该在一个圆圈中移动,但计算x,y移动的函数存在某种问题,这种移动应该基于移动的速度和角度。
它成功地四处旅行,但方式不对。第二和第四象限有点倒置,朝着圆心向内弯曲,而不是向外弯曲。
我不知道问题出在哪里…有人想帮忙吗?
#include<SFML/Graphics.hpp>
#include<SFML/System.hpp>
#include<cmath>
#include<vector>
# define M_PI 3.14159265358979323846
sf::RenderWindow Window;
template<typename T>
void CalculateMove(T Time, T Speed, T Angle, T& buffX, T& buffY)
{ //Make the degrees positive
if(Angle<0) Angle= 360-Angle;
//determine what quadrant of circle we're in
unsigned int Quadrant= 1;
if(Angle>90) Quadrant= 2;
if(Angle>180) Quadrant= 3;
if(Angle>270) Quadrant= 4;
//anything above 90 would be impossible triangle
Angle= (float)(Angle-(int)Angle)+(float)((int)Angle%90);
// calculates x and y based on angle and Hypotenuse.02433
if((int)Angle!=0){
buffX= sin(Angle / 180 * M_PI)/ (1.f/(Speed*Time));
buffY= sin((180-Angle-90)/ 180 * M_PI)/ (1.f/(Speed*Time));}
else{// Movement is a straight line on X or Y axis
if(Quadrant==0 || Quadrant==2) buffX= Speed*Time;
if(Quadrant==1 || Quadrant==4) buffY= Speed*Time;}
//Quadrant Factor (positive or negative movement on the axis)
switch(Quadrant){
case 1: break;
case 2: buffX=-buffX; break;
case 3: buffX=-buffX; buffY=-buffY; break;
case 4: buffY=-buffY; break;}
};
///////////////////////////////////////// Mysprite ////////////////////////////////
class mySprite : public sf::Sprite
{
private:
float velocity;
float angle;
public:
// all the values needed by the base class sprite();
mySprite(
const sf::Image& Img,
const sf::Vector2f& Position = sf::Vector2f(0, 0),
const sf::Vector2f& Scale = sf::Vector2f(1, 1),
float Rotation = 0.f,
const float Angle= 0.f,
const float Velocity= 0.f,
const sf::Color& Col = sf::Color(255, 255, 255, 255)):
Sprite(Img, Position, Scale, Rotation, Col){
angle= Angle;
velocity= Velocity;};
float Velocity(){return velocity;};
void SetVelocity(float newVelocity){velocity=newVelocity;};
float Angle(){return angle;};
void SetAngle(float newAngle){angle=(float)(newAngle-(int)newAngle)+(float)((int)newAngle%360);};
void Update(){
float frameTime= Window.GetFrameTime();
float X=0,Y=0;
CalculateMove(frameTime,velocity,angle,X,Y);
Move(X,-Y);
};
void Accelerate(float PPS){velocity+=PPS;};
void Turn(float degrees){
float test= (float)((angle+degrees)- (int)(angle+degrees)); //TODO: Get rid of these test
float test2=(float)((int)(angle+degrees)%360);
float test3=test+test2;
angle=(float)((angle+degrees)-(int)(angle+degrees))+(float)((int)(angle+degrees)%360);};
void Reflect(float CollAngle){
SetRotation(-GetRotation());
angle=-angle;
//TODO: factor in the collision angle
};
};
int main()
{
Window.Create(sf::VideoMode(800, 600), "Pong! by Griffin Howlett");
sf::Image img;
img.Create(30,50,sf::Color(255,0,0));
mySprite box(img, sf::Vector2f(400,200), sf::Vector2f(1,1), 0, 180, 200);
Window.Display();
for(;;){
Window.Clear();
box.Update();
box.Turn(45.0*Window.GetFrameTime());
Window.Draw(box);
Window.Display();
}
}
您的第一个错误:
if(Angle<0) Angle= 360-Angle;
应该是:
if(Angle<0) Angle= 360+Angle;
我不太明白你为什么要麻烦地把角度分成象限。你认为sin
函数只定义在0到90度的范围内吗?
不确定所有问题,但这行代码是错误的:
if(Angle<0) Angle= 360-Angle;
如果Angle < 0
,则360角度将>360
您也可以清理象限设置代码,否则当"角度">270时,您将执行4次赋值。
int Quadrant = 1;
if (Angle > 270)
{
Qadrant = 4;
}
else if (Angle > 180)
{
Quadrant = 3;
}
else if (Angle > 90)
{
Quadrant = 2;
}
我似乎错了,假设三角形形成并用于计算到达x、y坐标所需的移动,它总是自动使用y轴作为"角度"的相反侧,并指示象限2和4的坐标是向后的,不过感谢其他反馈!
这是更新后的代码:
if((int)Angle!=0){
if(Quadrant==2 || Quadrant==4) Angle=90-Angle; //The unit circle triangle is flipped otherwise, causing x and y to be switched
buffY= sin(Angle / 180 * M_PI)/ (1.f/(Speed*Time));
buffX= sin((180-Angle-90)/ 180 * M_PI)/ (1.f/(Speed*Time));}
通过做90角,我切换用来找到假想三角形的X和Y边的角度。。。。
相关文章:
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- C++理解计算字符变量的问题
- 使用指针计算堆栈问题的大 O 表示法
- 使用 cblas_dgemm 计算伪逆的问题
- 我的C++级数计算程序出了什么问题?被困了好几个星期
- 成绩计算问题
- 关于骰子概率计算的一个C++问题
- 我想知道我将如何实现 + 运算符重载.我已经从我上一个问题中计算出 += 运算符重载
- 在 64 位 Linux armv8 计算机上编译 32 位二进制文件时遇到问题
- 在C 中显示小数点,计算问题
- 计算最大成对产品C 的问题
- 优化递归问题以计算超级数字
- 传递数组和计算平均C++的问题
- 冰雹序列C 函数需要计算序列的长度.无限循环问题
- 计算当前日期和用户输入日期问题之间的天数
- 关于 Eigen::MatrixXd 逐列计算的问题
- 谁可以帮助解释我的代码中出了什么问题.计算C ++中的数组
- 在C 中给定范围内计算素数的问题
- 我的逻辑有什么问题?尝试计算文件中包含特定字符的单词数