圆周运动计算的问题

problems with circular movement calculations

本文关键字:问题 计算 圆周运动      更新时间:2023-10-16

问题被原海报撤销

嘿,所以当运行下面的代码时,我的正方形应该在一个圆圈中移动,但计算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边的角度。。。。