예제 #1
0
        //called by Physics World just before constraint is added to world.
        //the current constraint properties are used to rebuild the constraint.
        internal override bool _BuildConstraint()
        {
            BPhysicsWorld world = BPhysicsWorld.Get();

            if (constraintPtr != null)
            {
                if (isInWorld && world != null)
                {
                    isInWorld = false;
                    world.RemoveConstraint(constraintPtr);
                }
            }
            if (targetRigidBodyA == null)
            {
                Debug.LogError("Constraint target rigid body was not set.");
                return(false);
            }

            RigidBody rba = targetRigidBodyA.GetRigidBody();

            if (rba == null)
            {
                Debug.LogError("Constraint could not get bullet RigidBody from target rigid body");
                return(false);
            }

            RigidBody rbb = targetRigidBodyB.GetRigidBody();

            if (rbb == null)
            {
                Debug.LogError("Constraint could not get bullet RigidBody from target rigid body");
                return(false);
            }
            BulletSharp.Math.Matrix frameInA = BulletSharp.Math.Matrix.Translation(localPointInA.ToBullet());
            BulletSharp.Math.Matrix frameInB = BulletSharp.Math.Matrix.Translation(localPointInB.ToBullet());
            constraintPtr = new FixedConstraint(rba, rbb, frameInA, frameInB);


            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_rigidBody != null && isInWorld && world != null)
            {
                isInWorld = false;
                world.RemoveRigidBody(m_rigidBody);
            }

            if (transform.localScale != UnityEngine.Vector3.one)
            {
                BDebug.LogError(debugType, "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)
            {
                BDebug.LogError(debugType, "There was no collision shape component attached to this BRigidBody. {0}", name);
                return(false);
            }

            CollisionShape cs = m_collisionShape.GetCollisionShape();

            if (m_motionState == null)
            {
                m_motionState = new BGameObjectMotionState(transform);
            }

            BulletSharp.RigidBody rb = (BulletSharp.RigidBody)m_collisionObject;
            CreateOrConfigureRigidBody(ref rb, ref _localInertia, cs, m_motionState);
            m_collisionObject            = rb;
            m_collisionObject.UserObject = this;

            return(true);
        }
        //todo should be properties so can capture changes and propagate to scene

        //called by Physics World just before constraint is added to world.
        //the current constraint properties are used to rebuild the constraint.
        internal override bool _BuildConstraint()
        {
            if (m_constraintType == ConstraintType.constrainToPointInSpace)
            {
                Debug.LogError("A FixedConstraint can only be constrained to another object.");
                return(false);
            }
            BPhysicsWorld world = BPhysicsWorld.Get();

            if (m_constraintPtr != null)
            {
                if (m_isInWorld && world != null)
                {
                    m_isInWorld = false;
                    world.RemoveConstraint(m_constraintPtr);
                }
            }
            BRigidBody targetRigidBodyA = GetComponent <BRigidBody>();

            if (targetRigidBodyA == null)
            {
                Debug.LogError("Fixed Constraint needs to be added to a component with a BRigidBody.");
                return(false);
            }
            if (!targetRigidBodyA.isInWorld)
            {
                world.AddRigidBody(targetRigidBodyA);
            }
            RigidBody rba = (RigidBody)targetRigidBodyA.GetCollisionObject();

            if (rba == null)
            {
                Debug.LogError("Constraint could not get bullet RigidBody from target rigid body");
                return(false);
            }

            if (m_otherRigidBody == null)
            {
                Debug.LogError("Other rigid body is not set");
                return(false);
            }
            if (!m_otherRigidBody.isInWorld)
            {
                world.AddRigidBody(m_otherRigidBody);
            }
            RigidBody rbb = (RigidBody)m_otherRigidBody.GetCollisionObject();

            if (rbb == null)
            {
                Debug.LogError("Constraint could not get bullet RigidBody from target rigid body");
                return(false);
            }
            BM.Matrix frameInA, frameInOther;
            string    errormsg = "";

            if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
            {
                m_constraintPtr = new FixedConstraint(rbb, rba, frameInOther, frameInA);
            }
            else
            {
                Debug.LogError(errormsg);
                return(false);
            }
            m_constraintPtr.Userobject               = this;
            m_constraintPtr.DebugDrawSize            = m_debugDrawSize;
            m_constraintPtr.BreakingImpulseThreshold = m_breakingImpulseThreshold;

            return(true);
        }
예제 #4
0
 protected virtual void AddMultiBodyToBulletWorld()
 {
     BPhysicsWorld.Get().AddMultiBody(this);
 }
예제 #5
0
        //called by Physics World just before constraint is added to world.
        //the current constraint properties are used to rebuild the constraint.
        internal override bool _BuildConstraint()
        {
            BPhysicsWorld world = BPhysicsWorld.Get();

            if (m_constraintPtr != null)
            {
                if (m_isInWorld && world != null)
                {
                    m_isInWorld = false;
                    world.RemoveConstraint(m_constraintPtr);
                }
            }
            BRigidBody targetRigidBodyA = GetComponent <BRigidBody>();

            if (targetRigidBodyA == null)
            {
                Debug.LogError("BSliderConstraint needs to be added to a component with a BRigidBody.");
                return(false);
            }
            if (!targetRigidBodyA.isInWorld)
            {
                world.AddRigidBody(targetRigidBodyA);
            }
            RigidBody rba = (RigidBody)targetRigidBodyA.GetCollisionObject();

            if (rba == null)
            {
                Debug.LogError("Constraint could not get bullet RigidBody from target rigid body");
                return(false);
            }
            if (m_constraintType == ConstraintType.constrainToAnotherBody)
            {
                if (m_otherRigidBody == null)
                {
                    Debug.LogError("Other rigid body was not set");
                    return(false);
                }
                if (!m_otherRigidBody.isInWorld)
                {
                    world.AddRigidBody(m_otherRigidBody);
                }
                RigidBody rbb = (RigidBody)m_otherRigidBody.GetCollisionObject();
                if (rbb == null)
                {
                    Debug.LogError("Constraint could not get bullet RigidBody from target rigid body");
                    return(false);
                }

                BM.Matrix frameInA, frameInOther;
                string    errormsg = "";
                if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
                {
                    m_constraintPtr = new SliderConstraint(rbb, rba, frameInOther, frameInA, true);
                }
                else
                {
                    Debug.LogError(errormsg);
                    return(false);
                }
            }
            else
            {
                BulletSharp.Math.Matrix frameInA = BM.Matrix.Identity;
                string errormsg = "";
                if (CreateFrame(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, ref frameInA, ref errormsg))
                {
                    m_constraintPtr = new SliderConstraint(rba, frameInA, true);
                }
                else
                {
                    Debug.LogError(errormsg);
                    return(false);
                }
            }
            SliderConstraint sl = (SliderConstraint)m_constraintPtr;

            sl.LowerLinearLimit = m_lowerLinearLimit;
            sl.UpperLinearLimit = m_upperLinearLimit;

            sl.LowerAngularLimit                     = m_lowerAngularLimitRadians;
            sl.UpperAngularLimit                     = m_upperAngularLimitRadians;
            m_constraintPtr.Userobject               = this;
            m_constraintPtr.DebugDrawSize            = m_debugDrawSize;
            m_constraintPtr.BreakingImpulseThreshold = m_breakingImpulseThreshold;
            return(true);
        }
예제 #6
0
 protected override void RemoveObjectFromBulletWorld()
 {
     BPhysicsWorld.Get().RemoveRigidBody((RigidBody)m_collisionObject);
 }
예제 #7
0
        //called by Physics World just before constraint is added to world.
        //the current constraint properties are used to rebuild the constraint.
        internal override bool _BuildConstraint()
        {
            BPhysicsWorld world = BPhysicsWorld.Get();

            if (m_constraintPtr != null)
            {
                if (m_isInWorld && world != null)
                {
                    m_isInWorld = false;
                    world.RemoveConstraint(m_constraintPtr);
                }
            }
            BRigidBody targetRigidBodyA = GetComponent <BRigidBody>();

            if (targetRigidBodyA == null)
            {
                Debug.LogError("BHingedConstraint needs to be added to a component with a BRigidBody.");
                return(false);
            }
            if (!targetRigidBodyA.isInWorld)
            {
                world.AddRigidBody(targetRigidBodyA);
            }
            if (m_localConstraintAxisX == Vector3.zero)
            {
                Debug.LogError("Constaint axis cannot be zero vector");
                return(false);
            }
            RigidBody rba = (RigidBody)targetRigidBodyA.GetCollisionObject();

            if (rba == null)
            {
                Debug.LogError("Constraint could not get bullet RigidBody from target rigid body");
                return(false);
            }
            if (m_constraintType == ConstraintType.constrainToAnotherBody)
            {
                if (m_otherRigidBody == null)
                {
                    Debug.LogError("Other rigid body must not be null");
                    return(false);
                }
                if (!m_otherRigidBody.isInWorld)
                {
                    world.AddRigidBody(m_otherRigidBody);
                }
                RigidBody rbb = (RigidBody)m_otherRigidBody.GetCollisionObject();
                if (rbb == null)
                {
                    Debug.LogError("Constraint could not get bullet RigidBody from target rigid body");
                    return(false);
                }
                BM.Matrix frameInA, frameInOther;
                string    errormsg = "";
                if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
                {
                    //warning the frameInA, frameInB version of the constructor is broken
                    m_constraintPtr = new HingeConstraint(rbb, rba, frameInOther.Origin, frameInA.Origin, (BM.Vector3)frameInOther.Basis.Column1, (BM.Vector3)frameInA.Basis.Column1);
                }
                else
                {
                    Debug.LogError(errormsg);
                    return(false);
                }
            }
            else
            {
                //BM.Matrix frameInA = BM.Matrix.Identity;
                //CreateFrame(m_localConstraintForwardDir, m_localConstraintUpDir, m_localConstraintPoint, ref frameInA);
                m_constraintPtr = new HingeConstraint(rba, m_localConstraintPoint.ToBullet(), m_localConstraintAxisX.ToBullet(), false);
            }
            if (m_enableMotor)
            {
                ((HingeConstraint)m_constraintPtr).EnableAngularMotor(true, m_targetMotorAngularVelocity, m_maxMotorImpulse);
            }
            if (m_setLimit)
            {
                ((HingeConstraint)m_constraintPtr).SetLimit(m_lowLimitAngleRadians, m_highLimitAngleRadians, m_limitSoftness, m_limitBiasFactor);
            }
            m_constraintPtr.Userobject                  = this;
            m_constraintPtr.DebugDrawSize               = m_debugDrawSize;
            m_constraintPtr.BreakingImpulseThreshold    = m_breakingImpulseThreshold;
            m_constraintPtr.OverrideNumSolverIterations = m_overrideNumSolverIterations;
            return(true);
        }
예제 #8
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 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);
        }
예제 #9
0
 protected override void AddObjectToBulletWorld()
 {
     BPhysicsWorld.Get().AddRigidBody(this);
 }
예제 #10
0
 protected virtual void RemoveObjectFromBulletWorld()
 {
     BPhysicsWorld.Get().RemoveCollisionObject(m_collisionObject);
 }
예제 #11
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);
        }
예제 #12
0
        internal override bool _BuildConstraint()
        {
            BPhysicsWorld world = BPhysicsWorld.Get();

            if (m_constraintPtr != null)
            {
                if (m_isInWorld && world != null)
                {
                    m_isInWorld = false;
                    world.RemoveConstraint(m_constraintPtr);
                }
            }
            BRigidBody targetRigidBodyA = GetComponent <BRigidBody>();

            if (targetRigidBodyA == null)
            {
                Debug.LogError("BGeneric6DofSpringConstraint needs to be added to a component with a BRigidBody.");
                return(false);
            }
            if (!targetRigidBodyA.isInWorld)
            {
                world.AddRigidBody(targetRigidBodyA);
            }
            if (m_localConstraintAxisX == Vector3.zero)
            {
                Debug.LogError("Constaint axis cannot be zero vector");
                return(false);
            }
            RigidBody rba = (RigidBody)targetRigidBodyA.GetCollisionObject();

            if (rba == null)
            {
                Debug.LogError("Constraint could not get bullet RigidBody from target rigid body");
                return(false);
            }
            if (m_constraintType == ConstraintType.constrainToAnotherBody)
            {
                if (m_otherRigidBody == null)
                {
                    Debug.LogError("Other rigid body must not be null");
                    return(false);
                }
                if (!m_otherRigidBody.isInWorld)
                {
                    world.AddRigidBody(m_otherRigidBody);
                }
                RigidBody rbb = (RigidBody)m_otherRigidBody.GetCollisionObject();
                if (rbb == null)
                {
                    Debug.LogError("Constraint could not get bullet RigidBody from target rigid body");
                    return(false);
                }
                BM.Matrix frameInA, frameInOther;
                string    errormsg = "";
                if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
                {
                    m_constraintPtr = new Generic6DofSpringConstraint(rba, rbb, frameInA, frameInOther, true);
                }
                else
                {
                    Debug.LogError(errormsg);
                    return(false);
                }
            }
            else
            {
                // TODO
                //   m_constraintPtr = new Generic6DofSpringConstraint(rba, m_localConstraintPoint.ToBullet(), m_localConstraintAxisX.ToBullet(), false);
            }
            if (m_setLimit)
            {
                ((Generic6DofSpringConstraint)m_constraintPtr).SetLimit((int)m_springAxis, m_lowLimit, m_highLimit);
            }
            ((Generic6DofSpringConstraint)m_constraintPtr).EnableSpring((int)m_springAxis, true);
            ((Generic6DofSpringConstraint)m_constraintPtr).SetStiffness((int)m_springAxis, m_stiffness);
            ((Generic6DofSpringConstraint)m_constraintPtr).SetDamping((int)m_springAxis, m_damping);
            m_constraintPtr.Userobject                  = this;
            m_constraintPtr.DebugDrawSize               = m_debugDrawSize;
            m_constraintPtr.BreakingImpulseThreshold    = m_breakingImpulseThreshold;
            m_constraintPtr.OverrideNumSolverIterations = m_overrideNumSolverIterations;
            return(true);
        }
        //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)
            {
                Debug.LogError("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(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);
            }
            if (!(m_collisionShape is BHeightfieldTerrainShape))
            {
                Debug.LogError("The collision shape needs to be a BHeightfieldTerrainShape. " + 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.Matrix.Identity;
                Vector3 pos = transform.position + new Vector3(td.size.x * .5f, td.size.y * .5f, td.size.z * .5f);
                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 = transform.position + new Vector3(td.size.x * .5f, td.size.y * .5f, td.size.z * .5f);
                worldTrans.Origin = pos.ToBullet();
                m_collisionObject.WorldTransform = worldTrans;
                m_collisionObject.CollisionFlags = m_collisionFlags;
            }
            return(true);
        }
        //called by Physics World just before constraint is added to world.
        //the current constraint properties are used to rebuild the constraint.
        internal override bool _BuildConstraint()
        {
            BPhysicsWorld world = BPhysicsWorld.Get();

            if (m_constraintPtr != null)
            {
                if (m_isInWorld && world != null)
                {
                    m_isInWorld = false;
                    world.RemoveConstraint(m_constraintPtr);
                    return(false);
                }
            }

            BRigidBody targetRigidBodyA = GetComponent <BRigidBody>();

            if (targetRigidBodyA == null)
            {
                Debug.LogError("ConeTwistConstraint needs to be added to a component with a BRigidBody.");
                return(false);
            }
            if (!targetRigidBodyA.isInWorld)
            {
                world.AddRigidBody(targetRigidBodyA);
            }
            if (m_constraintType == ConstraintType.constrainToAnotherBody)
            {
                if (m_otherRigidBody == null)
                {
                    Debug.LogError("Other rigid body was not set");
                    return(false);
                }
                if (!m_otherRigidBody.isInWorld)
                {
                    world.AddRigidBody(m_otherRigidBody);
                }
                BM.Matrix frameInA, frameInOther;
                string    errormsg = "";
                if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
                {
                    m_constraintPtr = new ConeTwistConstraint((RigidBody)m_otherRigidBody.GetCollisionObject(), (RigidBody)targetRigidBodyA.GetCollisionObject(), frameInOther, frameInA);
                }
                else
                {
                    Debug.LogError(errormsg);
                    return(false);
                }
            }
            else
            {
                //TODO this is broken
                string    errormsg = "";
                BM.Matrix frameInB = BM.Matrix.Identity;
                if (CreateFrame(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, ref frameInB, ref errormsg))
                {
                    m_constraintPtr = new ConeTwistConstraint((RigidBody)targetRigidBodyA.GetCollisionObject(), frameInB);
                }
                else
                {
                    Debug.LogError(errormsg);
                    return(false);
                }
            }
            ConeTwistConstraint sl = (ConeTwistConstraint)m_constraintPtr;

            sl.SetLimit(m_swingSpan1Radians, m_swingSpan2Radians, m_twistSpanRadians, m_softness, m_biasFactor, m_relaxationFactor);
            m_constraintPtr.Userobject               = this;
            m_constraintPtr.DebugDrawSize            = m_debugDrawSize;
            m_constraintPtr.BreakingImpulseThreshold = m_breakingImpulseThreshold;
            return(true);
        }
예제 #15
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)
            {
                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);
            }
            if (!(m_collisionShape.GetCollisionShape() is ConvexShape))
            {
                Debug.LogError("The CollisionShape on this BCharacterController was not a convex shape. " + name);
                return(false);
            }

            m_collisionShape.GetCollisionShape();
            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 = transform.rotation.ToBullet();
                BulletSharp.Math.Matrix.RotationQuaternion(ref q, out worldTrans);
                worldTrans.Origin = transform.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 = transform.rotation.ToBullet();
                BulletSharp.Math.Matrix.RotationQuaternion(ref q, out worldTrans);
                worldTrans.Origin = transform.position.ToBullet();
                m_collisionObject.WorldTransform = worldTrans;
                m_collisionObject.UserObject     = this;
            }
            return(true);
        }
        //called by Physics World just before constraint is added to world.
        //the current constraint properties are used to rebuild the constraint.
        internal override bool _BuildConstraint()
        {
            BPhysicsWorld world = BPhysicsWorld.Get();

            if (m_constraintPtr != null)
            {
                if (m_isInWorld && world != null)
                {
                    m_isInWorld = false;
                    world.RemoveConstraint(m_constraintPtr);
                }
            }
            BRigidBody targetRigidBodyA = GetComponent <BRigidBody>();

            if (targetRigidBodyA == null)
            {
                Debug.LogError("B6DOFConstraint needs to be added to a component with a BRigidBody.");
                return(false);
            }
            if (!targetRigidBodyA.isInWorld)
            {
                world.AddRigidBody(targetRigidBodyA);
            }

            RigidBody rba = (RigidBody)targetRigidBodyA.GetCollisionObject();

            if (rba == null)
            {
                Debug.LogError("Constraint could not get bullet RigidBody from target rigid body");
                return(false);
            }
            if (m_constraintType == ConstraintType.constrainToAnotherBody)
            {
                if (m_otherRigidBody == null)
                {
                    Debug.LogError("Other Rigid Body is not set.");
                    return(false);
                }
                if (!m_otherRigidBody.isInWorld)
                {
                    world.AddRigidBody(m_otherRigidBody);
                    return(false);
                }
                RigidBody rbb = (RigidBody)m_otherRigidBody.GetCollisionObject();
                if (rbb == null)
                {
                    Debug.LogError("Constraint could not get bullet RigidBody from target rigid body");
                    return(false);
                }

                BM.Matrix frameInA, frameInOther;
                string    errormsg = "";
                if (CreateFramesA_B(m_localConstraintAxisX, m_localConstraintAxisY, m_localConstraintPoint, out frameInA, out frameInOther, ref errormsg))
                {
                    m_constraintPtr = new Generic6DofConstraint(rbb, rba, frameInOther, frameInA, true);
                }
                else
                {
                    Debug.LogError(errormsg);
                    return(false);
                }
            }
            else
            {
                //TODO think about this
                BM.Matrix frameInA = BulletSharp.Math.Matrix.Identity;
                m_constraintPtr = new Generic6DofConstraint(rba, frameInA, false);
            }
            m_constraintPtr.Userobject = this;
            Generic6DofConstraint sl = (Generic6DofConstraint)m_constraintPtr;

            sl.LinearLowerLimit  = m_linearLimitLower.ToBullet();
            sl.LinearUpperLimit  = m_linearLimitUpper.ToBullet();
            sl.AngularLowerLimit = m_angularLimitLowerRadians.ToBullet();
            sl.AngularUpperLimit = m_angularLimitUpperRadians.ToBullet();
            sl.TranslationalLimitMotor.TargetVelocity = m_motorLinearTargetVelocity.ToBullet();
            sl.TranslationalLimitMotor.MaxMotorForce  = m_motorLinearMaxMotorForce.ToBullet();
            sl.BreakingImpulseThreshold = m_breakingImpulseThreshold;
            sl.DebugDrawSize            = m_debugDrawSize;
            return(true);
        }
예제 #17
0
 public virtual void BOnCollisionExit(SingleCollision details)
 {
     Debug.Log("Exit with " + details.obj + " details " + details.collisionDetails + " frame" + BPhysicsWorld.Get().frameCount);
 }
예제 #18
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_rigidBody != null)
            {
                if (isInWorld && world != null)
                {
                    isInWorld = false;
                    world.RemoveRigidBody(m_rigidBody);
                }
            }

            if (transform.localScale != UnityEngine.Vector3.one)
            {
                BDebug.LogError(debugType, "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)
            {
                BDebug.LogError(debugType, "There was no collision shape component attached to this BRigidBody. {0}", 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 (isDynamic())
            {
                cs.CalculateLocalInertia(_mass, out _localInertia);
            }

            if (m_rigidBody == null)
            {
                m_motionState = new BGameObjectMotionState(transform);
                RigidBodyConstructionInfo rbInfo;
                if (isDynamic())
                {
                    rbInfo = new RigidBodyConstructionInfo(_mass, m_motionState, cs, _localInertia);
                }
                else
                {
                    rbInfo = new RigidBodyConstructionInfo(0, m_motionState, cs, localInertia);
                }
                m_rigidBody            = new RigidBody(rbInfo);
                m_rigidBody.UserObject = this;
                rbInfo.Dispose();
                m_rigidBody.CollisionFlags = m_collisionFlags;
            }
            else
            {
                float usedMass = 0f;
                if (isDynamic())
                {
                    usedMass = _mass;
                }
                m_rigidBody.SetMassProps(usedMass, localInertia);
                m_rigidBody.CollisionShape = cs;
                m_rigidBody.CollisionFlags = m_collisionFlags;
            }

            //if kinematic then disable deactivation
            if ((m_collisionFlags & BulletSharp.CollisionFlags.KinematicObject) != 0)
            {
                m_rigidBody.ActivationState = ActivationState.DisableDeactivation;
            }
            return(true);
        }
 public override void BOnTriggerStay(CollisionObject other, AlignedManifoldArray manifoldArray)
 {
     Debug.Log("Stay with " + other.UserObject + " fixedFrame " + BPhysicsWorld.Get().frameCount);
 }
예제 #20
0
 protected virtual void AddObjectToBulletWorld()
 {
     BPhysicsWorld.Get().AddCollisionObject(this);
 }
 public override void BOnTriggerExit(CollisionObject other)
 {
     Debug.Log("Exit with " + other.UserObject + " fixedFrame " + BPhysicsWorld.Get().frameCount);
 }
예제 #22
0
 //It is critical that Awake be called before any other scripts call BPhysicsWorld.Get()
 //Set this script and any derived classes very early in script execution order.
 protected virtual void Awake()
 {
     _isDisposed = false;
     singleton   = BPhysicsWorld.Get();
 }
        void FixedUpdate()
        {
            CollisionWorld collisionWorld = BPhysicsWorld.Get().world;

            collisionWorld.Dispatcher.DispatchAllCollisionPairs(m_ghostObject.OverlappingPairCache, collisionWorld.DispatchInfo, collisionWorld.Dispatcher);

            //m_currentPosition = m_ghostObject.WorldTransform.Origin;

            //float maxPen = 0f;
            objsCurrentlyInContactWith.Clear();
            for (int i = 0; i < m_ghostObject.OverlappingPairCache.NumOverlappingPairs; i++)
            {
                manifoldArray.Clear();

                BroadphasePair collisionPair = m_ghostObject.OverlappingPairCache.OverlappingPairArray[i];

                CollisionObject obj0 = collisionPair.Proxy0.ClientObject as CollisionObject;
                CollisionObject obj1 = collisionPair.Proxy1.ClientObject as CollisionObject;

                if ((obj0 != null && !obj0.HasContactResponse) || (obj1 != null && !obj1.HasContactResponse))
                {
                    continue;
                }

                if (collisionPair.Algorithm != null)
                {
                    collisionPair.Algorithm.GetAllContactManifolds(manifoldArray);
                }

                CollisionObject otherObj = null;
                if (manifoldArray.Count > 0)
                {
                    PersistentManifold pm = manifoldArray[0];
                    if (pm.Body0 == m_collisionObject)
                    {
                        otherObj = pm.Body1;
                    }
                    else
                    {
                        otherObj = pm.Body0;
                    }
                }
                else
                {
                    continue;
                }

                objsCurrentlyInContactWith.Add(otherObj);
                if (!objsIWasInContactWithLastFrame.Contains(otherObj))
                {
                    BOnTriggerEnter(otherObj, manifoldArray);
                }
                else
                {
                    BOnTriggerStay(otherObj, manifoldArray);
                }
            }
            objsIWasInContactWithLastFrame.ExceptWith(objsCurrentlyInContactWith);

            foreach (CollisionObject co in objsIWasInContactWithLastFrame)
            {
                BOnTriggerExit(co);
            }

            //swap the hashsets so objsIWasInContactWithLastFrame now contains the list of objs.
            HashSet <CollisionObject> temp = objsIWasInContactWithLastFrame;

            objsIWasInContactWithLastFrame = objsCurrentlyInContactWith;
            objsCurrentlyInContactWith     = temp;
        }