wiserim / phaser-raycaster

Raycasting plugin for Phaser 3. Documentation:

Home Page:https://wiserim.github.io/phaser-raycaster/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enable rays to bounce off (some) objects

mwcz opened this issue · comments

Is your feature request related to a problem? Please describe.

I'm trying out phaser-raycaster and really liking it so far. There is one feature I'd love to see, and that's the ability to have rays bounce off of objects. This would allow a few cool uses of phaser-raycaster that I have in mind. Perhaps there's a way to do this already, but I wasn't able to figure it out.

Describe the solution you'd like

Off the top of my head, the following would be nice:

  • new setting maxBounces, a number defining how many times a ray is allowed to bounce before it's complete. defaults to 1.
  • by default, rays bounce off all objects, unless...
  • shouldBounce, an optional function that when provided, will be called to determine if a given ray's collision with an object shold result in a bounce. This would allow defining only certain GameObjects as bouncable. returns boolean. defaults to undefined.
create() {
  this.raycaster = this.raycasterPlugin.createRaycaster({
    maxBounces: 4,
    shouldBounce: (collisionInfo) => collisionInfo.bodyB.data.get("reflective") == true
  });
}

Describe alternatives you've considered

I tried using a series of setOnCollide callbacks to cast new rays when existing ones collided, but it quickly got complex and difficult to manage, especially due to the asynchronous nature of the callbacks.

Thanks for all the work you've done and for reading this issue!

Sorry for the delay.

I'm planning to implement a method for casting reflective ray when I'll find free time.
For now I've updated Phaser-raycaster to version 0.9.3.
Now additionally to intersection point Ray.cast, Ray.castCircle and Ray.castCone methods return hit mapped game object and hit segment if available.

let intersection = this.ray.cast();
let hitObject = intersection.object;
let hitSegment = intersection.segment; //Phaser.Geom.Line object

Based on this you can calculate ray's reflection using law of reflection.

That's perfect, thank you!