fenomas / noa

Experimental voxel game engine.
MIT License
611 stars 87 forks source link

Raycast to entity meshes #131

Closed zoink closed 4 years ago

zoink commented 4 years ago

My current understanding is that fast-voxel-raycast is used to get the highlighted mesh on tick - what is the recommended way to raycast onto other entities in the ECS (with the mesh component attached)? Thanks!

fenomas commented 4 years ago

Hi, there's no set API for this currently. I have this kind of logic in my own game, but I haven't come up with a nice way to abstract it so it would be generally applicable to all game clients.

The rough outline of how I do it is:

  1. get all entities you want to check with e.g. ents.getStatesList(ents.names.mesh)
  2. do a broadphase test to filter out entities whose AABB doesn't overlap with the ray's AABB
  3. for remaining entities check the distance between the entity and the nearest point on the ray to that entity
  4. If multiple hits, return the one closest to the source of the ray

There could be a better way though.

For the broadphase test, similar logic can be found in the collideEntities component, that demonstrates how to quickly test a bunch of AABBs. Each entity's position component has an _extents array, which is in the format expected by the box-intersect library, so if you make a similar extents array for your ray, that library can quickly find the collisions. Note that the _extents arrays are in local coordinates, so the ray's extents should also be local.