I have a kinematic character object (box shape) that gets created with the following settings:
Code: Select all
// ...
_rigidBody.ActivationState = ActivationState.ActiveTag | ActivationState.DisableDeactivation;
_rigidBody.CollisionFlags = CollisionFlags.CharacterObject | CollisionFlags.KinematicObject;
// create ghost object for it:
_ghostObject = new GhostObject();
_ghostObject.CollisionShape = _rigidBody.CollisionShape;
_ghostObject.WorldTransform = _rigidBody.WorldTransform;
I move my object around with this method:
Code: Select all
public void MoveKinematic(float x, float y, float z)
{
BulletSharp.Math.Matrix newTransform = _rigidBody.MotionState.WorldTransform;
newTransform.Origin += new BulletSharp.Math.Vector3(x, y, z);
_rigidBody.MotionState.WorldTransform = newTransform;
// update the ghost's position as well:
if (HasGhostObject)
_ghostObject.WorldTransform = newTransform;
}
I use the ghost object to detect collisions with other objects:
Code: Select all
// the ghost always collides with the kinematic object
//(since they share the same shape and world transform).
// thus, count must be > 1 whenever the ghost collides with an object other than the player object:
if (_ghostObject.OverlappingPairs.Count > 1)
{
AlignedCollisionObjectArray collisions = GetGhostObject().OverlappingPairs;
foreach(CollisionObject b in collisions)
{
b.Activate(true);
// TODO: Undo the collision by moving the player object back by a bit
}
}
What I would like my kinematic player object to behave like is:
- Check for collisions (ok, works)
- Check by how much it is "inside" another object (minimum translation vector in order to undo the collision)
- Move itself out of the colliding object