# Bullet kinematic object: prevention of penetration

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?

I seem to have solved this myself; in the end a non-kinematic rigid body seems to work quite well, albeit that it involved applying an additional offset to my kinematic movement in order to keep the non-kinematic body off of the ground. I did encounter a few glitches along the way, but those seem to all be gone–for the moment, at least.

For anyone who might try something similar themselves, I recommend that, on each update of the kinematic body used for moving the controller around, you add the non-kinematic body’s linear velocity to the (presumed) separate velocity vector used for the kinematic body (to counter the kinematic body’s attempt to move into any obstacles), then zero the non-kinematic body’s linear velocity (to prevent accumulation of velocity in the kinematic velocity vector and to prevent the non-kinematic velocity from persisting and moving the body around too much).