I’m working on a relatively simple kinematic controller, and am unsure of how to prevent the character from penetrating world geometry.
Thus far I’ve come up with three ideas:
1) Use contactTest; for each manifold point produced, create a vector that is the manifold’s normal multiplied by half of its distance value and accumulate those (negatively, as it turns out) to create an offset vector. That done, offset the object by that vector. Something like this:
result = Common.getWorld().bulletWorld.contactTest(self.foot.node())
for contact in result.getContacts():
manifold = contact.getManifoldPoint()
dist = manifold.getDistance()
normal = manifold.getNormalWorldOnB()
offset -= normal*dist*0.5
nodePath.setPos(nodePath.getPos() + offset)
This seems to more or less work, but produces a lot of jittering; my guess is that the offset, in moving the object away from its contacts, may move the it into contact at new points which are only handled on the next frame.
2) Perform a sweep test, respond to the first contact, reduce the delta-time by the time to that contact; repeat until either there are no more contacts, or there is insufficient time (or an iteration counter runs out). In the case that the remaining delta-time is too small, don’t peform that last sweep test.
This should, I think, work rather better than the above… but at this point it feels like I’m re-inventing the wheel, given that I’m doing this with a physics engine that presumably has such calculations running in the background (and likely does a more robust job of it).
3) Use a non-kinematic rigid body (with a constraint preventing it from rotating) for penetration prevention.
The idea here is to make use of the fact that Bullet presumably already handles penetration prevention for non-kinematic bodies. However, it also places the object at the mercy of other arbitrary forces, when all that I really want is for the thing to not intersect other objects. Additionally, I haven’t come up with a way of arranging such a body such that it controls a node above it in the scene graph (as can be done with CollisionHandlerPusher, I believe), nor a way of offsetting the shape relative to its node, and so removing my main reason for wanting to have a node above it: adjusting its position relative to the logical position of the object that it represents. (I want this collider to be responsible for only a portion of the character’s “body”, leaving another, kinematic object to play the part of “feet” and handle movement and floor detection.)
So… Is there another way, or is one of the above correct (or partially so)? Am I missing something?