//Don't try to call functions on other objects such as the Physics world since they may not exit. public virtual void Awake() { m_collisionShape = this.GetParent <Unit>().GetComponent <BCollisionShape>(); if (m_collisionShape == null) { Log.Warning("A BCollisionObject component must be on an object with a BCollisionShape component."); } }
//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 virtual bool _BuildCollisionObject() { BPhysicsWorld world = BPhysicsWorld.Get; if (m_collisionObject != null) { if (isInWorld && world != null) { world.RemoveCollisionObject(this); } } // if (transform.localScale != Vector3.one) // { // Log.Warning("The local scale on this collision shape is not one. Bullet physics does not support scaling on a rigid body world transform. Instead alter the dimensions of the CollisionShape."); // } Unit Unit = this.GetParent <Unit>(); m_collisionShape = Unit.GetComponent <BCollisionShape>(); //必须用另一方式来赋值才行啊!! if (m_collisionShape == null) { Log.Warning("There was no collision shape component attached to this BRigidBody. "); return(false); } CollisionShape cs = m_collisionShape.GetCollisionShape; //rigidbody is dynamic if and only if mass is non zero, otherwise static if (m_collisionObject == null) { m_collisionObject = new CollisionObject(); m_collisionObject.CollisionShape = cs; m_collisionObject.UserObject = this; BulletSharp.Math.Matrix worldTrans; BulletSharp.Math.Quaternion q = this.GetParent <Unit>().Quaternion.ToBullet(); BulletSharp.Math.Matrix.RotationQuaternion(ref q, out worldTrans); worldTrans.Origin = this.GetParent <Unit>().Position.ToBullet(); m_collisionObject.WorldTransform = worldTrans; m_collisionObject.CollisionFlags = m_collisionFlags; } else { m_collisionObject.CollisionShape = cs; BulletSharp.Math.Matrix worldTrans; BulletSharp.Math.Quaternion q = this.GetParent <Unit>().Quaternion.ToBullet(); BulletSharp.Math.Matrix.RotationQuaternion(ref q, out worldTrans); worldTrans.Origin = this.GetParent <Unit>().Position.ToBullet(); m_collisionObject.WorldTransform = worldTrans; m_collisionObject.CollisionFlags = m_collisionFlags; } return(true); }