//called by Physics World just before rigid body is added to world. //the current rigid body properties are used to rebuild the rigid body. internal override bool _BuildCollisionObject() { BPhysicsWorld world = BPhysicsWorld.Get; if (m_rigidBody != null && isInWorld && world != null) { isInWorld = false; world.RemoveRigidBody(m_rigidBody); } m_collisionShape = this.GetParent <Unit>().GetComponent <BCollisionShape>().baseBCollisionShape; //必须在这里另外赋值,这个是最重要的组件。基本都用到它。 if (m_collisionShape == null) { Log.Warning("There was no collision shape component attached to this BRigidBody. {0}"); return(false); } CollisionShape cs = this.GetParent <Unit>().GetComponent <BCollisionShape>().GetCollisionShape; if (m_motionState == null) { m_motionState = new BGameObjectMotionState(this._Unit, this._Unit.Position, this._Unit.Quaternion);//创建MotionState,没有就无法更新状态。 } BulletSharp.RigidBody rb = (BulletSharp.RigidBody)m_collisionObject; CreateOrConfigureRigidBody(ref rb, ref _localInertia, cs, m_motionState); m_collisionObject = rb; m_collisionObject.UserObject = this.GetParent <Unit>();//这里就是碰撞检测等传递的对象。如果想不用unit,可以在这里改。 return(true); }
protected override void RemoveObjectFromBulletWorld() { BPhysicsWorld pw = BPhysicsWorld.Get; if (pw != null && m_rigidBody != null && isInWorld) { // Debug.Assert(m_rigidBody.NumConstraintRefs == 0, "Removing rigid body that still had constraints. Remove constraints first."); //constraints must be removed before rigid body is removed pw.RemoveRigidBody((RigidBody)m_collisionObject); } }