public override void Update()
        {
            dynamicsWorld.StepSimulation(1 / 60f, 10);

            if (Ctx2.Input.keyUp(Key.A))
            {
                var ballBody = BulletRigidBodyConstructor.CreateBall(10f, 1f, new TGCVector3(0f, 100f, 0f));
                ballBodys.Add(ballBody);
                dynamicsWorld.AddRigidBody(ballBody);
            }

            if (Ctx2.Input.keyUp(Key.S))
            {
                var boxBody = BulletRigidBodyConstructor.CreateBox(new TGCVector3(10, 10, 10), 1f, new TGCVector3(5f, 150f, 5f), MathUtil.SIMD_HALF_PI, MathUtil.SIMD_QUARTER_PI, MathUtil.SIMD_2_PI, 0.3f);
                boxBodys.Add(boxBody);
                dynamicsWorld.AddRigidBody(boxBody);
            }

            if (Ctx2.Input.keyUp(Key.Space))
            {
                var ballBody = BulletRigidBodyConstructor.CreateBall(10, 1f, new TGCVector3(Ctx2.Camara.Position.X, Ctx2.Camara.Position.Y, Ctx2.Camara.Position.Z));
                ballBody.LinearVelocity = new TGCVector3(-Ctx2.Camara.Position.X, -Ctx2.Camara.Position.Y, -Ctx2.Camara.Position.Z).ToBsVector * 0.2f;
                ballBody.Restitution    = 0.9f;
                ballBodys.Add(ballBody);
                dynamicsWorld.AddRigidBody(ballBody);
            }
        }
        public void Init(String MediaDir)
        {
            //Creamos el mundo fisico por defecto.
            collisionConfiguration = new DefaultCollisionConfiguration();
            dispatcher             = new CollisionDispatcher(collisionConfiguration);
            GImpactCollisionAlgorithm.RegisterAlgorithm(dispatcher);
            constraintSolver      = new SequentialImpulseConstraintSolver();
            overlappingPairCache  = new DbvtBroadphase(); //AxisSweep3(new BsVector3(-5000f, -5000f, -5000f), new BsVector3(5000f, 5000f, 5000f), 8192);
            dynamicsWorld         = new DiscreteDynamicsWorld(dispatcher, overlappingPairCache, constraintSolver, collisionConfiguration);
            dynamicsWorld.Gravity = new TGCVector3(0, -100f, 0).ToBsVector;

            //Creamos el terreno
            var meshRigidBody = BulletRigidBodyConstructor.CreateSurfaceFromHeighMap(triangleDataVB);

            dynamicsWorld.AddRigidBody(meshRigidBody);

            //Creamos la esfera del dragon
            dragonBall = BulletRigidBodyConstructor.CreateBall(30f, 0.75f, new TGCVector3(100f, 500f, 100f));
            dragonBall.SetDamping(0.1f, 0.5f);
            dragonBall.Restitution = 1f;
            dragonBall.Friction    = 1;
            dynamicsWorld.AddRigidBody(dragonBall);
            var textureDragonBall = TgcTexture.createTexture(D3DDevice.Instance.Device, MediaDir + @"Texturas\dragonball.jpg");

            sphereMesh = new TGCSphere(1, textureDragonBall, TGCVector3.Empty);
            sphereMesh.updateValues();
            director = new TGCVector3(1, 0, 0);
        }
示例#3
0
        public override void Update()
        {
            dynamicsWorld.StepSimulation(1 / 60f, 10);

            if (Ctx.Input.keyUp(Key.Space))
            {
                var ballBody = BulletRigidBodyConstructor.CreateBall(10f, 1f, new TGCVector3(Ctx.Camara.Position.X, Ctx.Camara.Position.Y, Ctx.Camara.Position.Z));
                var dir      = new TGCVector3(Ctx.Camara.LookAt.X - Ctx.Camara.Position.X, Ctx.Camara.LookAt.Y - Ctx.Camara.Position.Y, Ctx.Camara.LookAt.Z - Ctx.Camara.Position.Z).ToBsVector;
                dir.Normalize();
                ballBody.LinearVelocity = dir * 900;
                ballBody.LinearFactor   = TGCVector3.One.ToBsVector;
                //ballBody.SetDamping(0.1f, 0.5f);
                //ballBody.Restitution = 0.5f;
                ballBodys.Add(ballBody);
                dynamicsWorld.AddRigidBody(ballBody);
            }

            if (Ctx.Input.keyUp(Key.Q))
            {
                var ballBody = BulletRigidBodyConstructor.CreateBall(10f, 10f, new TGCVector3(Ctx.Camara.Position.X, Ctx.Camara.Position.Y, Ctx.Camara.Position.Z));
                var dir      = new TGCVector3(Ctx.Camara.LookAt.X - Ctx.Camara.Position.X, Ctx.Camara.LookAt.Y - Ctx.Camara.Position.Y, Ctx.Camara.LookAt.Z - Ctx.Camara.Position.Z).ToBsVector;
                dir.Normalize();
                ballBody.LinearVelocity = dir * 900;
                ballBody.LinearFactor   = TGCVector3.One.ToBsVector;
                //ballBody.SetDamping(0.1f, 0.5f);
                //ballBody.Restitution = 0.5f;
                ballBodys.Add(ballBody);
                dynamicsWorld.AddRigidBody(ballBody);
            }

            if (Ctx.Input.keyUp(Key.W))
            {
                var ballBody = BulletRigidBodyConstructor.CreateBall(10f, 0.1f, new TGCVector3(Ctx.Camara.Position.X, Ctx.Camara.Position.Y, Ctx.Camara.Position.Z));
                var dir      = new TGCVector3(Ctx.Camara.LookAt.X - Ctx.Camara.Position.X, Ctx.Camara.LookAt.Y - Ctx.Camara.Position.Y, Ctx.Camara.LookAt.Z - Ctx.Camara.Position.Z).ToBsVector;
                dir.Normalize();
                ballBody.LinearVelocity = dir * 900;
                ballBody.LinearFactor   = TGCVector3.One.ToBsVector;
                //ballBody.SetDamping(0.1f, 0.1f);
                //ballBody.Restitution = 0.9f;
                ballBodys.Add(ballBody);
                dynamicsWorld.AddRigidBody(ballBody);
            }
        }
        public override void Init(BulletExample3 ctx)
        {
            base.Init(ctx);

            //Creamos shapes y bodies.

            //El piso es un plano estatico se dice que si tiene masa 0 es estatico.
            var floorShape       = new StaticPlaneShape(TGCVector3.Up.ToBsVector, 0);
            var floorMotionState = new DefaultMotionState();
            var floorInfo        = new RigidBodyConstructionInfo(0, floorMotionState, floorShape);

            floorBody = new RigidBody(floorInfo);
            dynamicsWorld.AddRigidBody(floorBody);

            var boxBody = BulletRigidBodyConstructor.CreateBox(new TGCVector3(10, 10, 10), 1f, new TGCVector3(10f, 100f, 10f), MathUtil.SIMD_HALF_PI, MathUtil.SIMD_QUARTER_PI, MathUtil.SIMD_2_PI, 0);

            boxBodys.Add(boxBody);
            dynamicsWorld.AddRigidBody(boxBody);

            var ballBody = BulletRigidBodyConstructor.CreateBall(10f, 1f, new TGCVector3(0f, 50f, 0f));

            ballBodys.Add(ballBody);
            dynamicsWorld.AddRigidBody(ballBody);

            //Cargamos objetos de render del framework.
            var floorTexture = TgcTexture.createTexture(D3DDevice.Instance.Device, Ctx2.MediaDir + @"Texturas\granito.jpg");

            floorMesh = new TgcPlane(new TGCVector3(-2000, 0, -2000), new TGCVector3(4000, 0f, 4000), TgcPlane.Orientations.XZplane, floorTexture);

            var texture = TgcTexture.createTexture(D3DDevice.Instance.Device, Ctx2.MediaDir + @"MeshCreator\Scenes\Deposito\Textures\boxMetal.jpg");

            //Es importante crear todos los mesh con centro en el 0,0,0 y que este coincida con el centro de masa definido caso contrario rotaria de otra forma diferente a la dada por el motor de fisica.
            boxMesh = TGCBox.fromSize(new TGCVector3(20, 20, 20), texture);

            texture = TgcTexture.createTexture(D3DDevice.Instance.Device, Ctx2.MediaDir + @"Texturas\pokeball.jpg");
            //Se crea una esfera de tamaño 1 para escalarla luego (en render)
            sphereMesh = new TGCSphere(1, texture, TGCVector3.Empty);
            //Tgc no crea el vertex buffer hasta invocar a update values.
            sphereMesh.updateValues();
        }
        public void Init(String MediaDir)
        {
            #region world configuration
            // Create a physics world using default config
            collisionConfiguration = new DefaultCollisionConfiguration();
            dispatcher             = new CollisionDispatcher(collisionConfiguration);
            GImpactCollisionAlgorithm.RegisterAlgorithm(dispatcher);
            constraintSolver     = new SequentialImpulseConstraintSolver();
            overlappingPairCache = new DbvtBroadphase();
            dynamicsWorld        = new DiscreteDynamicsWorld(dispatcher, overlappingPairCache, constraintSolver, collisionConfiguration);

            var gravity = new TGCVector3(0, -60f, 0).ToBsVector;
            dynamicsWorld.Gravity = gravity;

            if (UsingHeightmap)
            {
                var heightMap = BulletRigidBodyConstructor.CreateSurfaceFromHeighMap(triangleDataVB);
                heightMap.Restitution = 0;
                dynamicsWorld.AddRigidBody(heightMap);
            }
            #endregion
            float      radius       = 30f;
            float      mass         = 0.75f;
            var        centerOfMass = new TGCVector3(-50f, 30, -200f);
            TGCVector3 size         = TGCVector3.Empty;

            #region Stone Sphere
            ball = BulletRigidBodyConstructor.CreateBall(radius, mass, centerOfMass);
            ball.SetDamping(0.1f, 0.5f);
            ball.Restitution = 1f;
            ball.Friction    = 1;
            dynamicsWorld.AddRigidBody(ball);

            var ballTexture = TgcTexture.createTexture(D3DDevice.Instance.Device, $"{MediaDir}\\Textures\\rockwall.jpg");
            sphereMesh = new TGCSphere(radius, ballTexture, TGCVector3.Empty);
            sphereMesh.BoundingSphere.setValues(centerOfMass, radius);
            sphereMesh.updateValues();

            director = new TGCVector3(1, 0, 0);
            #endregion

            #region BandicootRigidBody
            //Cuerpo rigido de una caja basica
            var position = new TGCVector3(0, 1, 0);
            mass = 1.5f;
            bandicootRigidBody = BulletRigidBodyConstructor.CreateCapsule(10, 5, position, mass, false);

            //Valores que podemos modificar a partir del RigidBody base
            bandicootRigidBody.SetDamping(0.1f, 0f);
            bandicootRigidBody.Restitution         = 0f;
            bandicootRigidBody.Friction            = 0.5f;
            bandicootRigidBody.InvInertiaDiagLocal = TGCVector3.Empty.ToBsVector;
            //Agregamos el RidigBody al World
            dynamicsWorld.AddRigidBody(bandicootRigidBody);
            #endregion

            #region Stairs
            mass = 0;
            size = new TGCVector3(50, 20, 30);
            var   platformTexture = TgcTexture.createTexture(D3DDevice.Instance.Device, $"{MediaDir}\\textures\\rockwall.jpg");
            float angle           = 0.75f * FastMath.PI;

            for (float i = 0, x = size.Z, y = size.Y, z = -size.X; i < 10; i++)
            {
                staticPlatform = BulletRigidBodyConstructor.CreateBox(size, mass, centerOfMass, 0, 0, 0, 0.7f);
                staticPlatform.CenterOfMassTransform = TGCMatrix.RotationY(angle).ToBsMatrix *TGCMatrix.Translation(x, y, z).ToBsMatrix;
                dynamicsWorld.AddRigidBody(staticPlatform);

                staticPlatformMesh           = TGCBox.fromSize(2 * size, platformTexture);
                staticPlatformMesh.Transform = new TGCMatrix(staticPlatform.InterpolationWorldTransform);
                stairsMesh.Add(staticPlatformMesh);

                x     += 35;
                y     += 40;
                z     -= 25;
                angle -= 0.1f;
            }
            #endregion

            #region Dynamic Platform

            position        = new TGCVector3(0, 0, 0);
            mass            = 1f;
            size            = new TGCVector3(70, 10, 30);
            dynamicPlatform = BulletRigidBodyConstructor.CreateBox(size, mass, position, 0, 0, 0, 2f);
            dynamicPlatform.CenterOfMassTransform = TGCMatrix.Translation(-300, 60, -200).ToBsMatrix;
            dynamicPlatform.AngularFactor         = (new Vector3(0, 0, 0));
            dynamicsWorld.AddRigidBody(dynamicPlatform);

            // mesh para visualizar plataforma

            var platformtexture = TgcTexture.createTexture(D3DDevice.Instance.Device, $"{MediaDir}\\textures\\rockwall.jpg");
            dynamicPlatformMesh                     = TGCBox.fromSize(2 * size, platformtexture);
            dynamicPlatformMesh.Transform           = new TGCMatrix(dynamicPlatform.InterpolationWorldTransform);
            dynamicPlatformMesh.AutoTransformEnable = false;
            dynamicPlatformMesh.updateValues();

            #endregion
        }
        public void Init(String MediaDir)
        {
            #region Configuracion Basica de World

            //Creamos el mundo fisico por defecto.
            collisionConfiguration = new DefaultCollisionConfiguration();
            dispatcher             = new CollisionDispatcher(collisionConfiguration);
            GImpactCollisionAlgorithm.RegisterAlgorithm(dispatcher);
            constraintSolver      = new SequentialImpulseConstraintSolver();
            overlappingPairCache  = new DbvtBroadphase(); //AxisSweep3(new BsVector3(-5000f, -5000f, -5000f), new BsVector3(5000f, 5000f, 5000f), 8192);
            dynamicsWorld         = new DiscreteDynamicsWorld(dispatcher, overlappingPairCache, constraintSolver, collisionConfiguration);
            dynamicsWorld.Gravity = new TGCVector3(0, -100f, 0).ToBsVector;

            #endregion Configuracion Basica de World

            #region Capsula

            //Cuerpo rigido de una capsula basica
            capsuleRigidBody = BulletRigidBodyConstructor.CreateCapsule(10, 50, new TGCVector3(200, 500, 200), 10, false);

            //Valores que podemos modificar a partir del RigidBody base
            capsuleRigidBody.SetDamping(0.1f, 0f);
            capsuleRigidBody.Restitution = 0.1f;
            capsuleRigidBody.Friction    = 1;

            //Agregamos el RidigBody al World
            dynamicsWorld.AddRigidBody(capsuleRigidBody);

            #endregion Capsula

            #region Terreno

            //Creamos el RigidBody basico del Terreno
            var meshRigidBody = BulletRigidBodyConstructor.CreateSurfaceFromHeighMap(triangleDataVB);

            //Agregamos algo de friccion al RigidBody ya que este va a interactuar con objetos moviles
            //del World
            meshRigidBody.Friction = 0.5f;

            //Agregamos el RigidBody del terreno al World
            dynamicsWorld.AddRigidBody(meshRigidBody);

            #endregion Terreno

            #region Esfera

            //Creamos una esfera para interactuar
            pokeball = BulletRigidBodyConstructor.CreateBall(10f, 0.5f, new TGCVector3(100f, 500f, 100f));
            pokeball.SetDamping(0.1f, 0.5f);
            pokeball.Restitution = 1f;
            //Agregamos la pokebola al World
            dynamicsWorld.AddRigidBody(pokeball);

            //Textura de pokebola
            var texturePokeball = TgcTexture.createTexture(D3DDevice.Instance.Device, MediaDir + @"Texturas\pokeball.jpg");

            //Se crea una esfera de tamaño 1 para escalarla luego (en render)
            sphereMesh = new TGCSphere(1, texturePokeball, TGCVector3.Empty);

            //Tgc no crea el vertex buffer hasta invocar a update values.
            sphereMesh.updateValues();

            #endregion Esfera

            #region Personaje

            //Cargamos personaje
            var skeletalLoader = new TgcSkeletalLoader();
            personaje = skeletalLoader.loadMeshAndAnimationsFromFile(
                MediaDir + "SkeletalAnimations\\Robot\\Robot-TgcSkeletalMesh.xml",
                MediaDir + "SkeletalAnimations\\Robot\\",
                new[]
            {
                MediaDir + "SkeletalAnimations\\Robot\\Caminando-TgcSkeletalAnim.xml",
                MediaDir + "SkeletalAnimations\\Robot\\Parado-TgcSkeletalAnim.xml"
            });

            //Le cambiamos la textura para diferenciarlo un poco
            personaje.changeDiffuseMaps(new[]
            {
                TgcTexture.createTexture(D3DDevice.Instance.Device,
                                         MediaDir + "SkeletalAnimations\\Robot\\Textures\\uvwGreen.jpg")
            });

            //Configurar animacion inicial
            personaje.playAnimation("Parado", true);

            #endregion Personaje

            #region Cajas

            var sizeBox = 20f;

            //Textura de caja
            var textureBox = TgcTexture.createTexture(D3DDevice.Instance.Device, MediaDir + @"MeshCreator\Textures\Madera\cajaMadera2.jpg");

            box = BulletRigidBodyConstructor.CreateBox(new TGCVector3(sizeBox, sizeBox, sizeBox), 0, new TGCVector3(0, 12, 0), 0, 0, 0, 0.5f);
            dynamicsWorld.AddRigidBody(box);
            boxMesh = TGCBox.fromSize(new TGCVector3(40f, 40f, 40f), textureBox);
            boxMesh.updateValues();

            sizeBox = 40f;
            boxB    = BulletRigidBodyConstructor.CreateBox(new TGCVector3(sizeBox, sizeBox, sizeBox), 0, new TGCVector3(100, 40, 0), 0, 0, 0, 0.5f);
            dynamicsWorld.AddRigidBody(boxB);
            boxMeshB = TGCBox.fromSize(new TGCVector3(80f, 80f, 80f), textureBox);
            boxMeshB.updateValues();

            box45 = BulletRigidBodyConstructor.CreateBox(new TGCVector3(sizeBox, sizeBox, sizeBox), 0, new TGCVector3(200, 40, 0), BulletSharp.MathUtil.SIMD_QUARTER_PI, 0, 0, 0.5f);
            dynamicsWorld.AddRigidBody(box45);

            boxPush = BulletRigidBodyConstructor.CreateBox(new TGCVector3(sizeBox, sizeBox, sizeBox), 0.5f, new TGCVector3(-200, 60, 0), BulletSharp.MathUtil.SIMD_QUARTER_PI, 0, 0, 0.25f);
            dynamicsWorld.AddRigidBody(boxPush);

            boxMeshPush = TGCBox.fromSize(new TGCVector3(80f, 80f, 80f), textureBox);
            boxMeshPush.updateValues();

            #endregion Cajas

            #region Escalera

            var a             = 0;
            var textureStones = TgcTexture.createTexture(D3DDevice.Instance.Device, MediaDir + @"Texturas\stones.bmp");

            //la altura de cualquier cubo que quiera subir una capsula debe ser menor a la mitad del radio
            var size = new TGCVector3(50, 4, 20);
            escalon = TGCBox.fromSize(size, textureStones);

            //Se crean 10 escalonescd d
            while (a < 10)
            {
                escalonRigidBody = BulletRigidBodyConstructor.CreateBox(size, 0, new TGCVector3(200, a * 4 + 10, a * 20 + 100), 0, 0, 0, 0.1f);

                escalonesRigidBodies.Add(escalonRigidBody);

                dynamicsWorld.AddRigidBody(escalonRigidBody);

                a++;
            }

            #endregion Escalera

            #region Plataforma

            textureStones       = TgcTexture.createTexture(D3DDevice.Instance.Device, MediaDir + @"Texturas\cobblestone_quad.jpg");
            rigidBodyPlataforma = BulletRigidBodyConstructor.CreateBox(new TGCVector3(50f, 15f, 50f), 0, new TGCVector3(200, 42.5f, 315), 0, 0, 0, 0.5f);
            dynamicsWorld.AddRigidBody(rigidBodyPlataforma);
            plataforma = TGCBox.fromSize(new TGCVector3(50f, 15f, 50f), textureStones);
            plataforma.updateValues();

            #endregion Plataforma

            #region Columna

            columnaRigidBody = BulletRigidBodyConstructor.CreateCylinder(new TGCVector3(10, 50, 10), new TGCVector3(100, 50, 100), 0);
            dynamicsWorld.AddRigidBody(columnaRigidBody);
            var columnaLoader = new TgcSceneLoader();
            columnaMesh          = columnaLoader.loadSceneFromFile(MediaDir + @"MeshCreator\Meshes\Cimientos\PilarEgipcio\PilarEgipcio-TgcScene.xml", MediaDir + @"MeshCreator\Meshes\Cimientos\PilarEgipcio\").Meshes[0];
            columnaMesh.Position = new TGCVector3(100, 7.5f, 100);
            columnaMesh.UpdateMeshTransform();

            #endregion Columna

            director = new TGCVector3(0, 0, 1);
        }