ContactClipper3D
Package: Libraries.Game.Collision.Narrowphase · Inherits from: Libraries.Language.Object
ContactClipper3D holds the polygon work that turns two touching shapes into a small set of contact points. It can cut a polygon by a plane, cut a polygon to the area over a triangle, and choose up to four points from a larger set. The colliders use it like this. A face of one shape is written into an array of points, cut down to the part that lies over the other shape, and the points that are close enough to touch are kept. If more than four are left, ReduceToFour picks the four that best describe the contact, because a collision stores at most four points. Every array a caller passes in must have room for MAXIMUM_POINTS vectors. CreatePointArray makes one that does. Cutting a convex polygon by a plane adds at most one point, so a four sided face cut by three planes has at most seven points, and an eight sided one at most eleven, both well under the limit.
Example Code
use Libraries.Compute.Vector3
use Libraries.Containers.Array
ContactClipper3D clipper
Array<Vector3> square = clipper:CreatePointArray()
Array<Vector3> clipped = clipper:CreatePointArray()
square:Get(0):Set(-1, 0, -1)
square:Get(1):Set(1, 0, -1)
square:Get(2):Set(1, 0, 1)
square:Get(3):Set(-1, 0, 1)
// Keep the part where x is at least 0.
Vector3 planeNormal
planeNormal:Set(1, 0, 0)
integer count = clipper:ClipAgainstPlane(square, 4, clipped, planeNormal, 0)
// count is 4: (0, 0, -1), (1, 0, -1), (1, 0, 1), (0, 0, 1), in some order.
Variables
integer TARGET_POINTS: Intermediate results while cutting by several planes.integer MAXIMUM_POINTS
Actions
ClipAgainstPlane(Libraries.Containers.Array<Libraries.Compute.Vector3> source, integer sourceCount, Libraries.Containers.Array<Libraries.Compute.Vector3> destination, Libraries.Compute.Vector3 planeNormal, number planeOffset)
This action cuts a polygon by a plane, keeping the part where planeNormal · point is at least planeOffset, which is the side the normal points to. The first sourceCount points of source are the polygon's corners in order around it. The kept polygon is written to the start of destination, and the number of its corners is returned. One point is kept or dropped. Two points are treated as a line segment, and are cut to the kept part of it. Three or more are treated as a closed polygon. source and destination must be different arrays.
Parameters
sourceLibraries.Containers.Array<Libraries.Compute.Vector3>: The polygon to cut.sourceCountinteger: How many points of source to use.destinationLibraries.Containers.Array<Libraries.Compute.Vector3>: Where the kept polygon is written.planeNormalLibraries.Compute.Vector3: The plane's normal, pointing toward the side that is kept.planeOffsetnumber: The plane's distance from the origin along the normal.
Returns
integer: the number of points written to destination.
ClipToTriangle(Libraries.Containers.Array<Libraries.Compute.Vector3> source, integer sourceCount, Libraries.Containers.Array<Libraries.Compute.Vector3> destination, Libraries.Game.Collision.Narrowphase.TriangleGeometry3D triangle, number expand)
This action cuts a polygon down to the part that lies over a triangle, when seen along the triangle's normal, by cutting it with the triangle's three side planes. Each side plane is moved outward by expand, so the area kept is the triangle grown by expand on every side. This is how a triangle's margin is included. The first sourceCount points of source are the polygon's corners in order around it. The result is written to the start of destination, and the number of its corners is returned. A degenerate triangle has no side planes, so nothing is kept. source and destination must be different arrays, and neither may be one of this clipper's own buffers.
Parameters
sourceLibraries.Containers.Array<Libraries.Compute.Vector3>sourceCountintegerdestinationLibraries.Containers.Array<Libraries.Compute.Vector3>triangleLibraries.Game.Collision.Narrowphase.TriangleGeometry3Dexpandnumber
Returns
integer
CreatePointArray()
This action returns a new array holding MAXIMUM_POINTS vectors, ready to be used as a source or destination by the other actions.
Returns
ReduceToFour(Libraries.Containers.Array<Libraries.Compute.Vector3> points, Libraries.Containers.Number32BitArray depths, integer count, Libraries.Compute.Vector3 normal, Libraries.Containers.Integer32BitArray chosen)
This action chooses at most four of a set of contact points, and writes their indexes to the start of chosen. If there are four or fewer, all of them are chosen, in order. Otherwise the deepest point is chosen first, and the other three are the points closest to evenly spaced directions around the middle of the set, starting from the deepest point. This is the method Bullet calls cullPoints2, and the one BoxBoxCollider uses. The points should be the corners of a polygon in order around it, which is what the clipping actions produce, and they should lie roughly in a plane at a right angle to normal.
Parameters
pointsLibraries.Containers.Array<Libraries.Compute.Vector3>: The contact points.depthsLibraries.Containers.Number32BitArray: How deep each point is. Lower values are deeper, and negative values overlap.countinteger: How many points there are.normalLibraries.Compute.Vector3: The direction the points are seen from.chosenLibraries.Containers.Integer32BitArray: Where the chosen indexes are written. It needs room for four values.
Returns
integer: the number of points chosen.