[英]How To Move Image To Point in SDL C++
i try to move particle image to the point that the player will click on the screen. 我尝试将粒子图像移动到播放器将在屏幕上单击的程度。 i use this formula from physics to calculate speed vector.
我使用物理学中的这个公式来计算速度矢量。
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 is where the player clicked and the current location is where the particle shot from. 转到是玩家单击的位置,当前位置是从中射击粒子的位置。 i think the problame is becuz he print the image by int and when i send it to print he miss the number after the point x.xxxx
我认为问题是因为他通过int打印图像,当我发送图像进行打印时,他错过了x.xxxx点之后的数字
_______
/
/
he go up and then straight, and i want him to go straight to the point 他先走然后直走,我要他直走到终点
/
/
/
how i can to fix it? 我该如何解决?
I'm not exactly sure if I understand the question correctly. 我不确定我是否正确理解了这个问题。
One error is probably how you calculate alpha
. 一种错误可能是您如何计算
alpha
。 You miss the sign of x
/ y
by doing it that way. 这样会错过
x
/ y
的符号。 There is atan2
to overcome this problem. 有
atan2
可以解决此问题。 It's just: 只是:
float alpha = atan2(y, x);
But you don't need to calculate alpha
at all. 但是您根本不需要计算
alpha
。 An easier solution would be: 一个更简单的解决方案是:
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) ;
(Note that you have switched sin
/ cos
for xVel
/ yVel
. sin
is always the y
-axis, cos
the x
-axis.) (请注意,您已切换
sin
/ cos
的xVel
/ yVel
。 sin
总是y
轴, cos
的x
轴)。
I think you have another error in the "move the object" code. 我认为您在“移动对象”代码中还有另一个错误。 You missed
yVel
there. 您在那里错过了
yVel
。
This might be the code you want: 这可能是您想要的代码:
//move the object
iceBallParticle[i]->SetCurrentLocation(
iceBallParticle[i]->GetCurrentLocation().x - xVel,
iceBallParticle[i]->GetCurrentLocation().y - yVel);
I don't really see why you have the "Move Left" check there. 我真的不明白为什么要在此处进行“向左移动”检查。 It should not be there.
它不应该在那里。
(Btw., it is more common to calculate GoTo - GetCurLoc
. Then you have the velocity with the right sign and you normally add the velocity, not subtract it.) (顺便说一句,计算
GoTo - GetCurLoc
更为常见。然后您的速度带有正确的符号,并且通常将速度相加,而不是相减。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.