Ray casting against any collider #2400
-
Is it possible to ray cast against any collider, or even a collision group? As far as I can tell from the docs it can only be used to check against a single collider, which you must have reference to. If it's not possible, any ideas on how I might approach checking an arbitrary coordinate for any collider? Essentially I just want to see if a spot is "free" for an actor prior to actually colliding with something there. I could get a reference to every collider in the scene and raycast on each, buuut that seems like a bad idea. Another idea is to attach a child actor with a passive collider, move it as needed and listen for collision events. Perhaps that's the right approach? I just thought of that as I was writing this up, so I'll give that a try, but I'm curious to hear anyone's general thoughts on this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This is something I've been thinking about recently, it is pretty inconvenient to need a reference to a collider to raycast against it. Unfortunately there isn't a super convenient way at the moment to raycast into a scene arbitrarily. What do you think of this possible api 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 It is the case In the current version of excalibur you will need a reference to anything you want to raycast against. I'll make an issue for this because it's definitely something we want to support. I haven't tried using a passive child collider before like this, that might work! Do report back if you try this out! Another approach if you are doing something grid based you could keep an alternate grid data structure and maintain whether cells are empty or occupied. The sample-grid uses a similar idea (but uses |
Beta Was this translation helpful? Give feedback.
This is something I've been thinking about recently, it is pretty inconvenient to need a reference to a collider to raycast against it. Unfortunately there isn't a super convenient way at the moment to raycast into a scene arbitrarily.
What do you think of this possible api sketch?