Пример #1
0
        internal override bool _BuildCollisionObject()
        {
            BPhysicsWorld world = BPhysicsWorld.Get;

            if (m_collisionObject != null)
            {
                if (isInWorld && world != null)
                {
                    world.RemoveCollisionObject(this);
                }
            }

            // if (transform.localScale != UnityEngine.Vector3.one)
            // {
            //     Log.Error("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.");
            // }

            m_collisionShape = this.GetParent <Unit>().GetComponent <BCollisionShape>();
            if (m_collisionShape == null)
            {
                Log.Error("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 BulletSharp.PairCachingGhostObject();
                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.UserObject      = this;
                m_collisionObject.CollisionFlags  = m_collisionObject.CollisionFlags | BulletSharp.CollisionFlags.KinematicObject;
                m_collisionObject.CollisionFlags &= ~BulletSharp.CollisionFlags.StaticObject;
            }
            else
            {
                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.CollisionShape  = cs;
                m_collisionObject.CollisionFlags  = m_collisionObject.CollisionFlags | BulletSharp.CollisionFlags.KinematicObject;
                m_collisionObject.CollisionFlags &= ~BulletSharp.CollisionFlags.StaticObject;
            }
            return(true);
        }
Пример #2
0
        //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_collisionObject != null)
            {
                if (isInWorld && world != null)
                {
                    isInWorld = false;
                    world.RemoveCollisionObject(this);
                }
            }
            m_collisionShape = this.GetParent <Unit>().GetComponent <BCollisionShape>().baseBCollisionShape;//这里将不能用mono的方式获取,就直接赋值了!
            if (m_collisionShape == null)
            {
                Log.Warning("There was no collision shape component attached to this BRigidBody. ");
                return(false);
            }
            if (!(m_collisionShape is BConvexHullShape))//这里是直接参考了BTerrainCollisionObject
            {
                Log.Warning("The collision shape needs to be a BHeightfieldTerrainShape. ");
                return(false);
            }

            CollisionShape cs = this.GetParent <Unit>().GetComponent <BCollisionShape>().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.Matrix.Identity;
                // Vector3 pos = this.GetParent<Unit>().Position + new Vector3(td.size.x * .5f, td.size.y * .5f, td.size.z * .5f);
                Vector3 pos = this.GetParent <Unit>().Position;
                worldTrans.Origin = pos.ToBullet();
                m_collisionObject.WorldTransform = worldTrans;
                m_collisionObject.CollisionFlags = m_collisionFlags;
            }
            else
            {
                m_collisionObject.CollisionShape = cs;
                BulletSharp.Math.Matrix worldTrans = BulletSharp.Math.Matrix.Identity;
                // Vector3 pos = this.GetParent<Unit>().Position + new Vector3(td.size.x * .5f, td.size.y * .5f, td.size.z * .5f);
                Vector3 pos = this.GetParent <Unit>().Position;
                worldTrans.Origin = pos.ToBullet();
                m_collisionObject.WorldTransform = worldTrans;
                m_collisionObject.CollisionFlags = m_collisionFlags;
            }
            return(true);
        }
Пример #3
0
        internal override bool _BuildCollisionObject()
        {
            BPhysicsWorld world = BPhysicsWorld.Get;

            if (m_collisionObject != null)
            {
                if (isInWorld && world != null)
                {
                    world.RemoveCollisionObject(this);
                }
            }
            m_collisionShape = this.GetParent <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 BulletSharp.GhostObject();
                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.UserObject     = this;
                m_collisionObject.CollisionFlags = m_collisionObject.CollisionFlags | BulletSharp.CollisionFlags.NoContactResponse;
            }
            else
            {
                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.CollisionShape = cs;
                m_collisionObject.CollisionFlags = m_collisionObject.CollisionFlags | BulletSharp.CollisionFlags.NoContactResponse;
            }
            return(true);
        }
Пример #4
0
        //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()
        {
            // if (td == null)
            // {
            //     Log.Warning("Must be attached to an object with a terrain ");
            //     return false;
            // }
            BPhysicsWorld world = BPhysicsWorld.Get;

            if (m_collisionObject != null)
            {
                if (isInWorld && world != null)
                {
                    isInWorld = false;
                    world.RemoveCollisionObject(this);
                }
            }

            // if (transform.localScale != UnityEngine.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.");
            // }

            m_collisionShape = this.GetParent <Unit>().GetComponent <BCollisionShape>();//这里将不能用mono的方式获取,就直接赋值了!
            if (m_collisionShape == null)
            {
                Log.Warning("There was no collision shape component attached to this BRigidBody. ");
                return(false);
            }
            // if (!(m_collisionShape is BHeightfieldTerrainShape))
            // {
            //     Log.Warning("The collision shape needs to be a BHeightfieldTerrainShape. ");
            //     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.Matrix.Identity;
                // Vector3 pos = this.GetParent<Unit>().Position + new Vector3(td.size.x * .5f, td.size.y * .5f, td.size.z * .5f);
                Vector3 pos = this.GetParent <Unit>().Position;
                worldTrans.Origin = pos.ToBullet();
                m_collisionObject.WorldTransform = worldTrans;
                m_collisionObject.CollisionFlags = m_collisionFlags;
            }
            else
            {
                m_collisionObject.CollisionShape = cs;
                BulletSharp.Math.Matrix worldTrans = BulletSharp.Math.Matrix.Identity;
                // Vector3 pos = this.GetParent<Unit>().Position + new Vector3(td.size.x * .5f, td.size.y * .5f, td.size.z * .5f);
                Vector3 pos = this.GetParent <Unit>().Position;
                worldTrans.Origin = pos.ToBullet();
                m_collisionObject.WorldTransform = worldTrans;
                m_collisionObject.CollisionFlags = m_collisionFlags;
            }
            return(true);
        }
Пример #5
0
        internal override bool _BuildCollisionObject()
        {
            BPhysicsWorld world = BPhysicsWorld.Get;

            if (m_collisionObject != null)
            {
                if (isInWorld && world != null)
                {
                    isInWorld = false;
                    world.RemoveCollisionObject(this);
                }
            }

            // if (transform.localScale != UnityEngine.Vector3.one)
            // {
            //     Log.Error("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.");
            // }

            m_collisionShape = this.GetParent <Unit>().GetComponent <BCollisionShape>();
            if (m_collisionShape == null)
            {
                Log.Error("There was no collision shape component attached to this BRigidBody. ");
                return(false);
            }
            if (!(m_collisionShape.GetCollisionShape is ConvexShape))
            {
                Log.Error("The CollisionShape on this BCharacterController was not a convex shape. ");
                return(false);
            }

            if (m_collisionObject == null)
            {
                m_collisionObject = new PairCachingGhostObject();
                m_collisionObject.CollisionShape = m_collisionShape.GetCollisionShape;
                m_collisionObject.CollisionFlags = m_collisionFlags;
                m_characterController            = new KinematicCharacterController((PairCachingGhostObject)m_collisionObject, (ConvexShape)m_collisionShape.GetCollisionShape, stepHeight, upAxis);
                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.UserObject     = this;
                //world.world.AddCollisionObject(m_collisionObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter);
                //((DynamicsWorld)world.world).AddAction(m_characterController);
            }
            else
            {
                m_collisionObject.CollisionShape = m_collisionShape.GetCollisionShape;
                m_collisionObject.CollisionFlags = m_collisionFlags;
                if (m_characterController != null)
                {
                    world.RemoveAction(m_characterController);
                }
                m_characterController = new KinematicCharacterController((PairCachingGhostObject)m_collisionObject, (ConvexShape)m_collisionShape.GetCollisionShape, stepHeight, upAxis);
                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.UserObject     = this;
            }
            return(true);
        }