计算移动球与移动线/多边形碰撞的时间(2D) [英] Calculate time when a moving ball collides with a moving line/polygon (2D)

查看:251
本文介绍了计算移动球与移动线/多边形碰撞的时间(2D)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多边形,里面是一个移动的球。
如果球击中边界,球应该反弹。

I have a polygon and inside of it is a moving ball. The ball should bounce back if it hits a border.

我当前的'解决方案':
我将多边形分成线并计算何时球击中移动线

My current 'solution': I split the polygon in lines and calculate when the ball hits the moving line

所有变量:

a = length of a
b = length of b
c = length of c
ax = x position of A
ay = y position of A
bx = x position of B
by = y position of B
cx = x position of C
cy = y position of C
vax = speed of A on the x-axis
vay = speed of A on the y-axis
vbx = speed of B on the x-axis
vby = speed of B on the y-axis
vcx = speed of C on the x-axis
vcy = speed of C on the y-axis
h = height (equals r, because it collides when h is r)
r = radius
t = time (one time unit equals 1 frame. not relevant)
axc = x positon of A at the collision
ayc = y positon of A at the collision
bxc = x positon of B at the collision
byc = y positon of B at the collision
cxc = x positon of C at the collision
cyc = y positon of C at the collision




  1. 计算所有点的碰撞位置:

  1. Calculate the collision position of all points:

axc:= ax + vax * t
ayc:= ay + vay * t

bxc:= bx + vbx * t
byc := by + vby * t

cyc:= cy + vcy * t
cxc:= cx + vcx * t

计算所有顶点的长度

a:=√((axc-cxc)^(2)+(ayc-cyc)^(2))

b:=√((bxc-cxc)^(2)+(byc-cyc)^(2))

c:=√((axc-bxc)^(2)+(ayc-byc)^(2) )

计算h

h =((√(2 *(A ^(2)* b ^(2)+ b ^(2)*一个^(2)+ C ^(2)*一个^(2)) - (一^(4- )+ b ^(4)+ c ^(4))))/(2 * c))

解决t

solve(h =((√(2 *(a ^(2)* b ^(2)+ b ^(2) *一个^(2)+ C ^(2)*一个^(2)) - (一^ (4)+ b ^(4)+ c ^(4))))/(2 * c)),t)

BUUUUUT:我的计算器(Ti-Nspire CX CAS)崩溃了。微软数学需要太长时间(我现在正在计算......持续1小时但仍然没有...)

BUUUUUT: My calculator (Ti-Nspire CX CAS) crashes. And Microsoft Mathematics takes waaay too long (I am calculating right now... for 1 hour and still nothing...)

所以...帮助!

(不要质疑我的绘画技巧)

(Don't question my paint skills)

推荐答案

如果您的多边形是凸起和所有速度始终保持不变,你可以使用我刚刚提出的这些系列技巧(所以可能有更好的方法):

If your polygon is convex and all velocities remain the same all the time, you could use these series of tricks I just came up with (so there is probably some better way):

只有在多边形为凸面时才能执行此操作。请考虑以下图像:

You can do this only if the polygon is convex. Consider the following image:

红线是原始多边形,绿色线是无限延伸。这个圆圈在遇到红线之前是否曾经遇到绿线?不。我们现在可以专注于击球无限长的线,这对于(至少对我而言)是更简单的任务。

The red lines are the original polygon, the green ones are the infinite extension. Can the circle ever hit a green line before it hits a red line? No. We can now focus on ball hitting infinitely long lines, which (at least for me) is simpler task.

如果我们想知道一个完美的圆球是否以及何时击中一条线,我们可以很容易地解决一个点是否碰到一条线,考虑下面的图像:

If we want to know whether and when a perfectly round ball hits a line, we can easily solve if a point hits a line instead, consider following image:

基本圈当它的中心将进入该线周围的区域时将会出现一条线,该区域是距离该线最多半径的所有点,其中 radius 是圆的半径。

Basicly circle will hit a line when it's center will enter an area around that line, where the area are all points that are at most radius away from the line, where radius is the radius of the circle.

所以我们可以继续只需用中心的一个点替换圆圈并移动通过半径朝向新创建的点的线。

So we can go ahead and just replace the circle with a single point at the center and move the line towards the newly created point by the radius.

如果该行由两个移动点 a 定义和 b ,速度 va vb ,以及点是 c ,速度 vc ,我们可以点 a 静止(不移动)和位置(0,0),用 ba 替换其他两点位置和速度, vb-va ca vc-va

If the line is defined by two moving points a and b with velocities va and vb, as well as the point is at point c with velocity vc, we can make point a stationary (not moving) and at position (0,0) by replacing other two points location and velocities by b-a, vb-va and c-a, vc-va.

现在让我们将 b c 的新坐标和速度命名为: [bx,by] (vbx,vby) [cx,cy] (vcx,vcy)。我们现在可以通过求解这个公式来计算碰撞时间:

Now let's name the new coordinates and velocities of b and c like this: [bx, by], (vbx, vby) and [cx, cy], (vcx, vcy). We can now figure out the time of collision time by solving this formula:

cx+t * vcx = s*bx + s*t*vbx
cy+t * vcy = s*by + s*t*vby

然而要小心:这会导致二次方程,你必须忽略可能的负解,这可能意味着该点正在远离线或者碰撞正在发生,所以在你开始做任何事情之前确保球还没有碰撞。

However be careful: this leads to quadratic equation, and you have to ignore possible negative solutions, which could mean that the point is moving away from the line or that the collision is happening right now, so make sure the ball isn't already colliding before you start doing anything.

在你替换<$ c $之后(我希望没有必要这么说) c> t 和 s ,你不会得到最后的碰撞点,你需要撤消你已经完成的所有地役权(添加 a 例如)

Also (I hope there is no need to say this) after you substitute for t and s, you will not get the final point of collision, you need to undo all the easements you have done (add a for example)

如果你需要非凸多边形,我有一个解决方法,所以写评论。

If you need it for non-convex polygons, I have a workaround for it, so write in the comments.

这篇关于计算移动球与移动线/多边形碰撞的时间(2D)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆