I wanted to Raycast against only certain layers. (For example, raycast against the chessboard, ignoring the chess-pieces on the chess board, so I know where the player is touching the chessboard itself).
A forum search shows up a couple of threads:
And the API suggests the raycaster only returns the first thing it hits, with no apparent means of ignoring layers and so forth.
Thank you for the reply. As user @devMidgard said in his reply, I would petition for this to be added to PlayCanvas’ feature backlog. Raycast layers are extremely useful - essential almost - for Game development.
Probably not the answer that you want, but you could set up seperate duplicate scenes for raycasting/collision detection respectively if needed under different contexts. (Which can be done, if using Playcanvas with it’s AmmoJS, by duplicating entities and adding any needed missing collision+physics components to the duplicates ), and turning off/on the entities used for raycasting under certain contexts. For me, however, i generally integrated playcanvas’ scene graph to link to my own personal scene graph CollisionBoundNode raycasting/collision framework that isn’t tied directly to pc.Entity to begin with to avoid having to load ammo.js just to do raycasting but run my own scene graph optimized for handling collisions (eg. bounding boxes are stored hierachically, mesh collision geometry stored as simple vertices/indices or BVH of triangles if detected mesh triangle count exceeds set limit). Playcanvas appears to make an architectural choice not to include default raycasting solution into it’s game rendering scene graph, because a game rendering scene graph was never optimized/meant for handling raycasting anyway and might often end up iterating over “empty” redudnant entities used for your game, which isn’t good.
Perhaps, manually switching off/on the collision/phsyics components could be done before raycasting? I’m not too sure.
If you only simply need to raycast against the chessboard, however, you simply write a basic ray plane intersection test standalone (the “Plane” is the chessboard) without any physics/scene graph overhead (like in the link shown above), get the point coordinate and divide by (4 or 8…depending on how you arrange the chessboard) for x and z respectively to find out what square it’s located on. You can refer to how such a common calculation is done like so: https://github.com/Mugen87/yuka/blob/master/src/math/Ray.js#L275 .
Or if you are not a purist, and the chessboard assumably is orthogonal to world, you can just do a simple raycast vs a thin box that represents the chessboard without using AmmoJS or any other collision framework. https://playcanvas.com/editor/code/457922?tabs=6649438
COme to think of it, I omitted multi hit results/filtering for my collision engine to begin with (partly because i optimized for nearest-hit early out exit case especially in cases with Quadtree/BSP/KD trees where it always recurses in to closer nodes first). To work around this, you perform a second raycast after the first hit. If you noticed, pc.RaycastResult has the “entity” info. So you can check what layer it’s on, and if it didn’t “succeed”, raycast again from that point along the same direction… https://developer.playcanvas.com/en/api/pc.RaycastResult.html