1.6 把算法扩展到三维图形情况下
在Internet上与游戏有关的网站上有许多关于先进的三维图形冲突检测算法的描述。我们并不过分强调一定要在冲突检测算法中加上z轴,只需在前面的算法中简单地进行一些扩展即可。
下面的这段代码描述了一个针对类似立方体的对象的邻近性检测:
VB.Net游戏编程入门经典(6)Dim Dx As Single = Math.Abs(R2.X - R1.X)
Dim Dy As Single = Math.Abs(R2.Y - R1.Y)
Dim Dz As Single = Math.Abs(R2.Z - R1.Z)
If Dx > R1.ExtentX + R2.ExtentX AndAlso _
Dy > R1.ExtentY + R2.ExtentY AndAlso _
Dz > R1.ExtentZ + R2.ExtentZ Then
'The boxes do not overlap
Else
'The boxes do overlap
End If
接下来的邻近性检测算法在三维空间里使用球体扩展了二维空间里关于圆形的邻近性检测:
Dx = Math.Abs(Object1.CenterX - Object2.CenterX);
Dy = Math.Abs(Object1.CenterY - Object2.CenterY);
Dz = Math.Abs(Object1.CenterZ - Object2.CenterZ);
double Distance = Math.Sqrt(Dx*Dx + Dy*Dy + Dz*Dz);
if (Distance > Object1.radiusRadius+ Object2.radiusRadius)
VB.Net游戏编程入门经典(6)// => The circles do not overlap.
else
// => The circles are overlapping.
上面的邻近性检测算法可用于检测球体/立方体的重叠情况,此时,您可能已经理解了如何扩展这些简单的重叠性检测算法。在Arvo算法中,只需要简单增加针对z轴的检测即可。
…
' Check z axis. If Circle is outside box limits, add to distance.
If CircleCenterZ < Me.MinZ Then
Dist += Math.Sqr(CircleCenterZ - Me.MinZ)
Else
If CircleCenterZ > Me.MaxZ Then
Dist += Math.Sqr(CircleCenterZ - Me.MaxZ)
End If
End If
' Now that distances along X, Y, and z axis are added, check if the square
' of the Circle's radius is longer and return the boolean result.
Return Radius * Radius < dist//
VB.Net游戏编程入门经典(6)在接下来的章节中,您将了解到如何把这些理论思想应用到实际的游戏项目开发中。