圆周运动计算的问题

时间:2011-06-17 03:10:01

标签: c++ graphics geometry

原始海报取消的问题

嘿,所以当运行下面的代码时,我的方块应该绕一圈运行,但是这个函数存在某种问题,它根据行进的速度和角度计算应该发生的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();
    }

}

3 个答案:

答案 0 :(得分:4)

你的第一个错误:

if(Angle<0) Angle= 360-Angle;

应该是:

if(Angle<0) Angle= 360+Angle;

我不太清楚你为什么要把角度划分为象限。您是否认为sin函数仅定义为0到90度的范围?

答案 1 :(得分:0)

不确定所有问题,但这行代码错误:

if(Angle<0) Angle= 360-Angle;

如果Angle < 0则360度 - 角度将是&gt; 360

您也可以清除象限设置代码,否则当角度为&gt;时270,你做了4次任务。

int Quadrant = 1;
if (Angle > 270)
{
    Qadrant = 4;
}
else if (Angle > 180)
{
    Quadrant = 3;
}
else if (Angle > 90)
{
    Quadrant = 2;
}

答案 2 :(得分:0)

似乎我错误地假设形成三角形并用于计算到达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侧的角度....