/// <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); }; }
/// <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; }
/// <summary> /// Update single object's aabb. /// </summary> /// <param name="body">Body to update.</param> public void UpdateSingleAabb(BasicPhysicalBody body) { _world.UpdateSingleAabb(body._BulletEntity); }
/// <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; }
public CollisionPersistData(BasicPhysicalBody body0, BasicPhysicalBody body1) { Body0 = body0; Body1 = body1; }
/// <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); }
/// <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); }
/// <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; }