I’d like to create a simple laser pointer test with Matter.js and Phaser 3 although I’d like for the laser beam to interact with the Matter.js tilemap as well.
Essentially the laser beam starts in the center of the room and is pointed toward the mouse.
To do this I used the Phaser 3 line game object and it all worked well until I introduced the Matter.js tilemap.
When the laser hits the tilemap it just goes straight through instead of stopping at the tile
I don’t really know where to start on this, I looked into raycasting but I didn’t understand it very well.
Adding a line game object will not by default have a matter body, you will have to make a matter body for it and then assign that body to the line. You can then use matter collision callbacks to check if the line should only be drawn to a tile.
You can also use a raycast, you will not have to add a body to the line to use this. To raycast in the matter world, you use Phaser.Physics.Matter.Matter.Query.ray(allTheTileBodies, centerOfTheRoom, mousePosition, widthOfRayInPixels); This gives you an array of bodies that you can iterate and find the closest body, use that to stop drawing the laserpointer line. “allTheTileBodies” are part of the bodies in the current scene can be found in matter.world.localWorld.bodies.
Sorry for not giving code examples, can’t do it right now. Check https://labs.phaser.io/ there might be some matter/tile examples that can be useful.