Ray world intersections
Using the IPhysicalWorld::RayWorldIntersection
function, we can cast a ray from one point of our world to another to detect distance to specific objects, surface types, normal of ground, and more.
RayWorldIntersection
is pretty easy to use, and we can prove it! To start off, see the following example of a ray cast:
ray_hit hit; Vec3 origin = pEntity->GetWorldPos(); Vec3 dir = Vec3(0, 0, -1); int numHits = gEnv->pPhysicalWorld->RayWorldIntersection(origin, dir, ent_static | ent_terrain, rwi_stop_at_pierceable | rwi_colltype_any, &hit, 1); if(numHits > 0) { // Hit something! }
The ray_hit struct
A reference to our ray_hit hit
variable is passed to RayWorldIntersection
, and is where we'll be able to retrieve all of the information about the ray hit.
Commonly used member variables
- float dist: This is the distance from the origin (in our case the position of our entity) to the place where the ray hit.
- IPhysicalEntity *pCollider: This...