预测球路 - 人工智能
我正在 Flash 中使用 Box2D 实现一个简单的排球游戏。我需要为 CPU 玩家实现一些 AI。我们就叫他杰克吧。因此,杰克需要预测球被人类玩家约翰击中后会落在哪里。我有以下信息:
- 约翰移动时球的初始线速度(x 和 y 方向) 击中它。
- 约翰击球时球的初始位置。 (x 和 y 坐标 值)
- 重力值。
- 球即将落下的地板的 y 位置值。
- 约翰击球的角度。
- 球遵循抛射轨迹。
因此,杰克需要预测球落地时的位置(x 值)。
我认为当球遵循射弹轨迹时,问题可能与从某个已知的初始位置、已知的初始速度和固定重力发射炮弹并预测其着陆点的问题相同。着陆点的 y 值也是已知的。空气阻力为零。
是否有任何数学方程可以帮助预测着陆点的 x 值?我查看了一些射弹方程,但大多数都将“时间”作为变量。我需要一个不涉及这个“时间”变量的解决方案。对于此问题的任何类型的解决方法也将不胜感激。谢谢你! :)
I'm implementing a simple Volleyball game using Box2D in Flash. I need to implement some AI for the CPU player. Let's call him Jack. So Jack needs to predict where's the ball gonna land after it has been hit by John, the human player. I have the following information :
- Initial Linear Velocity (both in x & y direction) of the ball when John
hits it.- The initial position of the ball when John hits it. (x & y coordinate
values)- The value of gravity.
- The value of y position of floor where's the ball's gonna fall.
- The angle at which John hits the ball.
- The ball follow a projectile trajectory.
So Jack needs to predict what would be the position (x value) of the ball when it hits the floor.
I think as the ball follows a projectile trajectory the problem might be seen same as that of - firing a cannon ball from some known initial position, known initial velocity with fixed gravity and predicting its landing spot. The y value of landing spot is also known. The air resistance is zero.
Is there any kind of mathematical equation which might help to predict the x value of the landing spot? I took a look at some projectile equations but most of them take 'time' as a variable. I need to have a solution that doesn't involve this 'time' variable. Any kind of work-arounds for this problem would also be appreciated. Thank you! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有一个以
t
表示的z
(即高度)方程,那么您需要求解z = 0
以获得球落地时t
的值。然后,您可以将其反馈到x
和y
的方程中。If you have an equation for
z
(i.e. height) in terms oft
, then you need to solve that forz = 0
to get the value oft
when the ball lands. You can then feed that back into the equations forx
andy
.您必须使用以下方程求解 z(x)=0:
g 是重力,v0 是沿 x 轴的初始速度,a 是角度。
当你求解它时,它给出了连接起点和终点的线段的长度(选择一个终点取决于击打方向)。
如果您处于 3D 环境中,则必须进行一些投影来消除问题的 3D 部分,并且只保留两个轴。
You have to solve z(x)=0 with this equation:
g is gravity, v0 is initial speed along x axis, a is the angle.
When you solve it, it gives the length of the segment joining start point and end point (choose one end point depending on the hit direction).
If you're in 3D, you'll have to do some projections to remove the 3D part of the problem, and keep only two axis.
当然,方向是负 y 方向。
32.2 ft/sec^2 = 9.8 m/sec^2
,对吧?如果没有其他玩家触及它,它将在 y = 0 处击中。
我认为最好谈谈约翰对球施加的力以及持续的时间。
不,你没有这样的东西。这就是你要解决的问题。
牛顿定律:
F = ma,
其中力是施加到球上的力的矢量,m 是球的质量,a 是施加到球上的加速度矢量。当然,球会因重力而在负 y 方向上加速,但您忘记了球员击球时施加的力矢量。
一旦你有了这些,你就可以及时求解两个耦合的常微分方程。
And the direction - negative y-direction, of course.
32.2 ft/sec^2 = 9.8 m/sec^2
, right?It'll hit at y = 0 if no other player touches it.
I think it'd be better to say something about the force John applies to the ball and for how long.
No, you have no such thing. This is what you're trying to solve.
What you have is Newton's law:
F = ma,
where force is the vector of force applied to the ball, m is the mass of the ball, a is the acceleration vector applied to the ball.The ball is accelerated by gravity in the negative y-direction, of course, but you're forgetting the force vector that the player applies when the ball is struck.
Once you have those you solve two coupled ODEs forward in time.