Skip to content

Ray casting against any collider #2400

Answered by eonarheim
mattjennings asked this question in Q&A
Discussion options

You must be logged in to vote

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].p…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@eonarheim
Comment options

@mattjennings
Comment options

mattjennings Jul 7, 2022
Collaborator Author

Answer selected by mattjennings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants