1

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

    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.

GoTo 是玩家单击的位置,当前位置是粒子发射的位置。我认为问题是因为他用 int 打印图像,当我发送它打印时,他错过了点 x.xxxx 之后的数字

  _______
 /
/

他上去然后直奔,我要他直奔主题

  /
 /
/

我该如何解决?

4

1 回答 1

0

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

一个错误可能是您的计算方式alpha。你这样做会错过x/的标志。yatan2办法克服这个问题。只是:

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) ;

(请注意,您已将sin/切换cosxVel/ yVelsin始终是y-axis,cos-axis x。)

我认为您在“移动对象”代码中还有另一个错误。你错过了yVel那里。

这可能是您想要的代码:

//move the object
iceBallParticle[i]->SetCurrentLocation(
  iceBallParticle[i]->GetCurrentLocation().x - xVel,
  iceBallParticle[i]->GetCurrentLocation().y - yVel);

我真的不明白为什么你在那里有“向左移动”检查。它不应该在那里。

(顺便说一句,计算 更常见GoTo - GetCurLoc。然后你有正确符号的速度,你通常添加速度,而不是减去它。)

于 2013-10-15T14:52:06.010 回答