/// <summary>
        /// Se crea uncuerpo rigido a partir de un TgcMesh, pero no tiene masa por lo que va a ser estatico.
        /// </summary>
        /// <param name="mesh">TgcMesh</param>
        /// <returns>Cuerpo rigido de un Mesh</returns>
        public RigidBody CreateRigidBodyFromTgcMesh(TgcMesh mesh)
        {
            var vertexCoords = mesh.getVertexPositions();

            TriangleMesh triangleMesh = new TriangleMesh();

            for (int i = 0; i < vertexCoords.Length; i = i + 3)
            {
                triangleMesh.AddTriangle(vertexCoords[i].ToBulletVector3(), vertexCoords[i + 1].ToBulletVector3(), vertexCoords[i + 2].ToBulletVector3());
            }

            var transformationMatrix       = TGCMatrix.RotationYawPitchRoll(0, 0, 0).ToBsMatrix;
            DefaultMotionState motionState = new DefaultMotionState(transformationMatrix);

            var bulletShape     = new BvhTriangleMeshShape(triangleMesh, false);
            var boxLocalInertia = bulletShape.CalculateLocalInertia(0);

            var bodyInfo  = new RigidBodyConstructionInfo(0, motionState, bulletShape, boxLocalInertia);
            var rigidBody = new RigidBody(bodyInfo);

            rigidBody.Friction        = 0.4f;
            rigidBody.RollingFriction = 1;
            rigidBody.Restitution     = 1f;

            return(rigidBody);
        }
Exemple #2
0
        public override void Run()
        {
            var conf       = new DefaultCollisionConfiguration();
            var dispatcher = new CollisionDispatcher(conf);
            var broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000));

            world = new DiscreteDynamicsWorld(dispatcher, broadphase, null, conf);

            var indexVertexArray = new TriangleIndexVertexArray(TorusMesh.Indices, TorusMesh.Vertices);

            foreach (var indexedMesh in indexVertexArray.IndexedMeshArray)
            {
                indexedMesh.ToString();
            }
            AddToDisposeQueue(indexVertexArray);

            var     gImpactMesh = new GImpactMeshShape(indexVertexArray);
            Vector3 aabbMin, aabbMax;

            gImpactMesh.GetAabb(Matrix.Identity, out aabbMin, out aabbMax);
            CreateBody(1.0f, gImpactMesh, Vector3.Zero);
            AddToDisposeQueue(gImpactMesh);
            gImpactMesh = null;

            var triangleMesh = new BvhTriangleMeshShape(indexVertexArray, true);

            triangleMesh.CalculateLocalInertia(1.0f);
            triangleMesh.GetAabb(Matrix.Identity, out aabbMin, out aabbMax);
            CreateBody(1.0f, triangleMesh, Vector3.Zero);
            AddToDisposeQueue(triangleMesh);
            triangleMesh = null;

            indexVertexArray = null;


            AddToDisposeQueue(conf);
            AddToDisposeQueue(dispatcher);
            AddToDisposeQueue(broadphase);
            AddToDisposeQueue(world);

            //conf.Dispose();
            conf = null;
            //dispatcher.Dispose();
            dispatcher = null;
            //broadphase.Dispose();
            broadphase = null;
            for (int i = 0; i < 600; i++)
            {
                world.StepSimulation(1.0f / 60.0f);
            }
            world.Dispose();
            world = null;

            ForceGC();
            TestWeakRefs();
            ClearRefs();
        }
Exemple #3
0
        private RigidBody construirRigidBodyDeTriangleMeshShape(BvhTriangleMeshShape triangleMeshShape)
        {
            var transformationMatrix       = TGCMatrix.RotationYawPitchRoll(0, 0, 0).ToBsMatrix;
            DefaultMotionState motionState = new DefaultMotionState(transformationMatrix);

            var boxLocalInertia = triangleMeshShape.CalculateLocalInertia(0);
            var bodyInfo        = new RigidBodyConstructionInfo(0, motionState, triangleMeshShape, boxLocalInertia);
            var rigidBody       = new RigidBody(bodyInfo);

            rigidBody.Friction        = 0.4f;
            rigidBody.RollingFriction = 1;
            rigidBody.Restitution     = 1f;

            return(rigidBody);
        }
Exemple #4
0
        public void SetScene(OpenTK.Mathematics.Vector3[] mesh, int[] indexes, OpenTK.Mathematics.Matrix4 startTransform)
        {
            //convert openTK to BulentSharp
            var meshP = new Vector3[mesh.Length];

            for (int i = 0; i < mesh.Length; i++)
            {
                meshP[i] = mesh[i].Convert();
            }

            TriangleIndexVertexArray triangles = new TriangleIndexVertexArray(indexes, meshP);
            CollisionShape           shape     = new BvhTriangleMeshShape(triangles, true);

            shape.CalculateLocalInertia(0);
            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(0, new DefaultMotionState(startTransform.Convert()), shape, Vector3.Zero);
            RigidBody Body = new RigidBody(rbInfo);

            Body.CollisionFlags = CollisionFlags.StaticObject;
            Body.UserObject     = "Ground";
            Body.UserIndex      = 10;
            World.AddRigidBody(Body, CollisionFilterGroups.StaticFilter, CollisionFilterGroups.CharacterFilter);

            /*
             * const float staticMass = 0;
             * RigidBody body;
             * CollisionShape shape = new TriangleMesh()
             * Matrix groundTransform = Matrix.Translation(0, -0.5f, 0);
             * using (var rbInfo = new RigidBodyConstructionInfo(staticMass, null, shape)
             * {
             *  StartWorldTransform = groundTransform,
             * })
             * {
             *  body = new RigidBody(rbInfo);
             * }
             * World.AddRigidBody(body, CollisionFilterGroups.StaticFilter, CollisionFilterGroups.AllFilter);
             */
        }