Пример #1
0
        protected override void OnDetach()
        {
            MotionState.Dispose();
            MotionState.Clear();

            if (NativeCollisionObject == null)
            {
                return;
            }

            //Remove constraints safely
            var toremove = new FastList <Constraint>();

            foreach (var c in LinkedConstraints)
            {
                toremove.Add(c);
            }

            foreach (var disposable in toremove)
            {
                disposable.Dispose();
            }

            LinkedConstraints.Clear();
            //~Remove constraints

            Simulation.RemoveRigidBody(this);

            InternalRigidBody = null;

            base.OnDetach();
        }
Пример #2
0
        protected override void OnAttach()
        {
            InternalRigidBody = new BulletSharp.RigidBody(0.0f, MotionState, ColliderShape.InternalShape, Vector3.Zero)
            {
                UserObject = this
            };

            NativeCollisionObject = InternalRigidBody;

            NativeCollisionObject.ContactProcessingThreshold = !Simulation.CanCcd ? 1e18f : 1e30f;

            if (ColliderShape.NeedsCustomCollisionCallback)
            {
                NativeCollisionObject.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback;
            }

            if (ColliderShape.Is2D) //set different defaults for 2D shapes
            {
                InternalRigidBody.LinearFactor  = new Vector3(1.0f, 1.0f, 0.0f);
                InternalRigidBody.AngularFactor = new Vector3(0.0f, 0.0f, 1.0f);
            }

            var inertia = ColliderShape.InternalShape.CalculateLocalInertia(mass);

            InternalRigidBody.SetMassProps(mass, inertia);
            InternalRigidBody.UpdateInertiaTensor(); //this was the major headache when I had to debug Slider and Hinge constraint

            base.OnAttach();

            Mass            = mass;
            LinearDamping   = linearDamping;
            AngularDamping  = angularDamping;
            OverrideGravity = overrideGravity;
            Gravity         = gravity;
            RigidBodyType   = IsKinematic ? RigidBodyTypes.Kinematic : RigidBodyTypes.Dynamic;

            GetWorldTransformCallback = (out Matrix transform) => RigidBodyGetWorldTransform(out transform);
            SetWorldTransformCallback = transform => RigidBodySetWorldTransform(ref transform);

            UpdatePhysicsTransformation(); //this will set position and rotation of the collider

            if (IsDefaultGroup)
            {
                Simulation.AddRigidBody(this, CollisionFilterGroupFlags.DefaultFilter, CollisionFilterGroupFlags.AllFilter);
            }
            else
            {
                Simulation.AddRigidBody(this, (CollisionFilterGroupFlags)CollisionGroup, CanCollideWith);
            }
        }
Пример #3
0
        protected override void OnAttach()
        {
            if (NativeCollisionObject == null)
            {
                MotionState = new XenkoMotionState(this);

                SetupBoneLink();

                var rbci = new BulletSharp.RigidBodyConstructionInfo(0.0f, MotionState, ColliderShape.InternalShape, Vector3.Zero);
                InternalRigidBody = new BulletSharp.RigidBody(rbci)
                {
                    UserObject = this,
                };

                NativeCollisionObject = InternalRigidBody;

                NativeCollisionObject.ContactProcessingThreshold = !Simulation.CanCcd ? 1e18f : 1e30f;
            }

            if (ColliderShape.NeedsCustomCollisionCallback)
            {
                NativeCollisionObject.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback;
            }

            if (ColliderShape.Is2D) //set different defaults for 2D shapes
            {
                InternalRigidBody.LinearFactor  = new Vector3(1.0f, 1.0f, 0.0f);
                InternalRigidBody.AngularFactor = new Vector3(0.0f, 0.0f, 1.0f);
            }

            var inertia = ColliderShape.InternalShape.CalculateLocalInertia(mass);

            InternalRigidBody.SetMassProps(mass, inertia);
            InternalRigidBody.UpdateInertiaTensor(); //this was the major headache when I had to debug Slider and Hinge constraint

            base.OnAttach();

            Mass            = mass;
            LinearDamping   = linearDamping;
            AngularDamping  = angularDamping;
            OverrideGravity = overrideGravity;
            Gravity         = gravity;
            RigidBodyType   = type;

            Simulation.AddRigidBody(this, (CollisionFilterGroupFlags)CollisionGroup, CanCollideWith);
        }
Пример #4
0
        public void InitializeWorld()
        {
            mGameObjects = new List <NetGameObject>();
            //mWorldMap = new WorldMap();

#if _USE_BEPU_PHYSICS
            space = new BEPUphysics.Space();
            space.ForceUpdater.Gravity = new BEPUutilities.Vector3(0, -9.81f, 0);
#elif _USE_BULLET_SHARP
            Broadphase = new BulletSharp.AxisSweep3(new BulletSharp.Math.Vector3(-1000, -1000, -1000), new BulletSharp.Math.Vector3(1000, 1000, 1000));

            var conf       = new BulletSharp.DefaultCollisionConfiguration();
            var dispatcher = new BulletSharp.CollisionDispatcher(conf);
            world         = new BulletSharp.MultiBodyDynamicsWorld(dispatcher, Broadphase, new BulletSharp.MultiBodyConstraintSolver(), conf);
            world.Gravity = new BulletSharp.Math.Vector3(0, -9.81f, 0);

            var box = new BulletSharp.BoxShape(50f, 1f, 50f);
            //box.Margin = 0f;
            floor = new BulletSharp.RigidBody(new BulletSharp.RigidBodyConstructionInfo(0f, new BulletSharp.DefaultMotionState(), box, new BulletSharp.Math.Vector3(0, -2f, 0)));
            floor.CollisionFlags = BulletSharp.CollisionFlags.KinematicObject;


            BulletSharp.Math.Matrix worldTrans = new BulletSharp.Math.Matrix();
            worldTrans.M41       = 0f;
            worldTrans.M42       = -2f;
            worldTrans.M43       = 0;
            floor.WorldTransform = worldTrans;
            //floor.Friction = 0.5f;


            world.AddRigidBody(floor, BulletSharp.CollisionFilterGroups.DefaultFilter, //BulletSharp.CollisionFilterGroups.Everything);
                               (BulletSharp.CollisionFilterGroups.DefaultFilter
                                | BulletSharp.CollisionFilterGroups.StaticFilter
                                | BulletSharp.CollisionFilterGroups.KinematicFilter
                                | BulletSharp.CollisionFilterGroups.DebrisFilter
                                | BulletSharp.CollisionFilterGroups.SensorTrigger
                                | BulletSharp.CollisionFilterGroups.CharacterFilter
                               )
                               );
#elif _USE_BEPU_PHYSICS_V2
            characters = new BepuPhysics.CharacterControllers(BufferPool);
            Simulation = Simulation.Create(BufferPool, new BepuPhysics.CharacterNarrowphaseCallbacks(characters), new DemoPoseIntegratorCallbacks(new System.Numerics.Vector3(0, -10, 0)));

            Simulation.Statics.Add(new StaticDescription(new System.Numerics.Vector3(0, 0, 0), new CollidableDescription(Simulation.Shapes.Add(new Box(50, 1, 50)), 0.1f)));
#endif
        }
Пример #5
0
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                if ((Time.time - lastShotTime) < (1 / maxShotsPerSecond))
                {
                    return;
                }

                Vector3    camPos = Camera.main.transform.position;
                Quaternion camRot = Camera.main.transform.rotation;

                GameObject go = BSphere.CreateNew(camPos + new Vector3(0, 0, 2), camRot);

                BSphere bs = go.GetComponent <BSphere>();
                bs.meshSettings = meshSettings;
                bs.BuildMesh();

                lastShotTime = Time.time;

                //linVel.Normalize();
                BRigidBody bRb = go.GetComponent <BRigidBody>();
                bRb.mass = mass;

                BulletSharp.RigidBody rb = (BulletSharp.RigidBody)bRb.GetCollisionObject();

                rb.LinearVelocity             = (Camera.main.transform.forward * shootBoxInitialSpeed).ToBullet();
                rb.AngularVelocity            = BulletSharp.Math.Vector3.Zero;
                rb.ContactProcessingThreshold = 1e30f;

                go.GetComponent <MeshRenderer>().material.color =
                    new Color(UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f));

                Destroy(go, lifeTime);
            }
        }
Пример #6
0
        public void CreatePhysicsObjectBulletSharp()
        {
            BulletSharp.TriangleIndexVertexArray vertexArray = new BulletSharp.TriangleIndexVertexArray();
            BulletSharp.IndexedMesh mesh = new BulletSharp.IndexedMesh();

            mesh.Allocate(verts.Length, System.Runtime.InteropServices.Marshal.SizeOf(Vector3.Zero), (width - 1) * (height - 1) * 2, 3 * sizeof(int));
            BulletSharp.DataStream vData = mesh.LockVerts();

            Color[] md = new Color[verts.Length];
            heightMap.GetData <Color>(md);

            Vector3[] realVerts = new Vector3[verts.Length];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int idx = x + y * width;
                    realVerts[idx] = new Vector3(x, (md[idx].R / 256f) * 30f, y);
                }
            }

            for (int v = 0; v < realVerts.Length; v++)
            {
                vData.Write(realVerts[v].X);
                vData.Write(realVerts[v].Y);
                vData.Write(realVerts[v].Z);
            }

            indices = new int[(width - 1) * (height - 1) * 6];
            for (int x = 0; x < width - 1; x++)
            {
                for (int y = 0; y < height - 1; y++)
                {
                    indices[(x + y * (width - 1)) * 6]     = ((x + 1) + (y + 1) * width);
                    indices[(x + y * (width - 1)) * 6 + 1] = ((x + 1) + y * width);
                    indices[(x + y * (width - 1)) * 6 + 2] = (x + y * width);

                    indices[(x + y * (width - 1)) * 6 + 3] = ((x + 1) + (y + 1) * width);
                    indices[(x + y * (width - 1)) * 6 + 4] = (x + y * width);
                    indices[(x + y * (width - 1)) * 6 + 5] = (x + (y + 1) * width);
                }
            }



            BulletSharp.IntArray iData = mesh.TriangleIndices;
            for (int idx = 0; idx < indices.Length; idx++)
            {
                iData[idx] = indices[idx];
            }

            vertexArray.AddIndexedMesh(mesh);
            BulletSharp.CollisionShape            btTerrain = new BulletSharp.BvhTriangleMeshShape(vertexArray, true);
            BulletSharp.RigidBodyConstructionInfo rbInfo    =
                new BulletSharp.RigidBodyConstructionInfo(0, new BulletSharp.DefaultMotionState(Matrix.Identity), btTerrain, Vector3.Zero);

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

            bulletSharpRigidBody.Translate(Position);

            RigidBody = bulletSharpRigidBody;
        }
        protected override void OnAttach()
        {
            MotionState = new XenkoMotionState(this);

            SetupBoneLink();

            GetWorldTransformCallback = (out Matrix transform) => RigidBodyGetWorldTransform(out transform);
            SetWorldTransformCallback = transform => RigidBodySetWorldTransform(ref transform);

            InternalRigidBody = new BulletSharp.RigidBody(0.0f, MotionState, ColliderShape.InternalShape, Vector3.Zero)
            {
                UserObject = this
            };

            NativeCollisionObject = InternalRigidBody;

            NativeCollisionObject.ContactProcessingThreshold = !Simulation.CanCcd ? 1e18f : 1e30f;

            if (ColliderShape.NeedsCustomCollisionCallback)
            {
                NativeCollisionObject.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback;
            }

            if (ColliderShape.Is2D) //set different defaults for 2D shapes
            {
                InternalRigidBody.LinearFactor = new Vector3(1.0f, 1.0f, 0.0f);
                InternalRigidBody.AngularFactor = new Vector3(0.0f, 0.0f, 1.0f);
            }

            var inertia = ColliderShape.InternalShape.CalculateLocalInertia(mass);
            InternalRigidBody.SetMassProps(mass, inertia);
            InternalRigidBody.UpdateInertiaTensor(); //this was the major headache when I had to debug Slider and Hinge constraint

            base.OnAttach();

            Mass = mass;
            LinearDamping = linearDamping;
            AngularDamping = angularDamping;
            OverrideGravity = overrideGravity;
            Gravity = gravity;
            RigidBodyType = IsKinematic ? RigidBodyTypes.Kinematic : RigidBodyTypes.Dynamic;

            Simulation.AddRigidBody(this, (CollisionFilterGroupFlags)CollisionGroup, CanCollideWith);
        }
        public void CreatePhysicsObjectBulletSharp()
        {
            BulletSharp.TriangleIndexVertexArray vertexArray = new BulletSharp.TriangleIndexVertexArray();
            BulletSharp.IndexedMesh mesh = new BulletSharp.IndexedMesh();

            mesh.Allocate(verts.Length, System.Runtime.InteropServices.Marshal.SizeOf(Vector3.Zero), (width - 1) * (height - 1) * 2, 3 * sizeof(int));
            BulletSharp.DataStream vData = mesh.LockVerts();

            Color[] md = new Color[verts.Length];
            heightMap.GetData<Color>(md);

            Vector3[] realVerts = new Vector3[verts.Length];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int idx = x + y * width;
                    realVerts[idx] = new Vector3(x, (md[idx].R / 256f) * 30f, y);
                }
            }

            for (int v = 0; v < realVerts.Length; v++)
            {
                vData.Write(realVerts[v].X);
                vData.Write(realVerts[v].Y);
                vData.Write(realVerts[v].Z);
            }

            indices = new int[(width - 1) * (height - 1) * 6];
            for (int x = 0; x < width - 1; x++)
            {
                for (int y = 0; y < height - 1; y++)
                {
                    indices[(x + y * (width - 1)) * 6] = ((x + 1) + (y + 1) * width);
                    indices[(x + y * (width - 1)) * 6 + 1] = ((x + 1) + y * width);
                    indices[(x + y * (width - 1)) * 6 + 2] = (x + y * width);

                    indices[(x + y * (width - 1)) * 6 + 3] = ((x + 1) + (y + 1) * width);
                    indices[(x + y * (width - 1)) * 6 + 4] = (x + y * width);
                    indices[(x + y * (width - 1)) * 6 + 5] = (x + (y + 1) * width);
                }
            }



            BulletSharp.IntArray iData = mesh.TriangleIndices;
            for (int idx = 0; idx < indices.Length; idx++)
            {
                iData[idx] = indices[idx];
            }

            vertexArray.AddIndexedMesh(mesh);
            BulletSharp.CollisionShape btTerrain = new BulletSharp.BvhTriangleMeshShape(vertexArray, true);
            BulletSharp.RigidBodyConstructionInfo rbInfo =
                            new BulletSharp.RigidBodyConstructionInfo(0, new BulletSharp.DefaultMotionState(Matrix.Identity), btTerrain, Vector3.Zero);

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

            bulletSharpRigidBody.Translate(Position);

            RigidBody = bulletSharpRigidBody;

        }
Пример #9
0
 protected abstract Element CreateSpecificElement(TgcMesh mesh, BulletSharp.RigidBody rigidBody);