TriangleGeometry3D
Package: Libraries.Game.Collision.Narrowphase · Inherits from: Libraries.Language.Object
TriangleGeometry3D holds one triangle in world space, along with the values that the triangle colliders need over and over: its edges, its normal, the plane it lies in, and the three planes that stand up along its edges. A collider loads a triangle once, then asks for these values as many times as it needs. Every vector here is created once and reused, so loading a new triangle creates no objects. The corners are numbered 0, 1 and 2. Edge 0 runs from corner 0 to corner 1, edge 1 from corner 1 to corner 2, and edge 2 from corner 2 back to corner 0. The normal follows the winding of the corners, so it points toward a viewer who sees the corners in counterclockwise order. Each edge also has a side plane, which contains the edge and stands straight up from the triangle. Its normal lies in the triangle's plane and points into the triangle. A point is over the triangle, when seen along the normal, when it is on the inner side of all three side planes.
Example Code
use Libraries.Compute.Vector3
TriangleGeometry3D geometry
Vector3 a
a:Set(0, 0, 0)
Vector3 b
b:Set(1, 0, 0)
Vector3 c
c:Set(0, 0, -1)
geometry:Load(a, b, c)
Vector3 point
point:Set(0.25, 2, -0.25)
Vector3 closest
integer region = geometry:ClosestPoint(point, closest)
// closest is (0.25, 0, -0.25), and region is geometry:FACE.
Variables
integer EDGE_2integer CORNER_0integer CORNER_2: this is treated as having no area. This is a squared length.integer CORNER_1integer FACE: from corner i to corner i + 1, and corner i is corner i.integer EDGE_1integer EDGE_0number DEGENERATE_AREA: this is treated as having no area. This is a squared length.
Actions
ClosestPoint(Libraries.Compute.Vector3 point, Libraries.Compute.Vector3 closest)
This action finds the point on the triangle closest to the given point, stores it in closest, and returns which part of the triangle it is on: FACE, EDGE_0, EDGE_1, EDGE_2, CORNER_0, CORNER_1 or CORNER_2. A point exactly on the line between two parts may be reported as either one. The method is the one in Christer Ericson's Real-Time Collision Detection, section 5.1.5, which is also what ConvexConvexSimplexSolver uses. For a degenerate triangle, the closest point on its three edges is used instead.
Parameters
pointLibraries.Compute.Vector3: The point to measure from.closestLibraries.Compute.Vector3: Where the closest point on the triangle is stored.
Returns
integer: which part of the triangle the closest point is on.
ClosestPointOnSegment(Libraries.Compute.Vector3 point, Libraries.Compute.Vector3 start, Libraries.Compute.Vector3 finish, Libraries.Compute.Vector3 closest)
This action finds the point on the segment from start to finish that is closest to the given point, stores it in closest, and returns how far along the segment it is, from 0 at start to 1 at finish.
Parameters
pointLibraries.Compute.Vector3startLibraries.Compute.Vector3finishLibraries.Compute.Vector3closestLibraries.Compute.Vector3
Returns
number
ClosestPointsOnSegments(Libraries.Compute.Vector3 start1, Libraries.Compute.Vector3 finish1, Libraries.Compute.Vector3 start2, Libraries.Compute.Vector3 finish2, Libraries.Compute.Vector3 closest1, Libraries.Compute.Vector3 closest2)
This action finds the closest pair of points between the segment from start1 to finish1 and the segment from start2 to finish2. The points are stored in closest1 and closest2, and the squared distance between them is returned. Parallel segments give one of the closest pairs. The method is the one in Christer Ericson's Real-Time Collision Detection, section 5.1.9.
Parameters
start1Libraries.Compute.Vector3finish1Libraries.Compute.Vector3start2Libraries.Compute.Vector3finish2Libraries.Compute.Vector3closest1Libraries.Compute.Vector3closest2Libraries.Compute.Vector3
Returns
number
GetCorner(integer index)
This action returns the corner at the given index, 0, 1 or 2, in world space. This is the vector stored here, not a copy.
Parameters
indexinteger
Returns
GetEdge(integer index)
This action returns the edge at the given index, 0, 1 or 2. Edge i is the vector from corner i to the next corner. This is the vector stored here, not a copy.
Parameters
indexinteger
Returns
GetNormal()
This action returns the triangle's unit normal. This is the vector stored here, not a copy.
Returns
GetPlaneOffset()
This action returns how far the triangle's plane is from the origin along the normal. A point p is on the plane when normal · p equals this value.
Returns
number
GetProjectionMaximum(Libraries.Compute.Vector3 axis)
This action returns the far end of the triangle's projection onto the given axis, which is the farthest the triangle reaches in the direction of the axis. Each corner is projected onto the axis by taking axis · corner, and the largest of the three values is returned. GetProjectionMinimum returns the smallest. One way to picture it is a line through the origin pointing in the axis's direction. Each corner casts a shadow onto that line, at a position equal to its dot product with the axis. The minimum and maximum are the two ends of the stretch of the line that the triangle's shadow covers. This is the basic step of the separating axis test. For a candidate axis, the triangle's stretch, from GetProjectionMinimum to GetProjectionMaximum, is compared with the other shape's stretch along the same axis. If the two do not overlap, by more than the margin and contact threshold, the shapes cannot be touching. The values are distances only when the axis is unit length. For an axis of any other length, they are scaled by that length. That is still fine for comparing against another shape projected onto the same axis, but they are not true distances. For example, for the triangle with corners (0, 0, 0), (1, 0, 0) and (0, 0, -1), the stretch along (1, 0, 0) is 0 to 1, along (0, 0, 1) it is -1 to 0, and along its normal, (0, 1, 0), both ends are 0, because the triangle is flat.
Parameters
axisLibraries.Compute.Vector3: The direction to measure along.
Returns
number: the largest value of axis · corner over the three corners.
GetProjectionMinimum(Libraries.Compute.Vector3 axis)
This action returns the smallest value of axis · corner over the three corners, which is the near end of the stretch of the axis that the triangle covers. See GetProjectionMaximum for what the projection is and how the two are used together.
Parameters
axisLibraries.Compute.Vector3: The direction to measure along.
Returns
number: the smallest value of axis · corner over the three corners.
GetSideNormal(integer index)
This action returns the inward normal of the side plane along the edge at the given index. This is the vector stored here, not a copy.
Parameters
indexinteger
Returns
GetSideOffset(integer index)
This action returns the offset of the side plane along the edge at the given index. A point p is on the inner side of that plane when the side normal · p is at least this value.
Parameters
indexinteger
Returns
number
IsDegenerate()
This action returns true if the triangle has no area, because its corners are all on one line or in one place. A degenerate triangle has no normal or side planes, so only ClosestPoint and the segment actions work with it.
Returns
boolean
Load(Libraries.Compute.Vector3 a, Libraries.Compute.Vector3 b, Libraries.Compute.Vector3 c)
This action loads a triangle whose corners are already in world space. The values are copied, so the vectors may be reused by the caller.
Parameters
Load(Libraries.Game.Collision.Shapes.Triangle triangle, Libraries.Game.Collision.PhysicsPosition3D transform)
This action loads a triangle whose corners are given in the triangle's own space, and moves the corners into the world using the given transform. The triangle itself is not changed.
Parameters
triangleLibraries.Game.Collision.Shapes.Triangle: The triangle to load.transformLibraries.Game.Collision.PhysicsPosition3D: Where the triangle is in the world.
PlaneDistance(Libraries.Compute.Vector3 point)
This action returns how far a point is from the triangle's plane, measured along the normal. It is positive on the side the normal points to.
Parameters
Returns
number
SideDistance(integer index, Libraries.Compute.Vector3 point)
This action returns how far a point is inside the side plane at the given index. It is positive on the inner side, the side the triangle is on.
Parameters
indexintegerpointLibraries.Compute.Vector3
Returns
number