Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support arbitrary raycasting into a Scene #2401

Closed
eonarheim opened this issue Jul 7, 2022 · 4 comments · Fixed by #2410
Closed

Support arbitrary raycasting into a Scene #2401

eonarheim opened this issue Jul 7, 2022 · 4 comments · Fixed by #2410
Labels
stale This issue or PR has not had any activity recently

Comments

@eonarheim
Copy link
Member

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 
@mattjennings
Copy link
Contributor

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).

@eonarheim
Copy link
Member Author

eonarheim commented Jul 7, 2022

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]  
})

@mattjennings
Copy link
Contributor

@eonarheim I like that!

eonarheim added a commit that referenced this issue Jul 12, 2022
TODO:
* [ ] TileMaps aren't working yet
* [ ] Tests
@github-actions
Copy link

github-actions bot commented Sep 6, 2022

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

@github-actions github-actions bot added the stale This issue or PR has not had any activity recently label Sep 6, 2022
eonarheim added a commit that referenced this issue Sep 11, 2022
Closes #2401

This PR implements an arbitrary raycasting api in Excalibur

```typescript
const hits = engine.currentScene.physics.rayCast(new ex.Ray(player.pos, ex.Vector.Down), {
  maxDistance: 100,
  collisionGroup: blockGroup,
  searchAllColliders: false
});
```

<img width="797" alt="image" src="https://user-images.githubusercontent.com/612071/178625499-d9f97052-5b18-49fe-9440-89bd93e46ff6.png">

## Changes:

- Fix issue where TileMaps weren't correctly added to the collider data structure
- Add physics world with raycasting endpoint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale This issue or PR has not had any activity recently
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants