Example #1
0
        //----------------------------------------------------------------------------------------------

        public RigidBody LocalCreateRigidBodyMultiWorld(float mass, ref IndexedMatrix startTransform, CollisionShape shape, DiscreteDynamicsWorld world)
        {
            Debug.Assert((shape == null || shape.GetShapeType() != BroadphaseNativeTypes.INVALID_SHAPE_PROXYTYPE));

            //rigidbody is dynamic if and only if mass is non zero, otherwise static
            bool isDynamic = !MathUtil.CompareFloat(mass, 0f);

            IndexedVector3 localInertia = IndexedVector3.Zero;

            if (isDynamic)
            {
                shape.CalculateLocalInertia(mass, out localInertia);
            }
            //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects

            //#define USE_MOTIONSTATE 1
            //#ifdef USE_MOTIONSTATE
            DefaultMotionState myMotionState = new DefaultMotionState(startTransform, IndexedMatrix.Identity);

            RigidBodyConstructionInfo cInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);

            RigidBody body = new RigidBody(cInfo);

            if (BulletGlobals.g_streamWriter != null && true)
            {
                BulletGlobals.g_streamWriter.WriteLine("localCreateRigidBody [{0}] startTransform", body.m_debugBodyId);
                MathUtil.PrintMatrix(BulletGlobals.g_streamWriter, startTransform);
                BulletGlobals.g_streamWriter.WriteLine("");
            }

            world.AddRigidBody(body);

            return(body);
        }
Example #2
0
            public RigidBody LocalCreateRigidBody(float mass, Matrix startTransform, CollisionShape shape)
            {
                //rigidbody is dynamic if and only if mass is non zero, otherwise static
                bool isDynamic = (mass != 0.0f);

                Vector3 localInertia = Vector3.Zero;

                if (isDynamic)
                {
                    shape.CalculateLocalInertia(mass, out localInertia);
                }

                //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
                DefaultMotionState myMotionState = new DefaultMotionState(startTransform);
                RigidBody          body;

                using (var rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia))
                {
                    body = new RigidBody(rbInfo);
                }

                ownerWorld.AddRigidBody(body);

                return(body);
            }
Example #3
0
        protected void RecalculateInertia(bool set = false)
        {
            if (Node)
            {
                CoreEngine.pEngine.World.RemoveCollisionObject(body);
            }
            if (set)
            {
                body.MotionState.Dispose();
                body.Dispose();
            }

            Vector3 inertia = Vector3.Zero;

            shape.CalculateLocalInertia(mass, out inertia);
            if (Node)
            {
                rbInfo = new RigidBodyConstructionInfo(mass, new DefaultMotionState(Node.GetTransform.GlobalTransform.Convert()), shape, inertia);
            }
            else
            {
                rbInfo = new RigidBodyConstructionInfo(mass, new DefaultMotionState(Matrix.Identity), shape, inertia);
            }
            body = new RigidBody(rbInfo);

            if (Node)
            {
                CoreEngine.pEngine.World.AddRigidBody(body, GroupFlags, CollisionGroupFlags);
            }
        }
        public override RigidBody LocalCreateRigidBody(float mass, Matrix startTransform, CollisionShape shape, bool isKinematic)
        {
            //rigidbody is dynamic if and only if mass is non zero, otherwise static
            bool isDynamic = (mass != 0.0f);

            Vector3 localInertia = Vector3.Zero;

            if (isDynamic)
            {
                shape.CalculateLocalInertia(mass, out localInertia);
            }

            //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects

            RigidBody body;

            using (var rbInfo = new RigidBodyConstructionInfo(mass, null, shape, localInertia))
            {
                body = new RigidBody(rbInfo);
            }

            body.WorldTransform = startTransform;
            World.AddRigidBody(body, CollisionFilterGroups.DefaultFilter,
                               CollisionFilterGroups.DefaultFilter | CollisionFilterGroups.StaticFilter);
            return(body);
        }
Example #5
0
        public static RigidBody CreateBody(float mass, Matrix startTransform, CollisionShape shape, DynamicsWorld world)
        {
            // A body with zero mass is considered static
            if (mass == 0)
            {
                return(CreateStaticBody(startTransform, shape, world));
            }

            // Using a motion state is recommended,
            // it provides interpolation capabilities and only synchronizes "active" objects
            var myMotionState = new DefaultMotionState(startTransform);

            Vector3 localInertia = shape.CalculateLocalInertia(mass);

            RigidBody body;

            using (var rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia))
            {
                body = new RigidBody(rbInfo);
            }

            if (world != null)
            {
                world.AddRigidBody(body);
            }

            return(body);
        }
Example #6
0
        /// <summary>
        /// 剛体を作る
        /// </summary>
        /// <param name="collisionShape">剛体の形</param>
        /// <param name="world">剛体のワールド変換行列</param>
        /// <param name="rigidProperty">剛体の物性</param>
        /// <param name="superProperty">物理演算を超越した特性</param>
        /// <returns></returns>
        public RigidBody CreateRigidBody(CollisionShape collisionShape, Matrix world, RigidProperty rigidProperty, SuperProperty superProperty)
        {
            var mass = superProperty.kinematic ? 0 : rigidProperty.mass;

            collisionShapes.Add(collisionShape);
            Vector3 localInertia = new Vector3(0, 0, 0);

            if (mass != 0)
            {
                collisionShape.CalculateLocalInertia(mass, out localInertia);
            }
            DefaultMotionState        motionState = new DefaultMotionState(world);
            RigidBodyConstructionInfo rbInfo      = new RigidBodyConstructionInfo(mass, motionState, collisionShape, localInertia);
            RigidBody body = new RigidBody(rbInfo);

            body.Restitution = rigidProperty.restitution;
            body.Friction    = rigidProperty.friction;
            body.SetDamping(rigidProperty.linear_damp, rigidProperty.angular_damp);
            float linearDamp  = body.LinearDamping;
            float angularDamp = body.AngularDamping;

            if (superProperty.kinematic)
            {
                body.CollisionFlags = body.CollisionFlags | CollisionFlags.KinematicObject;
            }
            body.ActivationState = ActivationState.DisableDeactivation;
            dynamicsWorld.AddRigidBody(body, superProperty.group, superProperty.mask);
            return(body);
        }
Example #7
0
        public static Collider CreateDynamicCollider(CollisionShape shape, float mass, Matrix startTransform = default)
        {
            Vector3   localInertia = shape.CalculateLocalInertia(mass);
            RigidBody body         = CreateBody(mass, startTransform, shape, localInertia);

            return(Collider.Create(body));
        }
Example #8
0
        private RigidBody CreateRigidBody(float mass, Matrix4 startTransform, CollisionShape shape)
        {
            bool isDynamic = (mass != 0.0f);

            Vector3 localInertia = Vector3.Zero;

            if (isDynamic)
            {
                shape.CalculateLocalInertia(mass, out localInertia);
            }

            DefaultMotionState myMotionState = new DefaultMotionState(startTransform);

            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);
            RigidBody body = new RigidBody(rbInfo);


            body.SetSleepingThresholds(0, 0);
            body.ContactProcessingThreshold = 0;
            body.CcdMotionThreshold         = 0;

            //World.AddRigidBody(body);

            return(body);
        }
Example #9
0
        public virtual RigidBody LocalCreateRigidBody(float mass, Matrix startTransform, CollisionShape shape, bool isKinematic = false)
        {
            //rigidbody is dynamic if and only if mass is non zero, otherwise static
            bool isDynamic = (mass != 0.0f);

            Vector3 localInertia = Vector3.Zero;

            if (isDynamic)
            {
                shape.CalculateLocalInertia(mass, out localInertia);
            }

            //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
            DefaultMotionState myMotionState = new DefaultMotionState(startTransform);

            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);
            RigidBody body = new RigidBody(rbInfo);

            if (isKinematic)
            {
                body.CollisionFlags  = body.CollisionFlags | CollisionFlags.KinematicObject;
                body.ActivationState = ActivationState.DisableDeactivation;
            }
            rbInfo.Dispose();

            _world.AddRigidBody(body);

            return(body);
        }
        public override RigidBody LocalCreateRigidBody(float mass, Matrix startTransform, CollisionShape shape, bool isKinematic = false)
        {
            //rigidbody is dynamic if and only if mass is non zero, otherwise static
            bool isDynamic = (mass != 0.0f);

            Vector3 localInertia = Vector3.Zero;

            if (isDynamic)
            {
                shape.CalculateLocalInertia(mass, out localInertia);
            }

            //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects

            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, null, shape, localInertia);
            RigidBody body = new RigidBody(rbInfo);

            rbInfo.Dispose();
            body.ContactProcessingThreshold = defaultContactProcessingThreshold;
            body.WorldTransform             = startTransform;

            World.AddRigidBody(body);

            return(body);
        }
Example #11
0
        public RigidBody LocalCreateRigidBody(float mass, Matrix startTransform, CollisionShape shape)
        {
            collisionShapes.Add(shape);

            bool isDynamic = (mass != 0.0f);

            Vector3 localInertia = Vector3.Zero;

            if (isDynamic)
            {
                shape.CalculateLocalInertia(mass, out localInertia);
            }

            DefaultMotionState myMotionState = new DefaultMotionState(startTransform);

            RigidBody body;

            using (var rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia))
            {
                body = new RigidBody(rbInfo);
            }

            World.AddRigidBody(body);

            return(body);
        }
        // Cutting
        public RigidBody LocalCreateRigidBody(float mass, BulletSharp.Math.Vector3 startpos, CollisionShape shape, bool isKinematic = false)
        {
            //rigidbody is dynamic if and only if mass is non zero, otherwise static
            bool isDynamic = (mass != 0.0f);

            BulletSharp.Math.Vector3 localInertia = BulletSharp.Math.Vector3.Zero;
            if (isDynamic)
            {
                shape.CalculateLocalInertia(mass, out localInertia);
            }

            //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
            BulletSharp.Math.Matrix matrixtrans = BulletSharp.Math.Matrix.Identity;
            matrixtrans.Origin = startpos;
            DefaultMotionState myMotionState = new DefaultMotionState(matrixtrans);

            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);
            RigidBody body = new RigidBody(rbInfo);

            if (isKinematic)
            {
                body.CollisionFlags  = body.CollisionFlags | BulletSharp.CollisionFlags.KinematicObject;
                body.ActivationState = ActivationState.DisableDeactivation;
            }
            rbInfo.Dispose();

            return(body);
        }
Example #13
0
        protected override void Initialize()
        {
            base.Initialize();

            // collision configuration contains default setup for memory, collision setup
            CollisionConf = new DefaultCollisionConfiguration();
            Dispatcher    = new CollisionDispatcher(CollisionConf);

            Broadphase = new DbvtBroadphase();

            World             = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf);
            World.Gravity     = new Vector3(0, -10, 0);
            World.DebugDrawer = DebugDrawer;

            // create the ground
            _groundShape = new BoxShape(50, 1, 50);
            LoadModel("ground", _groundShape);
            CollisionShapes.Add(_groundShape);
            CollisionObject ground = LocalCreateRigidBody(0, Matrix.Identity, _groundShape);

            // create a few dynamic rigidbodies
            const float mass = 1.0f;

            _boxShape = new BoxShape(1);
            LoadModel("cube", _boxShape);
            CollisionShapes.Add(_boxShape);

            var rbInfo = new RigidBodyConstructionInfo(mass, null, _boxShape);

            rbInfo.LocalInertia = _boxShape.CalculateLocalInertia(mass);

            for (int k = 0; k < ArraySizeY; k++)
            {
                for (int i = 0; i < ArraySizeX; i++)
                {
                    for (int j = 0; j < ArraySizeZ; j++)
                    {
                        Matrix startTransform = Matrix.CreateTranslation(
                            _start + new Vector3(2 * i, 2 * k, 2 * j));

                        // using motionstate is recommended, it provides interpolation capabilities
                        // and only synchronizes 'active' objects
                        rbInfo.MotionState = new DefaultMotionState(startTransform);

                        var body = new RigidBody(rbInfo);

                        // make it drop from a height
                        body.Translate(new Vector3(0, 20, 0));

                        World.AddRigidBody(body);
                    }
                }
            }

            rbInfo.Dispose();

            FreeLook.SetEyeTarget(ref _eye, ref _target);
        }
Example #14
0
        /**
         * Creates or configures a RigidBody based on the current settings. Does not alter the internal state of this component in any way.
         * Can be used to create copies of this BRigidBody for use in other physics simulations.
         */
        public virtual bool CreateMultiBody(ref MultiBody mb, ref BulletSharp.Math.Vector3 localInertia, CollisionShape cs)
        {
            localInertia = BulletSharp.Math.Vector3.Zero;

            if (_mass > 0)
            {
                cs.CalculateLocalInertia(_mass, out localInertia);
            }

            if (mb == null)
            {
                int nbLinks = Links.Count;
                foreach (BMultiBodyLink link in Links)
                {
                    nbLinks += link.NbLinks;
                }

                Debug.Log("Adding multibody with " + nbLinks + " links");
                mb = new MultiBody(nbLinks, _mass, localInertia, false, false);
                mb.BaseWorldTransform = transform.localToWorldMatrix.ToBullet();
                mb.HasSelfCollision   = SelfCollision;
                var collider = new MultiBodyLinkCollider(mb, -1);
                collider.CollisionShape = cs;
                collider.WorldTransform = transform.localToWorldMatrix.ToBullet();
                collider.CollisionFlags = collisionFlags;
                collider.UserObject     = UserObject ?? this;
                BPhysicsWorld.Get().world.AddCollisionObject(collider, groupsIBelongTo, collisionMask);
                mb.BaseCollider   = collider;
                m_collisionObject = collider;

                BulletMultiBodyLinkColliderProxy baseProxy = gameObject.GetComponent <BulletMultiBodyLinkColliderProxy>();
                if (baseProxy == null)
                {
                    baseProxy = gameObject.AddComponent <BulletMultiBodyLinkColliderProxy>();
                }
                baseProxy.target = collider;

                try
                {
                    int currentLinkIndex = 0;
                    for (int i = 0; i < Links.Count; ++i)
                    {
                        currentLinkIndex += Links[i].AddLinkToMultiBody(this, currentLinkIndex, -1, transform);
                    }

                    mb.FinalizeMultiDof();
                }
                catch (Exception e) // if an error occurs, don't add the object, otherwise unity will crash
                {
                    Debug.LogErrorFormat("Error occured while setting MultiBody : {0}\n{1}", e.Message, e.StackTrace);
                    BPhysicsWorld.Get().world.RemoveCollisionObject(collider);
                    return(false);
                }
                mb.CanSleep   = false;
                mb.UserObject = UserObject ?? this;
            }
            return(true);
        }
Example #15
0
        // ------- Métodos Privados -------

        protected RigidBody CreateChassisRigidBodyFromShape(CollisionShape compound, TGCVector3 position, float rotation)
        {
            //since it is dynamic, we calculate its local inertia
            var localInertia = compound.CalculateLocalInertia(mass);

            var transformationMatrix = TGCMatrix.RotationYawPitchRoll(FastMath.PI + rotation, 0, 0).ToBsMatrix;

            transformationMatrix.Origin = position.ToBsVector;
            DefaultMotionState motionState = new DefaultMotionState(transformationMatrix);
            var bodyInfo  = new RigidBodyConstructionInfo(mass, motionState, compound, localInertia);
            var rigidBody = new RigidBody(bodyInfo);

            return(rigidBody);
        }
        public CollisionShape GetShape(ShapeCustomData sc)
        {
            CollisionShape shape = this.CreateShape();

            shape.LocalScaling = this.Scaling;
            sc.CustomString    = this.CustomString;
            if (sc.CustomObject != null)
            {
                sc.CustomObject = (ICloneable)this.CustomObject.Clone();
            }
            shape.UserObject = sc;
            shape.CalculateLocalInertia(this.Mass);

            return(shape);
        }
Example #17
0
        public RigidBody LocalCreateRigidBody(float mass, Matrix startTransform, CollisionShape shape)
        {
            using (var rbInfo = new RigidBodyConstructionInfo(mass, null, shape))
            {
                bool isDynamic = mass != 0.0f;
                if (isDynamic)
                {
                    rbInfo.LocalInertia = shape.CalculateLocalInertia(mass);
                    rbInfo.MotionState  = new DefaultMotionState(startTransform);
                }

                var body = new RigidBody(rbInfo);
                World.AddRigidBody(body);
                return(body);
            }
        }
Example #18
0
        public override RigidBody LocalCreateRigidBody(float mass, Matrix startTransform, CollisionShape shape)
        {
            //rigidbody is dynamic if and only if mass is non zero, otherwise static
            bool    isDynamic    = (mass != 0.0f);
            Vector3 localInertia = isDynamic ? shape.CalculateLocalInertia(mass) : Vector3.Zero;

            using (var rbInfo = new RigidBodyConstructionInfo(mass, null, shape, localInertia))
            {
                var body = new RigidBody(rbInfo)
                {
                    ContactProcessingThreshold = defaultContactProcessingThreshold,
                    WorldTransform             = startTransform
                };
                World.AddRigidBody(body);
                return(body);
            }
        }
Example #19
0
        /// <summary>
        /// Creates <see cref="CharacterPhysics"/>.
        /// </summary>
        /// <param name="c">Entity of character. No restrictions.</param>
        public CharacterPhysics(Entity c)
        {
            MotionState    = new EntityMotionState(c, false);
            CollisionShape = new SphereShape(2.0f);
            Vector3 inertia;

            CollisionShape.CalculateLocalInertia(50.0f, out inertia);

            Body = new RigidBody(new RigidBodyConstructionInfo(50.0f, MotionState, CollisionShape, inertia))
            {
                Friction        = 2.5f,
                Gravity         = new Vector3(0f, -9.8f, 0f),
                CollisionFlags  = CollisionFlags.None,
                ActivationState = ActivationState.ActiveTag,
                AngularFactor   = new Vector3(0, 0.0f, 0),
            };
            Body.SetSleepingThresholds(0, 0);
        }
Example #20
0
        public RigidBody AddRigidBody(float mass, Matrix4 startTransform, CollisionShape shape)
        {
            // Rigidbody is dynamic if and only if mass is non zero, otherwise static
            var isDynamic    = (mass != 0.0f);
            var localInertia = Vector3.Zero.ToBullet();

            if (isDynamic)
            {
                shape.CalculateLocalInertia(mass, out localInertia);
            }
            // Using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
            var myMotionState = new DefaultMotionState(startTransform.ToBullet());
            var rbInfo        = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);
            var body          = new RigidBody(rbInfo);

            rbInfo.Dispose();
            _physicsWorld.AddRigidBody(body);
            return(body);
        }
Example #21
0
        protected override void ProcessObject(RigidBody obj, int slice)
        {
            if (obj.CollisionShape.IsCompound && FWorld.PluginIO.IsConnected)
            {
                CompoundShape  comp = (CompoundShape)obj.CollisionShape;
                BodyCustomData bc   = (BodyCustomData)obj.UserObject;
                bc.MarkedForDeletion = true;


                for (int i = 0; i < comp.ChildList.Count; i++)
                {
                    CollisionShape shape = comp.GetChildShape(i);

                    float mass      = 1.0f / obj.InvMass;
                    float massshape = mass / (float)comp.ChildList.Count;

                    Vector3 inert;
                    shape.CalculateLocalInertia(massshape, out inert);

                    Matrix      m  = obj.MotionState.WorldTransform;
                    MotionState ms = new DefaultMotionState(m);
                    //List<RigidBody> bodies = new List<RigidBody>();

                    RigidBody rb = new RigidBody(new RigidBodyConstructionInfo(mass, ms, shape, inert));
                    rb.LinearVelocity  = obj.LinearVelocity;
                    rb.AngularVelocity = obj.AngularVelocity;
                    rb.Restitution     = obj.Restitution;
                    rb.Friction        = obj.Friction;
                    //rb.CollisionShape = shape;

                    BodyCustomData copy = new BodyCustomData();
                    copy.Id     = this.FWorld[0].GetNewBodyId();
                    copy.Custom = bc.Custom;

                    rb.UserObject = copy;

                    this.FWorld[0].Register(rb);

                    //bodies.Add(rb);
                }
            }
        }
Example #22
0
        RigidBody bulletCreateRigidBody(float mass, BulletSharp.Matrix startTransform, CollisionShape shape)
        {
            bool isDynamic = (mass != 0.0f);

            Vector3 localInertia = Vector3.Zero;

            if (isDynamic)
            {
                shape.CalculateLocalInertia(mass, out localInertia);
            }

            DefaultMotionState motionState = new DefaultMotionState(startTransform);

            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, motionState, shape, localInertia);
            RigidBody body = new RigidBody(rbInfo);

            bulletWorld.AddRigidBody(body);

            return(body);
        }
Example #23
0
        public BulletSharp.RigidBody LocalCreateRigidBody(float mass, Matrix4 startTransform, CollisionShape shape)
        {
            bool isDynamic = (mass != 0.0f);

            Vector3 localInertia = Vector3.Zero;

            if (isDynamic || _Static == false)
            {
                shape.CalculateLocalInertia(mass, out localInertia);
            }

            DefaultMotionState myMotionState = new DefaultMotionState(startTransform);

            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);

            BulletSharp.RigidBody body = new BulletSharp.RigidBody(rbInfo);

            Physics.AddRigidBody(body);

            return(body);
        }
Example #24
0
        /// <summary>
        /// Creates new rigid body and adds it to the world.
        /// </summary>
        /// <param name="mass">Body mass, if 0 body is static.</param>
        /// <param name="startTransform">Starting body transform.</param>
        /// <param name="shape">Body shape.</param>
        /// <returns>Created rigid body.</returns>
        public RigidBody CreateRigidBody(float mass, Matrix startTransform, CollisionShape shape)
        {
            //rigidbody is dynamic if and only if mass is non zero, otherwise static
            bool isDynamic = (mass != 0.0f);

            Vector3 localInertia = new Vector3();

            if (isDynamic)
            {
                shape.CalculateLocalInertia(mass, out localInertia);
            }

            //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects

            DefaultMotionState myMotionState = new DefaultMotionState(startTransform, Matrix.Identity);
            RigidBody          body          = new RigidBody(mass, myMotionState, shape, localInertia, 0.0f, 0.0f, 0.5f, 0.0f);

            _world.AddRigidBody(body);

            return(body);
        }
Example #25
0
        public static RigidBody CreateBoneRigidbody(float mass, ref Matrix boneTrans, ref Matrix rigidTrans, CollisionShape shape)
        {
            // A dynamic body with zero mass is invalid
            if (mass == 0)
            {
                throw new ArgumentException("{0} can not be zero.", nameof(mass));
            }

            // Using a motion state is recommended,
            // it holds the offset between bone and rigidbody
            var myMotionState = new BoneMotionState(boneTrans, rigidTrans);

            Vector3 localInertia = shape.CalculateLocalInertia(mass);

            RigidBody body;

            using (var rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia))
            {
                body = new RigidBody(rbInfo);
            }
            return(body);
        }
Example #26
0
        public RigidBody 剛体を作成して返す(CollisionShape 剛体の形, Matrix 剛体のワールド変換行列, 剛体物性 剛体の物性, 物理演算を超越した特性 物理演算を超越した特性)
        {
            var mass = (物理演算を超越した特性.物理演算の影響を受けないKinematic剛体である) ? 0 : 剛体の物性.質量;

            this._CollisionShapes.Add(剛体の形);

            var localInertia = new BulletSharp.Math.Vector3(0, 0, 0);

            if (mass != 0)
            {
                剛体の形.CalculateLocalInertia(mass, out localInertia);
            }

            var motionState = new DefaultMotionState(剛体のワールド変換行列.ToBulletSharp());
            var rbInfo      = new RigidBodyConstructionInfo(mass, motionState, 剛体の形, localInertia);

            var body = new RigidBody(rbInfo)
            {
                Restitution = 剛体の物性.反発係数,
                Friction    = 剛体の物性.摩擦係数,
            };

            body.SetDamping(剛体の物性.移動減衰係数, 剛体の物性.回転減衰係数);

            float linearDamp  = body.LinearDamping;
            float angularDamp = body.AngularDamping;

            if (物理演算を超越した特性.物理演算の影響を受けないKinematic剛体である)
            {
                body.CollisionFlags = body.CollisionFlags | CollisionFlags.KinematicObject;
            }

            body.ActivationState = ActivationState.DisableDeactivation;

            this._DynamicsWorld.AddRigidBody(body, 物理演算を超越した特性.自身の衝突グループ番号, 物理演算を超越した特性.自身と衝突する他の衝突グループ番号);

            return(body);
        }
Example #27
0
    // Creates a rigid body from the given shape and adds it to the Unity scene.
    protected RigidBody CreateRigidBody(float mass, Matrix startTransform, CollisionShape shape, Material renderMat, float friction = 0.5f, bool isKinematic = false, bool viz = false)
    {
        //rigidbody is dynamic if and only if mass is non zero, otherwise static
        bool isDynamic = (mass != 0.0f);

        BulletSharp.Math.Vector3 localInertia = BulletSharp.Math.Vector3.Zero;
        if (isDynamic)
        {
            shape.CalculateLocalInertia(mass, out localInertia);
        }

        //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
        DefaultMotionState myMotionState = new DefaultMotionState(startTransform);

        RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);

        rbInfo.Friction = friction;
        RigidBody body = new RigidBody(rbInfo);

        if (isKinematic)
        {
            body.CollisionFlags  = body.CollisionFlags | BulletSharp.CollisionFlags.KinematicObject;
            body.ActivationState = ActivationState.DisableDeactivation;
        }
        rbInfo.Dispose();

        m_world.AddRigidBody(body);

        // create unity object from it
        if (viz)
        {
            AddUnityObject(body, renderMat);
        }

        return(body);
    }
Example #28
0
        public Physics()
        {
            // collision configuration contains default setup for memory, collision setup
            collisionConf = new DefaultCollisionConfiguration();
            dispatcher    = new CollisionDispatcher(collisionConf);

            broadphase    = new DbvtBroadphase();
            World         = new DiscreteDynamicsWorld(dispatcher, broadphase, null, collisionConf);
            World.Gravity = new Vector3(0, -10, 0);

            // create the ground
            CollisionShape groundShape = new BoxShape(50, 50, 50);

            collisionShapes.Add(groundShape);
            ground            = LocalCreateRigidBody(0, Matrix4.CreateTranslation(0, -50, 0), groundShape);
            ground.UserObject = "Ground";

            // create a few dynamic rigidbodies
            const float mass = 1.0f;

            boxColShape = new BoxShape(1);

            collisionShapes.Add(boxColShape);
            Vector3 localInertia = boxColShape.CalculateLocalInertia(mass);

            var rbInfo = new RigidBodyConstructionInfo(mass, null, boxColShape, localInertia);

            int boxNumber = 0;

            for (float curX = fromX; curX < toX; curX += stepX)
            {
                int curXOffset = boxNumber / FunctionZWidth * functionDensity;
                for (int z = 0; z < FunctionZWidth; ++z)
                {
                    Vector3 boxPos         = new Vector3(curX + curXOffset, (float)Math.Cos(functionPeriod * curX) * functionAmplitude + functionOffsetY, z);
                    Matrix4 startTransform = Matrix4.CreateTranslation(boxPos);

                    // using motionstate is recommended, it provides interpolation capabilities
                    // and only synchronizes 'active' objects
                    rbInfo.MotionState = new DefaultMotionState(startTransform);

                    RigidBody body = new RigidBody(rbInfo);

                    // make it drop from a height
                    // body.Translate(new Vector3(0, 0, 0));

                    World.AddRigidBody(body);
                    body.Gravity = Vector3.Zero;
                    functionBoxes.Add(body);
                    boxNumber++;
                }
            }
            timer           = new Timer(100);
            timer.Elapsed  += Timer_Elapsed;
            timer.AutoReset = true;
            timer.Enabled   = true;



            rbInfo.Dispose();
        }
Example #29
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);
        }
Example #30
0
        /**
         * Creates or configures a RigidBody based on the current settings. Does not alter the internal state of this component in any way.
         * Can be used to create copies of this BRigidBody for use in other physics simulations.
         */
        public bool CreateOrConfigureRigidBody(ref RigidBody rb, ref BulletSharp.Math.Vector3 localInertia, CollisionShape cs, MotionState motionState)
        {
            //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 (rb == null)
            {
                float bulletMass = _mass;
                if (!isDynamic())
                {
                    bulletMass = 0f;
                }
                RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(bulletMass, motionState, cs, localInertia);
                rbInfo.Friction                             = _friction;
                rbInfo.RollingFriction                      = _rollingFriction;
                rbInfo.LinearDamping                        = _linearDamping;
                rbInfo.AngularDamping                       = _angularDamping;
                rbInfo.Restitution                          = _restitution;
                rbInfo.LinearSleepingThreshold              = _linearSleepingThreshold;
                rbInfo.AngularSleepingThreshold             = _angularSleepingThreshold;
                rbInfo.AdditionalDamping                    = _additionalDamping;
                rbInfo.AdditionalAngularDampingFactor       = _additionalAngularDampingFactor;
                rbInfo.AdditionalAngularDampingThresholdSqr = _additionalAngularDampingThresholdSqr;
                rbInfo.AdditionalDampingFactor              = _additionalDampingFactor;
                rbInfo.AdditionalLinearDampingThresholdSqr  = _additionalLinearDampingThresholdSqr;
                rb = new RigidBody(rbInfo);
                rbInfo.Dispose();
            }
            else
            {
                float usedMass = 0f;
                if (isDynamic())
                {
                    usedMass = _mass;
                }
                rb.SetMassProps(usedMass, localInertia);
                rb.Friction        = _friction;
                rb.RollingFriction = _rollingFriction;
                rb.SetDamping(_linearDamping, _angularDamping);
                rb.Restitution = _restitution;
                rb.SetSleepingThresholds(_linearSleepingThreshold, _angularSleepingThreshold);
                rb.CollisionShape = cs;
            }

            rb.AngularVelocity = angularVelocity.ToBullet();
            rb.LinearVelocity  = velocity.ToBullet();

            rb.CollisionFlags = m_collisionFlags;
            rb.LinearFactor   = _linearFactor.ToBullet();
            rb.AngularFactor  = _angularFactor.ToBullet();
            if (m_rigidBody != null)
            {
                rb.DeactivationTime             = m_rigidBody.DeactivationTime;
                rb.InterpolationLinearVelocity  = m_rigidBody.InterpolationLinearVelocity;
                rb.InterpolationAngularVelocity = m_rigidBody.InterpolationAngularVelocity;
                rb.InterpolationWorldTransform  = m_rigidBody.InterpolationWorldTransform;
            }

            //if kinematic then disable deactivation
            if ((m_collisionFlags & BulletSharp.CollisionFlags.KinematicObject) != 0)
            {
                rb.ActivationState = ActivationState.DisableDeactivation;
            }
            return(true);
        }