I have a particular case that I have to solve and I’m not sure on the best way to do it.
Here is an example of what I want to do.
Let’s say I have a big peace of rock running down an hill. It’s a physX rigid dynamic let free (no kinematic flag).
I also have characters running down the same hill, in front of the rock.
What I want to achieve is to make some of my characters fall whenever they are hit by the rock, but depending on some conditions of my game logic.
My first approach.
The first way I did it was setting the characters in kinematic mode (PxRigidBodyFlag::eKINEMATIC flag on the character’s rigid bodies). When there was a collision, I would record it thanks to the “onContact” callback, and on the next step in my game engine, I would remove kinematic flag.
The problem with this approach is that I’m one frame late to remove the flag: when physX detects the collision, it instantaneously reacts with the kinematic object and bound on my character. When my game engine realize there is a collision (after the physX step), I change remove the kinematic flag, but it’s already too late, the wrong reaction already occurs.
I wish I could remove the kinematic flag during the collision callback, but obviously it’s forbidden to change the physics during this part.
The way I’m thinking of doing it right now is as follow.
When a collision occurs between rocks and characters,
- if it’s the first time it occurs, I use the physX collision filter system to record this collision in structures of my own and to force the phsyX engine to NOT solve this contact. Next step in my game engine, I switch my character in dynamic mode if they have to (depending on my game logics).
- if it’s not the first time this collision occurs (I know about this because I recorded the collision when it happened the first time, as written above), then I let physX solve this contact
The downside of this method is that I loose 1 frame:
- when the switch to dynamic should occur
- and also when the switch to dynamic should not occur.
But the good point is that when the character have to switch in dynamic mode, the rock smash him with its full force without the strange reaction it used to have.
Does anybody see a better way of doing what i want to achieve ?
The way I want to do it, I have to use the CPU filter system to track pairs and solve the contact or not depending on the fact that it was already tracked, so I’m afraid I’ll considerably slow down the physX simulation even if I do the minimum and fastest thing I can do with filters.
I’m using physX 3.3.3 from the git repo.