Raycasting stops recognizing items

Has anyone noticed, when using raycasting to detect entities that are clicked on, when entities are repeatedly enabled and disabled that they are sometimes no longer recognized by raycasting even though they are re-enabled?

It acts as if collisions have been disabled on the object, but entity.collision.enabled is coming back true.

Thanks.

Hi @rdavid,

I haven’t done any experiment like this, but I think each time you disable an entity it gets removed from the physics simulation and its Ammo.js shape destroyed. When it’s enabled a new shape is being instantiated and added to the simulation.

Most likely if you repeatably do this there may be a sync issue with the simulation trying to keep up with the correct positions.

If you can provide a sample project we can try and debug this to find out if it’s a bug or something else.

OK. I’ll share a scene in a little while. It’s a little complex to test at this point because I didn’t make UI buttons yet.

If I wait before trying the raycasting it doesn’t start to work after a pause.

Is there anything I can try to call to re-initialize the entity/ collisions when I re-enable the object?

I would need to check the system update order. It’s possible that a collision component could be enabled but the next collision system update isn’t till the next frame where the shapes are added to the physics world.

An example would really help us narrow it down or at least some idea of what is done logic wise in the app.

Sure. Here is a scene https://playcanvas.com/project/694942/overview/frogdissection_share
Are you able to access it from this link? It’s a frog dissection simulation.

After launching the scene you can call some functions in the console to make things happen.
window.elPlayCanvas.setStart1(); sets the camera to a convenient position.
window.elPlayCanvas.enableFemale(); turns on or switches to a female frog
window.elPlayCanvas.enableMale(); turns on or switches to a male frog
window.elPlayCanvas.showScalpel(); enables a scalpel that is used to open the frog skin
elPlayCanvas.toggleMouse(“pickup”); enables forceps that can be used to open the skin, muscles and pick up internal organs

You are supposed to first use the scalpel to open the skin, then use toggleMouse(pickup) to click on the skin and muscles to open them before removing organs.

We’re using raycasting to detect which side of the frog and which item (skin or muscles) is clicked on to determine which animation state should be changed.

The problem is, if you toggle between male and female frogs a few time before opening the skin and muscles completely, raycasting stops recognizing that the skin and muscle colliders are being clicked on.

Thank for your help. Let me know if you need any further explanation on the game. Sorry it’s in the middle of development and still a bit messy.

As it’s quite a large project and I don’t know if I get a chance to look at it anytime soon, it be worth trying out Will’s physics debug renderer to see what shapes have been enabled.

It’s not perfect but may be able to help: https://github.com/playcanvas/engine/blob/master/examples/assets/scripts/debug/debug-physics.js

Thanks for the script. I made an interesting observation even though I’m still not exactly sure what is going on.

I used the script and noticed that it doesn’t label the collisions objects if they are mesh, only the other types. So I changed the collision objects to boxes for the skin and muscles in the male frog and that seems to have solved this issues I was having for both the male and female frog.

I have to do a little more testing, but I think I can use the box meshes for what I need.

I do need mesh collision objects sometimes, so is the issue arising from something about using the same collision mesh json file on two different entities? I don’t seem to have this issue with other entities. Should I avoid that for other entities anyway? Thanks for your help!

Shouldn’t be as long as they are both static