C++ 如何在SDL C++;

C++ 如何在SDL C++;,c++,sdl,C++,Sdl,我尝试将粒子图像移动到玩家在屏幕上单击的位置。 我用这个物理公式来计算速度矢量 float x = iceBallParticle[i]->GetCurrentLocation().x - iceBallParticle[i]>GetGoToX() ; float y = iceBallParticle[i]->GetCurrentLocation().y - iceBallParticle[i]->GetGoToY() ; float c = (

我尝试将粒子图像移动到玩家在屏幕上单击的位置。 我用这个物理公式来计算速度矢量

    float x = iceBallParticle[i]->GetCurrentLocation().x - iceBallParticle[i]>GetGoToX() ;
    float y = iceBallParticle[i]->GetCurrentLocation().y - iceBallParticle[i]->GetGoToY() ;
    float c = (x/y);
    float alpha = atan(c);

    //calculate the velocity x,y
    float yVel = (speedOfMoveSkill * cos(alpha)) ; 
    float xVel = (speedOfMoveSkill * sin(alpha)) ;

        //Move Left
        if(iceBallParticle[i]->GetCurrentLocation().x > iceBallParticle[i]->GetGoToX())
            //move the object
            iceBallParticle[i]->SetCurrentLocation(iceBallParticle[i]->GetCurrentLocation().x - xVel , iceBallParticle[i]->GetCurrentLocation().y);
. . . more moves direction down here.
转到是玩家单击的位置,当前位置是粒子从中射击的位置。 我认为问题是因为他用int打印图像,当我发送给他打印时,他错过了x.xxxx点后的数字

  _______
 /
/
他往上走,然后直走,我要他直奔主题

  /
 /
/

我怎样才能解决这个问题?

我不确定我是否正确理解了这个问题

一个错误可能是如何计算
alpha
。这样做会错过
x
/
y
。要克服这个问题,还有很多方法。只是:

float alpha = atan2(y, x);
但是您根本不需要计算
alpha
。更简单的解决办法是:

float dist = sqrt(x*x + y*y);
if(dist == 0) return; // exit here to avoid div-by-zero errors.
                      // also, we are already at our point

float yVel = (speedOfMoveSkill * x / dist) ; 
float xVel = (speedOfMoveSkill * y / dist) ;
(请注意,您已经为
xVel
/
yVel
sin
切换了
sin
始终是
y
-轴,
cos
始终是
x
-轴。)

我想“移动对象”代码中还有一个错误。你错过了那里的伊维尔

这可能是您想要的代码:

//move the object
iceBallParticle[i]->SetCurrentLocation(
  iceBallParticle[i]->GetCurrentLocation().x - xVel,
  iceBallParticle[i]->GetCurrentLocation().y - yVel);
我真的不明白你为什么要在那里签“向左移动”的支票。它不应该在那里


(顺便说一句,计算
GoTo-GetCurLoc
更常见。然后你有一个带右号的速度,你通常加上速度,而不是减去速度。)

注意
sin
通常是y轴,
cos
是x轴。你的问题有点不清楚。用户在屏幕上单击的位置是什么?或者你所说的“会点击”是什么意思?你是说将来?你怎么知道的?我梅娜:当我用光标指向并点击时,冰球不会沿着直线移动。它在一条曲线上移动。你的问题仍然有点不清楚。代码在哪里执行?多久执行一次?您在哪里设置
GoTo
?这都是密码吗?为什么只有“向左移动”的案例,其他案例呢?请添加完整的代码。