BoxTriangleCollider
Package: Libraries.Game.Collision.Narrowphase · Inherits from: Libraries.Language.Object, Libraries.Game.Collision.Narrowphase.TriangleCollider3D
BoxTriangleCollider finds the contacts between a box and one triangle. It works in the box's own space, where the box is centered on the origin and lines up with the axes, so the triangle's corners are moved into that space first. It first looks for a direction that separates the two, using the separating axis test. A box and a triangle can only be separated along one of 13 directions: the triangle's normal, the box's three axes, or one of the box's axes crossed with one of the triangle's three edges. If any of these shows a gap wider than the contact threshold, there is no contact. Otherwise, the direction with the smallest overlap is the one to push the two apart along, and it decides how the contacts are built: The triangle's normal: the box face that points most toward the triangle is cut down to the area over the triangle, and each of its corners that reaches the triangle becomes a contact. This is the usual case for a box resting on terrain, and gives up to four contacts. One of the box's axes: the triangle is cut down to the area of the box face, and each of its corners that reaches the box becomes a contact. This is the case for a triangle smaller than the box face, or a box resting on a triangle's corner. An edge pair: one box edge crosses one triangle edge, and the closest points between the two edges give a single contact. Edge directions are only chosen if they are clearly better than a face direction, since face contacts are steadier for a box at rest. This is the same preference BoxBoxCollider has. The triangle is thickened by its margin, as with the general collision test. The box is its full size, its half extents plus its margin, as in BoxBoxCollider. Its corners and edges are sharp, while the general test rounds them by the box's margin, so the two can differ slightly when a contact is at a corner or edge.
Example Code
use Libraries.Compute.Vector3
use Libraries.Interface.Item3D
use Libraries.Game.Collision.PhysicsPosition3D
use Libraries.Game.Collision.LocalCollisionPoints3D
use Libraries.Game.Collision.CollisionPointsResult3D
use Libraries.Game.Collision.Shapes.Box
use Libraries.Game.Collision.Shapes.Triangle
Item3D ground
Item3D crate
LocalCollisionPoints3D points
points:Set(ground, crate)
CollisionPointsResult3D result
result:Initialize(ground, crate)
Triangle triangle
Vector3 a
a:Set(-5, 0, 5)
Vector3 b
b:Set(5, 0, 5)
Vector3 c
c:Set(0, 0, -5)
triangle:Set(a, b, c)
PhysicsPosition3D triangleTransform
Box box
box:Set(0.5, 0.5, 0.5)
PhysicsPosition3D boxTransform
boxTransform:GetOrigin():Set(0, 0.28, 0)
BoxTriangleCollider collider
collider:Collide(points, triangle, triangleTransform, box, boxTransform, result, false)
// points now holds four contacts, one under each corner of the box.
Actions
Collide(Libraries.Game.Collision.LocalCollisionPoints3D collisionPoints, Libraries.Game.Collision.Shapes.Triangle triangle, Libraries.Game.Collision.PhysicsPosition3D triangleTransform, Libraries.Game.Collision.Shapes.Box box, Libraries.Game.Collision.PhysicsPosition3D transform, Libraries.Game.Collision.CollisionPointsResult3D result, boolean boxIsItemA)
This action tests a box against a triangle, and reports up to four contacts if they are within the collision's contact threshold. It does not refresh the collision's points, so it can be called for many triangles in a row.
Parameters
collisionPointsLibraries.Game.Collision.LocalCollisionPoints3D: The collision's stored contact points.triangleLibraries.Game.Collision.Shapes.Triangle: The triangle, with its corners in its own space.triangleTransformLibraries.Game.Collision.PhysicsPosition3D: Where the triangle is in the world.boxLibraries.Game.Collision.Shapes.Box: The box.transformLibraries.Game.Collision.PhysicsPosition3D: Where the box is in the world. Its origin is the box's center.resultLibraries.Game.Collision.CollisionPointsResult3D: Where contacts are reported. It must already be initialized with the collision's two items.boxIsItemAboolean: False when the triangle is item A, as terrain calls it, and true when the box is item A.
CollideLoaded(Libraries.Game.Collision.Shapes.Box box, Libraries.Game.Collision.PhysicsPosition3D transform)
This action tests a box against the triangle that is already loaded. Begin and LoadTriangle must have been called first.
Parameters
boxLibraries.Game.Collision.Shapes.Box: The box.transformLibraries.Game.Collision.PhysicsPosition3D: Where the box is in the world.
PrepareBox(Libraries.Game.Collision.Shapes.Box box)
This action reads a box's size once, before it is tested against many triangles in a row. Until ReleaseBox is called, CollideLoaded uses the size read here for this box, so the box must not change size in between.
Parameters
boxLibraries.Game.Collision.Shapes.Box: The box about to be tested.
ReleaseBox()
This action ends a run of triangles started by PrepareBox.
Inherited Actions
9 actions from Libraries.Game.Collision.Narrowphase.TriangleCollider3D
- AddContact(Libraries.Compute.Vector3 normalTowardShape, Libraries.Compute.Vector3 pointOnShape, number separation)
- Begin(Libraries.Game.Collision.LocalCollisionPoints3D collisionPoints, Libraries.Game.Collision.CollisionPointsResult3D result, boolean shapeIsItemA)
- GetClipper()
- GetContactThreshold()
- GetGeometry()
- GetTriangleMargin()
- IsSwapped()
- LoadTriangle(Libraries.Game.Collision.Shapes.Triangle triangle, Libraries.Game.Collision.PhysicsPosition3D transform)
- LoadTriangle(Libraries.Compute.Vector3 a, Libraries.Compute.Vector3 b, Libraries.Compute.Vector3 c, number margin)