Exemplo n.º 1
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);
        }
Exemplo n.º 2
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;
        }
        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;

        }