Ejemplo n.º 1
0
        /// <summary>
        /// Initialize physical-engine related stuff and set callbacks to respond to contact start / ended / processed events.
        /// </summary>
        public static void Initialize()
        {
            // set collision start callback
            BulletSharp.ManifoldPoint.ContactAdded += (
                BulletSharp.ManifoldPoint cp,
                BulletSharp.CollisionObjectWrapper obj0,
                int partId0,
                int index0,
                BulletSharp.CollisionObjectWrapper obj1,
                int partId1,
                int index1) =>
            {
                // get physical bodies
                BasicPhysicalBody body0 = obj0.CollisionObject.UserObject as BasicPhysicalBody;
                BasicPhysicalBody body1 = obj1.CollisionObject.UserObject as BasicPhysicalBody;

                // if one of the bodies don't support collision skip
                if (body0 == null || body1 == null)
                {
                    return;
                }

                // store both bodies for the collision end event
                cp.UserPersistentData = new CollisionPersistData(body0, body1);

                // send collision events
                CollisionData data = new CollisionData(ToMonoGame.Vector(cp.PositionWorldOnA));
                body0.CallCollisionStart(body1, ref data);
                body1.CallCollisionStart(body0, ref data);
            };

            // set while-collising callback
            BulletSharp.PersistentManifold.ContactProcessed += (
                BulletSharp.ManifoldPoint cp,
                BulletSharp.CollisionObject body0,
                BulletSharp.CollisionObject body1) =>
            {
                if (cp.UserPersistentData == null)
                {
                    return;
                }
                CollisionPersistData data = (CollisionPersistData)cp.UserPersistentData;
                data.Body0.CallCollisionProcess(data.Body1);
                data.Body1.CallCollisionProcess(data.Body0);
            };

            // set collising-ended callback
            BulletSharp.PersistentManifold.ContactDestroyed += (object userPersistantData) =>
            {
                if (userPersistantData == null)
                {
                    return;
                }
                CollisionPersistData data = (CollisionPersistData)userPersistantData;
                data.Body0.CallCollisionEnd(data.Body1);
                data.Body1.CallCollisionEnd(data.Body0);
            };
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Remove a physical body from the world.
 /// </summary>
 /// <param name="body"></param>
 public void RemoveBody(BasicPhysicalBody body)
 {
     // this might happen after the world was destroyed, hence the _world != null test.
     if (_world != null)
     {
         body.RemoveSelfFromBulletWorld(_world);
     }
     body._world = null;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Update single object's aabb.
 /// </summary>
 /// <param name="body">Body to update.</param>
 public void UpdateSingleAabb(BasicPhysicalBody body)
 {
     _world.UpdateSingleAabb(body._BulletEntity);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Add a physical body to the world
 /// </summary>
 /// <param name="body">Physics entity to add.</param>
 public void AddBody(BasicPhysicalBody body)
 {
     Utils.CountAndAlert.Count(Utils.CountAndAlert.PredefAlertTypes.AddedOrCreated);
     body.AddSelfToBulletWorld(_world);
     body._world = this;
 }
Ejemplo n.º 5
0
 public CollisionPersistData(BasicPhysicalBody body0, BasicPhysicalBody body1)
 {
     Body0 = body0;
     Body1 = body1;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Called while this physical body is colliding with another body.
 /// </summary>
 /// <param name="other">The other body we are colliding with.</param>
 public void CallCollisionProcess(BasicPhysicalBody other)
 {
     EcsComponent.CallCollisionProcess(other.EcsComponent);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Called when this physical body start colliding with another body.
 /// </summary>
 /// <param name="other">The other body we collide with.</param>
 /// <param name="data">Extra collision data.</param>
 public void CallCollisionStart(BasicPhysicalBody other, ref CollisionData data)
 {
     EcsComponent.CallCollisionStart(other.EcsComponent, data);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Add a physical body to the world
 /// </summary>
 /// <param name="body">Physics entity to add.</param>
 public void AddBody(BasicPhysicalBody body)
 {
     body.AddSelfToBulletWorld(_world);
     body._world = this;
 }