Click or drag to resize
AB4D logo

MeshCollider Class

MeshCollider can be used to test collision between mesh and a 3D position, Rect3D, BoundingBox or another mesh. See remarks for more info.
Inheritance Hierarchy
SystemObject
  Ab3d.DirectX.UtilitiesMeshCollider

Namespace: Ab3d.DirectX.Utilities
Assembly: Ab3d.DXEngine.Wpf (in Ab3d.DXEngine.Wpf.dll) Version: 7.1.9105.2048
Syntax
C#
public class MeshCollider

The MeshCollider type exposes the following members.

Constructors
 NameDescription
Public methodMeshCollider(MeshBase) Constructor with DXEngine's mesh (mesh must implement IRayHitTestedObject interface).
Public methodMeshCollider(MeshGeometry3D) Constructor with WPF's MeshGeometry3D. DXEngine's DXMeshGeometry3D will be generated from WPF mesh.
Top
Properties
 NameDescription
Public propertyLastIntersectionPositionIndex Gets the position index of a position that was the first to be inside the mesh (this is not the closest position). This is set only when calling HasIntersection with mesh as parameter. Default value -1 indicates no position is inside the mesh.
Public propertyMesh Gets the mesh that was used to create this MeshCollider
Top
Methods
 NameDescription
Public methodGetClosestPosition GetClosestPosition returns a DXRayHitTestResult with HitPosition set to the closest position on the mesh, DistanceToRayOrigin is set to the distance from the specified position to the position on the mesh and TriangleIndex is set to the position index in the Positions array (or vertex buffer array).
Public methodHasIntersection(MeshBase) Returns true when the specified mesh intersects with the mesh that was used to create this MeshCollider.
Public methodHasIntersection(BoundingBox, Boolean) Returns true when the specified BoundingBox intersects with the mesh. When checkEachCorner is false, then only a simple bounding box intersection check is done. This is the fastest but the least accurate. When checkEachCorner is true (by default), then the center position and all 8 corners of the BoundingBox are checked for intersection.
Public methodHasIntersection(MeshBase, Matrix) Returns true when the specified mesh intersects with the mesh that was used to create this MeshCollider.
Public methodHasIntersection(MeshGeometry3D, Transform3D) Returns true when the specified MeshGeometry3D intersects with the mesh that was used to create this MeshCollider.
Public methodHasIntersection(Rect3D, Boolean) Returns true when the specified Rect3D intersects with the mesh. When checkEachCorner is false, then only a simple bounding box intersection check is done. This is the fastest but the least accurate. When checkEachCorner is true (by default), then the center position and all 8 corners of the Rect3D are checked for intersection.
Public methodIsInsideMesh(Point3D) Returns true when the specified position is inside the mesh.
Public methodIsInsideMesh(Vector3) Returns true when the specified position is inside the mesh.
Top
Fields
 NameDescription
Public fieldPrimaryRayDirection Direction of the ray that is first used for hit testing. Default value is (0, 1, 0).
Public fieldSecondaryRayDirection Direction of the ray that is used to confirm the hit test results of the first ray. This prevents invalid results in case the ray just touches the mesh. Default value is (0, 0, 1).
Top
Remarks

MeshCollider can be used to test collision between mesh and a 3D position, Rect3D, BoundingBox or another mesh.

MeshCollider uses ray hit testing to determine if a position is inside a mesh. This is done by creating a ray from the specified position and into the direction specified by PrimaryRayDirection (this direction can be any direction). Then a ray hit testing is performed and number of hits is counted.

If number of hits is even (1, 3, 5, etc.) then the position is inside the mesh. If number of hits is odd (0, 2, 4, etc.) then the position is outside the mesh.

When number of hits is even, then the accuracy of the result is confirmed by doing another check with ray in SecondaryRayDirection. This prevents false positive results when the ray just touches the mesh in one position.

When checking if another mesh is intersecting the mesh that was used to create this MeshCollider, then each position in the mesh is checked for intersection. To speed up this process first ten positions that are equally distributed in the mesh are checked. If no intersection is found, then all other positions are checked (duplicate positions are checked only once). The first intersected position stops the check. The index of this position is written to LastIntersectionPositionIndex property.

To make hit testing significantly faster, an OctTree is generated from the mesh that was used to create this MeshCollider.

MeshCollider does not support transforming the mesh that was used to create this MeshCollider. To achieve that you need to transform the mesh by yourself. For WPFs' MeshGeometry3D this can be done by using the Ab3d.Utilities.MeshUtils.TransformMeshGeometry3D method. The DXEngine's mesh can be transformed by transforming each position by Vector3.Transform method and normals by Vector3.TransformNormal method.

See Also