Excalibur icon indicating copy to clipboard operation
Excalibur copied to clipboard

Support arbitrary raycasting into a Scene

Open eonarheim opened this issue 2 years ago • 3 comments

Context

Currently you must have a reference to a collider in order to perform a raycast against it, this quickly becomes slow, unwieldy, or impractical if a scene has many colliders.

Proposal

Add a way off of scene to raycast arbitrarily and get results

Possible sketch

const scene = game.currentScene;
const ray = new ex.Ray(ex.vec(100, 100), ex.Vector.Right);
const maxDistance = 100; // pixels
const hitResults: RayCastResults[] = scene.physicsWorld.rayCast(ray, maxDistance);

// list sorted by distance least to greatest?
console.log(hitResults[0].distance); // distance in pixels
console.log(hitResults[0].collider); // collider that was intersected 
console.log(hitResults[0].point); // world space point that was intersected 

eonarheim avatar Jul 07 '22 02:07 eonarheim

This looks like a good approach to me!

The only other suggestions I might have is some way to filter out by collision groups. I might want to just raycast against a "floor" group, for example. That could either be an option passed to the raycast (the collision group/name), or if there's a way to get all colliders from a collision group I could cross-reference them with the hitResults (though that could involve a lot of iterating).

mattjennings avatar Jul 07 '22 02:07 mattjennings

Good point, maybe this where we can pass an optional option bag with the various configurations?

const floorGroup = ex.CollisionGroupManager.create("floor");
scene.physicWorld.rayCast(ray, {
  maxDistance: 100,
  collisionGroups: [floorGroup]  
})

eonarheim avatar Jul 07 '22 03:07 eonarheim

@eonarheim I like that!

mattjennings avatar Jul 07 '22 15:07 mattjennings

This issue hasn't had any recent activity lately and is being marked as stale automatically.

github-actions[bot] avatar Sep 06 '22 00:09 github-actions[bot]