Collision Mesh - Raycast Not Hitting

I’m generating some procedural meshes based on a collision layer in my tilemap. The mesh seems to be generated correctly (as far as I can tell), but my raycasts won’t hit it. If I attach a ‘collisionstart’ event to the player object, it definitely hits the mesh. Its just the raycast that’s failing.

The relevant raycast code is in scripts/player.js and the mesh is being generated in scripts/LDtk.js in the parseCollisionLayer anonymous function.

Editor link: https://playcanvas.com/editor/scene/1219365

Can’t tell what could be causing an issue at a glance.

Given it’s a 2D game, maybe it’s due the ray being in the wrong Z position and it’s in front/behind the map?

I wondered about that too, but unless there’s something weird going on with how the entities are parented/layered, everything should have a z value of 0.

Might be worth doing some manual raycasts where you know for sure it’s going to hit

(On a side note, I really wish I have the time to port a decent tilemap 2D physics to that LDtk project]

2 Likes

There’s definitely something weird going on, I tried manually casting a ray from a high z to a low z value directly “through” the mesh and its still not hitting. I may just have to bite the bullet and use a 3D camera controller script to view the mesh from all sides once its been generated.

Maybe have the raycast at a 90 degree angle to the map, and have another entity to spawn the raycast at a certain point, maybe a point that follows player?

Might be worth rendering the mesh as a model. Maybe there’s a hole in the side and the raycast is going between two sides of the model?

What’s the easiest way to display a mesh like this as a model? I’m still pretty new to procedural mesh generation as a whole.

Is a it a 2d mesh being made? And is it an infinitely generated map?

And maybe the raycast won’t hit the mesh unless the mesh has a collision and rigid body

Yes its a 2D mesh, all of the Z values are the same. And no the map regions will be finite sizes.

1 Like

If it’s just a 2D mesh, that might be why the raycast isn’t hitting as it’s always going to be 90 degrees to the polys.

It’s like hitting the side of a paper with a needle.

Sounds like a 2D physics library would be better here? Might want to try p2.js? https://github.com/schteppe/p2.js/

We have a partial integration here https://playcanvas.com/project/446127/overview/p2js-integration

Alternatively, use what you have got and give the generated meshes some depth so it’s not just thin poly.

Add a model component with the same scale as your collision component. You can also make your raycast visible with renderLine.

https://developer.playcanvas.com/en/api/pc.Application.html#renderLine

Switching to p2 is giving me much better results! I still need to tinker with some things to get it all working properly, but I think that may be the solution for me. :slight_smile: Thanks for suggesting that, it didn’t even occur to me to just use a non-3D library… :man_facepalming:

1 Like