SphereTriangleCollider
Package: Libraries.Game.Collision.Narrowphase · Inherits from: Libraries.Language.Object, Libraries.Game.Collision.Narrowphase.TriangleCollider3D
SphereTriangleCollider finds the contact between a sphere and one triangle. Because a sphere is round, the contact is always at the point on the triangle closest to the sphere's center, whether that point is on the triangle's face, along an edge or at a corner, so one closest point calculation is all that is needed. This is the same approach Bullet uses for a sphere against a triangle. It reports at most one contact. The triangle is treated as thickened by its margin, the same way the general collision test treats it, so a sphere comes to rest at the same height on either one.
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.Sphere
use Libraries.Game.Collision.Shapes.Triangle
Item3D ground
Item3D ball
LocalCollisionPoints3D points
points:Set(ground, ball)
CollisionPointsResult3D result
result:Initialize(ground, ball)
Triangle triangle
Vector3 a
a:Set(0, 0, 0)
Vector3 b
b:Set(1, 0, 0)
Vector3 c
c:Set(0, 0, -1)
triangle:Set(a, b, c)
PhysicsPosition3D triangleTransform
Sphere sphere
sphere:SetRadius(0.5)
PhysicsPosition3D sphereTransform
sphereTransform:GetOrigin():Set(0.25, 0.5, -0.25)
SphereTriangleCollider collider
collider:Collide(points, triangle, triangleTransform, sphere, sphereTransform, result, false)
// points now holds one contact, with the sphere resting on the triangle.
Actions
Collide(Libraries.Game.Collision.LocalCollisionPoints3D collisionPoints, Libraries.Game.Collision.Shapes.Triangle triangle, Libraries.Game.Collision.PhysicsPosition3D triangleTransform, Libraries.Game.Collision.Shapes.Sphere sphere, Libraries.Game.Collision.PhysicsPosition3D sphereTransform, Libraries.Game.Collision.CollisionPointsResult3D result, boolean sphereIsItemA)
This action tests a sphere against a triangle, and reports a contact 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.sphereLibraries.Game.Collision.Shapes.Sphere: The sphere.sphereTransformLibraries.Game.Collision.PhysicsPosition3D: Where the sphere is in the world. Its origin is the sphere's center.resultLibraries.Game.Collision.CollisionPointsResult3D: Where contacts are reported. It must already be initialized with the collision's two items.sphereIsItemAboolean: False when the triangle is item A, as terrain calls it, and true when the sphere is item A.
CollideLoaded(number radius, Libraries.Compute.Vector3 center)
This action tests a sphere with the given radius and center against the triangle that is already loaded. Begin and LoadTriangle must have been called first.
Parameters
radiusnumber: The sphere's radius.centerLibraries.Compute.Vector3: The sphere's center in world space.
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.Compute.Vector3 a, Libraries.Compute.Vector3 b, Libraries.Compute.Vector3 c, number margin)
- LoadTriangle(Libraries.Game.Collision.Shapes.Triangle triangle, Libraries.Game.Collision.PhysicsPosition3D transform)