Example #1
0
 public void RemoveBCollisionShape(BCollisionShape bs)
 {
     if (colliders != null)
     {
         colliders.Remove(bs);
     }
 }
 //Don't try to call functions on other objects such as the Physics world since they may not exit.
 protected virtual void Awake()
 {
     m_collisionShape = GetComponent <BCollisionShape>();
     if (m_collisionShape == null)
     {
         Debug.LogError("A BCollisionObject component must be on an object with a BCollisionShape component.");
     }
 }
        /// <summary>
        /// called by Physics World just before rigid body is added to world.
        /// the current rigid body properties are used to rebuild the rigid body.
        /// </summary>
        internal virtual bool _BuildCollisionObject()
        {
            WorldController world = GetWorld();

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

            if (transform.localScale != Vector3.one)
            {
                Debug.LogError("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.", transform);
            }

            m_collisionShape = GetComponent <BCollisionShape>();

            if (m_collisionShape == null)
            {
                Debug.LogError("There was no collision shape component attached to this BRigidBody. " + name, transform);
                return(false);
            }

            CollisionShape cs = m_collisionShape.GetCollisionShape();

            //rigidbody is dynamic if and only if mass is non zero, otherwise static

            if (collisionObject == null)
            {
                collisionObject = new CollisionObject();
                collisionObject.CollisionShape = cs;
                collisionObject.UserObject     = this;

                MatrixB     worldTrans;
                QuaternionB q = transform.rotation.ToBullet();
                MatrixB.RotationQuaternion(ref q, out worldTrans);
                worldTrans.Origin = transform.position.ToBullet();
                collisionObject.WorldTransform = worldTrans;
                collisionObject.CollisionFlags = m_collisionFlags;
            }
            else
            {
                collisionObject.CollisionShape = cs;
                MatrixB     worldTrans;
                QuaternionB q = transform.rotation.ToBullet();
                MatrixB.RotationQuaternion(ref q, out worldTrans);
                worldTrans.Origin = transform.position.ToBullet();
                collisionObject.WorldTransform = worldTrans;
                collisionObject.CollisionFlags = m_collisionFlags;
            }

            return(true);
        }
Example #4
0
 public void AddBCollisionShape(BCollisionShape bs)
 {
     if (colliders == null)
     {
         colliders = new List <BCollisionShape>();
     }
     if (!colliders.Contains(bs))
     {
         colliders.Add(bs);
     }
 }
 void Awake()
 {
     BRigidBody[] rbs = GetComponentsInParent <BRigidBody>();
     if (rbs.Length != 1)
     {
         Debug.LogError("Can't nest rigid bodies. The transforms are updated by Bullet in undefined order which can cause spasing. Object " + name);
     }
     m_collisionShape = GetComponent <BCollisionShape>();
     if (m_collisionShape == null)
     {
         Debug.LogError("A BRigidBody component must be on an object with a BCollisionShape component.");
     }
 }
Example #6
0
        /// <summary>
        /// A MultiBody object needs to be created and added a controled sequence
        ///    1) The multibody needs to be added to the PhysicsWorld.
        ///    2) Then the base colliders and other colliders can be created and added. They
        ///       depend on the multibody reference.
        ///    3) Then collision objects can be added.
        /// </summary>
        internal void CreateColliders()
        {
            if (m_multibody == null)
            {
                Debug.LogError("Multibody must exist before creating colliders");
                return;
            }
            m_baseCollider            = new MultiBodyLinkCollider(m_multibody, -1);
            m_baseCollider.UserObject = this;
            //todo validate that shape exists on base and all
            BCollisionShape shape = GetComponent <BCollisionShape>();

            m_baseCollider.CollisionShape = shape.GetCollisionShape();
            Matrix worldTrans = Matrix.RotationQuaternion(transform.rotation.ToBullet());

            worldTrans.Origin             = transform.position.ToBullet();
            m_baseCollider.WorldTransform = worldTrans;
            bool isDynamic = (baseMass > 0 && !fixedBase);

            m_groupsIBelongTo        = isDynamic ? (m_groupsIBelongTo) : (m_groupsIBelongTo | BulletSharp.CollisionFilterGroups.StaticFilter);
            m_collisionMask          = isDynamic ? (m_collisionMask) : (m_collisionMask | BulletSharp.CollisionFilterGroups.StaticFilter);
            m_multibody.BaseCollider = m_baseCollider;

            for (int i = 0; i < m_links.Count; i++)
            {
                //create colliders
                MultiBodyLinkCollider col = new MultiBodyLinkCollider(m_multibody, m_links[i].index);
                m_links[i].AssignMultiBodyLinkColliderOnCreation(this, col);
                col.UserObject     = m_links[i];
                shape              = m_links[i].GetComponent <BCollisionShape>();
                col.CollisionShape = shape.GetCollisionShape();
                worldTrans         = Matrix.RotationQuaternion(m_links[i].transform.rotation.ToBullet());
                worldTrans.Origin  = m_links[i].transform.position.ToBullet();
                col.WorldTransform = worldTrans;
                m_multibody.GetLink(i).Collider = col;
            }
        }
        //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 bool _BuildRigidBody()
        {
            BPhysicsWorld world = BPhysicsWorld.Get();

            if (m_Brigidbody != null)
            {
                if (isInWorld && world != null)
                {
                    isInWorld = false;
                    world.RemoveRigidBody(m_Brigidbody);
                }
            }

            if (transform.localScale != UnityEngine.Vector3.one)
            {
                Debug.LogError("The local scale on this rigid body is not one. Bullet physics does not support scaling on a rigid body world transform. Instead alter the dimensions of the CollisionShape.");
            }

            m_collisionShape = GetComponent <BCollisionShape>();
            if (m_collisionShape == null)
            {
                Debug.LogError("There was no collision shape component attached to this BRigidBody. " + name);
                return(false);
            }

            CollisionShape cs = m_collisionShape.GetCollisionShape();

            //rigidbody is dynamic if and only if mass is non zero, otherwise static
            _localInertia = BulletSharp.Math.Vector3.Zero;
            if (_type == RBType.dynamic)
            {
                cs.CalculateLocalInertia(_mass, out _localInertia);
            }

            if (m_Brigidbody == null)
            {
                m_motionState = new BGameObjectMotionState(transform);
                RigidBodyConstructionInfo rbInfo;
                if (_type == RBType.dynamic)
                {
                    rbInfo = new RigidBodyConstructionInfo(_mass, m_motionState, cs, _localInertia);
                }
                else
                {
                    rbInfo = new RigidBodyConstructionInfo(0, m_motionState, cs, localInertia);
                }
                m_Brigidbody = new RigidBody(rbInfo);
                rbInfo.Dispose();
            }
            else
            {
                m_Brigidbody.SetMassProps(_mass, localInertia);
                m_Brigidbody.CollisionShape = cs;
            }

            if (_type == RBType.kinematic)
            {
                m_Brigidbody.CollisionFlags  = m_Brigidbody.CollisionFlags | BulletSharp.CollisionFlags.KinematicObject;
                m_Brigidbody.ActivationState = ActivationState.DisableDeactivation;
            }
            if (_isTrigger)
            {
                m_Brigidbody.CollisionFlags = m_Brigidbody.CollisionFlags | BulletSharp.CollisionFlags.NoContactResponse;
            }
            return(true);
        }
 //Don't try to call functions on other objects such as the Physics world since they may not exit.
 protected virtual void Awake()
 {
     m_collisionShape = GetComponent<BCollisionShape>();
     if (m_collisionShape == null)
     {
         Debug.LogError("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(m_collisionObject);
                }
            }

            if (transform.localScale != UnityEngine.Vector3.one)
            {
                Debug.LogError("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 = GetComponent<BCollisionShape>();
            if (m_collisionShape == null)
            {
                Debug.LogError("There was no collision shape component attached to this BRigidBody. " + name);
                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 = transform.rotation.ToBullet();
                BulletSharp.Math.Matrix.RotationQuaternion(ref q, out worldTrans);
                worldTrans.Origin = transform.position.ToBullet();
                m_collisionObject.WorldTransform = worldTrans;
                m_collisionObject.CollisionFlags = m_collisionFlags;
            }
            else {
                m_collisionObject.CollisionShape = cs;
                BulletSharp.Math.Matrix worldTrans;
                BulletSharp.Math.Quaternion q = transform.rotation.ToBullet();
                BulletSharp.Math.Matrix.RotationQuaternion(ref q, out worldTrans);
                worldTrans.Origin = transform.position.ToBullet();
                m_collisionObject.WorldTransform = worldTrans;
                m_collisionObject.CollisionFlags = m_collisionFlags;
            }
            return true;
        }
Example #10
0
        internal bool _BuildMultiBody()
        {
            BPhysicsWorld world = BPhysicsWorld.Get();

            if (m_multibody != null && isInWorld && world != null)
            {
                isInWorld = false;
                world.RemoveMultiBody(this);
            }

            if (transform.localScale != UnityEngine.Vector3.one)
            {
                Debug.LogErrorFormat("The local scale on {0} rigid body is not one. Bullet physics does not support scaling on a rigid body world transform. Instead alter the dimensions of the CollisionShape.", name);
            }

            if (!fixedBase && baseMass <= 0f)
            {
                Debug.LogErrorFormat("If not fixed base then baseMass must be greater than zero.");
                return(false);
            }

            m_baseCollisionShape = GetComponent <BCollisionShape>();
            if (m_baseCollisionShape == null)
            {
                Debug.LogErrorFormat("There was no collision shape component attached to this BMultiBody. {0}", name);
                return(false);
            }
            if (GetComponent <BMultiBodyLink>() != null)
            {
                Debug.LogErrorFormat("There must not be a BMultiBodyLink component attached to the gameObject with a BMultiBody component. {0}", name);
                return(false);
            }

            m_links = new List <BMultiBodyLink>();
            if (!GetLinksInChildrenAndNumber(transform, m_links, -1))
            {
                Debug.LogError("Error building multibody");
                return(false);
            }

            if (m_links.Count == 0)
            {
                Debug.LogError("Could not find any links");
                return(false);
            }

            if (!ValidateMultiBodyHierarchy(m_links))
            {
                return(false);
            }

            BCollisionShape[]      shapes      = new BCollisionShape[m_links.Count];
            BMultiBodyConstraint[] constraints = new BMultiBodyConstraint[m_links.Count];
            for (int i = 0; i < m_links.Count; i++)
            {
                shapes[i] = m_links[i].GetComponent <BCollisionShape>();
                if (shapes[i] == null && shapes[i].GetComponent <RigidBody>() != null)
                {
                    Debug.LogErrorFormat("BMultiBodyLink must not have a RigidBody component. {0}", m_links[i]);
                    return(false);
                }
                constraints[i] = m_links[i].GetComponent <BMultiBodyConstraint>();
            }

            BulletSharp.MultiBody mb = m_multibody;
            CreateOrConfigureMultiBody(ref mb, baseMass, shapes, constraints);
            m_multibody = mb;

            //TODO is this allowed
            //m_multibody.UserObject = this;

            return(true);
        }
 void Awake()
 {
     BRigidBody[] rbs = GetComponentsInParent<BRigidBody>();
     if (rbs.Length != 1) {
         Debug.LogError("Can't nest rigid bodies. The transforms are updated by Bullet in undefined order which can cause spasing. Object " + name);
     }
     m_collisionShape = GetComponent<BCollisionShape>();
     if (m_collisionShape == null) {
         Debug.LogError("A BRigidBody 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 bool _BuildRigidBody()
        {
            BPhysicsWorld world = BPhysicsWorld.Get();
            if (m_Brigidbody != null) {
                if (isInWorld && world != null) {
                    isInWorld = false;
                    world.RemoveRigidBody(m_Brigidbody);
                }
            }

            if (transform.localScale != UnityEngine.Vector3.one) {
                Debug.LogError("The local scale on this rigid body is not one. Bullet physics does not support scaling on a rigid body world transform. Instead alter the dimensions of the CollisionShape.");
            }

            m_collisionShape = GetComponent<BCollisionShape>();
            if (m_collisionShape == null) {
                Debug.LogError("There was no collision shape component attached to this BRigidBody. " + name);
                return false;
            }

            CollisionShape cs = m_collisionShape.GetCollisionShape();
            //rigidbody is dynamic if and only if mass is non zero, otherwise static
            _localInertia = BulletSharp.Math.Vector3.Zero;
            if (_type == RBType.dynamic) {
                cs.CalculateLocalInertia(_mass, out _localInertia);
            }

            if (m_Brigidbody == null) {
                m_motionState = new BGameObjectMotionState(transform);
                RigidBodyConstructionInfo rbInfo;
                if (_type == RBType.dynamic) {
                    rbInfo = new RigidBodyConstructionInfo(_mass, m_motionState, cs, _localInertia);
                } else {
                    rbInfo = new RigidBodyConstructionInfo(0, m_motionState, cs, localInertia);
                }
                m_Brigidbody = new RigidBody(rbInfo);
                rbInfo.Dispose();
            } else {
                m_Brigidbody.SetMassProps(_mass, localInertia);
                m_Brigidbody.CollisionShape = cs;
            }

            if (_type == RBType.kinematic) {
                m_Brigidbody.CollisionFlags = m_Brigidbody.CollisionFlags | BulletSharp.CollisionFlags.KinematicObject;
                m_Brigidbody.ActivationState = ActivationState.DisableDeactivation;
            }
            if (_isTrigger)
            {
                m_Brigidbody.CollisionFlags = m_Brigidbody.CollisionFlags | BulletSharp.CollisionFlags.NoContactResponse;
            }
            return true;
        }