MrA*_*del 5 java math geometry polygon
我有一个多边形,里面是一个移动的球.如果球击中边界,球应该会反弹.
我当前的'解决方案':我将多边形分成几行,并计算球何时击中移动线

所有变量:
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
Run Code Online (Sandbox Code Playgroud)
计算所有点的碰撞位置:
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)*a^(2)+c^(2)*a^(2))-(a^(4)+b^(4)+c^(4))))/(2*c))
解决问题
solve(h=((?(2*(a^(2)*b^(2)+b^(2)*a^(2)+c^(2)*a^(2))-(a^(4)+b^(4)+c^(4))))/(2*c)), t)
BUUUUUT:我的计算器(Ti-Nspire CX CAS)崩溃了.微软数学需要花费太长时间(我现在正在计算... 1小时但仍然没有...)
所以...帮助!
(不要质疑我的涂料技巧)
如果你的多边形是凸的并且所有速度始终保持不变,你可以使用我刚刚想出的这一系列技巧(所以可能有一些更好的方法):
仅当多边形是凸多边形时才可以执行此操作。考虑下图:

红线是原始多边形,绿线是无限延伸。圆圈在撞到红线之前能否撞到绿线?不。我们现在可以专注于击球无限长的线,这(至少对我来说)是更简单的任务。
如果我们想知道一个完美的圆球是否以及何时撞到一条线,我们可以轻松地解决一个点是否撞到一条线,请考虑下图:

基本上,当圆的中心进入该线周围的区域时,圆就会碰到一条线,该区域是距离radius该线最远的所有点,其中radius是圆的半径。
因此,我们可以继续将圆替换为中心的一个点,然后将线按半径向新创建的点移动。
如果直线由两个移动点定义a,b速度为va和vb,并且该点位于c速度为 的点vc,我们可以a通过替换其他两点的位置和速度来使点静止(不移动)并位于位置 (0,0)由b-a,vb-va和c-a, vc-va.
现在让我们将b和c的新坐标和速度命名为:[bx, by],(vbx, vby)和[cx, cy], (vcx, vcy)。现在我们可以通过求解这个公式来计算出碰撞时间:
cx+t * vcx = s*bx + s*t*vbx
cy+t * vcy = s*by + s*t*vby
Run Code Online (Sandbox Code Playgroud)
但是要小心:这会导致二次方程,并且您必须忽略可能的负解,这可能意味着该点正在远离直线或碰撞现在正在发生,因此请确保球尚未碰撞在你开始做任何事之前。
另外(我希望没有必要这么说)当你替换t和后s,你将不会得到最终的碰撞点,你需要撤消你所做的所有地役权(a例如添加)
如果您需要它用于非凸多边形,我有一个解决方法,所以请写在评论中。