C4 Engine Terathon Software C4 Engine API Documentation

• World Manager

API Links
Developer Links

World::DetectCollision

Defined in:  C4World.h
Detects a collision between world geometry and a swept sphere.
Prototype

bool DetectCollision(const Point3D& p1, const Point3D& p2, float radius, unsigned long clas, CollisionData *data) const;

Parameters
p1 The beginning of the line segment in world space.
p2 The end of the line segment in world space.
radius The radius of the sphere. This cannot be negative, but it can be zero.
clas The collision class.
data The returned collision data.
Description
The points specified by the parameters p1 and p2, combined with the radius specified by the radius parameter, define a directed swept sphere. The DetectCollision function detects the first collision between this swept sphere and all Geometry nodes possessing collision information. If a collision is detected, then the function returns true; otherwise, it returns false.

The clas parameter can be used to invalidate certain types of collisions. When a candidate geometry is encountered in the collision detection process, its collision exclusion mask is logically ANDed with the value of the clas parameter. A collision can only occur if the result of this operation is zero. The collision mask associated with a geometry can be set using the GeometryObject::SetCollisionExclusionMask function. The collision class can be a combination (through logical OR) of the following predefined values and application-defined values.
kCollisionRigidBody Any type of rigid body.
kCollisionCharacter A rigid body that represents a character.
kCollisionProjectile A rigid body that represents a projectile.
kCollisionVehicle A rigid body that represents a vehicle.
kCollisionCamera A type of camera.
kCollisionSightPath When used in an exclusion mask, does not obstruct sight.
kCollisionSoundPath When used in an exclusion mask, does not obstruct sound.
kCollisionBaseClass First application-defined collision class.
kCollisionExcludeAll When used as a collision exclusion mask, this value prevents collisions with everything.
If a collision occurs, then the CollisionData structure pointed to by the data parameter is filled out with information about the collision. The param field of this data structure represents the fraction of the distance that the sphere traveled from p1 to p2 before the collision occurred.

The DetectCollision function works by intersecting a line segment with the Minkowski sum of a sphere and arbitrary polygon meshes. The algorithm is very precise and can determine when collisions occur with the expanded faces, edges, or vertices of the mesh referenced by a geometry node. If the value of the radius parameter is 0.0, then the collision detection reduces to a ray intersection with faces only.
See Also

CollisionData

World::QueryWorld

GeometryObject::GetCollisionExclusionMask

GeometryObject::SetCollisionExclusionMask