简体   繁体   English

如何计算球击中目标所需的速度?

[英]How to calculate needed velocity for a ball to hit targer?

I want to calculate velocity for ball to hit target(my ball/finger).我想计算球击中目标(我的球/手指)的速度。 x and y of my ball are know and are changing while I'm moving my finger but assume that it is not changing (X and Y are some random number) and ball that should reach my finger is coming from left upper corner (x=0,y=0).我的球的 x 和 y 是已知的并且在我移动手指时发生变化,但假设它没有变化(X 和 Y 是一些随机数)并且应该到达我手指的球来自左上角(x= 0,y=0)。 How to calculate velocityX and velocityY for that ball in order to hit me ?如何计算该球的速度X和速度Y以击中我?

You could set velocityX to negative if the ball is X is greater then the target's X else set velocity to a positive number if it is less then the target's X. This would make the ball move towards the X coordinate of the target.如果球的 X 大于目标的 X,则可以将速度 X 设置为负数,如果小于目标的 X,则将速度设置为正数。这将使球向目标的 X 坐标移动。 Just do the same for the y Axis and the ball should move towards the target.只需对 y 轴执行相同操作,球就会向目标移动。 However, this may cause the ball to "shake" when it reaches the target so, in order to fix this, set velocityX and velocity to 0 when it reaches the target.但是,这可能会导致球在到达目标时“晃动”,因此,为了解决此问题,请将到达目标时的速度 X 和速度设置为 0。

Hope this helped.希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM