Example #1
0
        private void _worker_DoWork(object sender, DoWorkEventArgs e)
        {
            var conf       = new DefaultCollisionConfiguration();
            var dispatcher = new CollisionDispatcher(conf);
            var broadphase = new DbvtBroadphase();
            var Solver     = new SequentialImpulseConstraintSolver();

            //var Solver = new MultiBodyConstraintSolver();

            world         = new DiscreteDynamicsWorld(dispatcher, broadphase, Solver, conf);
            world.Gravity = new Vector3(0, 0, 0);
            world.SolverInfo.NumIterations   = 120;
            world.SolverInfo.SolverMode      = SolverModes.RandomizeOrder;
            world.DispatchInfo.UseContinuous = true;
            CreateParticles();
            CreateWalls();
            SetupDoubleSlit();

            while (_worker.CancellationPending == false)
            {
                world.StepSimulation(1.0f / 60.0f);

                for (int i = 0; i < Photons.Count; i++)
                {
                    RigidBody r = Photons[i];
                    r.ApplyCentralImpulse(new Vector3(5, 0, 0));
                }
            }
        }
Example #2
0
        public void Update(TgcD3dInput input, float time)
        {
            dynamicsWorld.StepSimulation(1 / 60f, 100);
            var strength = 10.30f;
            var angle    = 5;

            #region Comoportamiento

            if (input.keyDown(Key.W))
            {
                //Activa el comportamiento de la simulacion fisica para la capsula
                capsuleRigidBody.ActivationState = ActivationState.ActiveTag;
                capsuleRigidBody.AngularVelocity = TGCVector3.Empty.ToBsVector;
                capsuleRigidBody.ApplyCentralImpulse(-strength * director.ToBsVector);
            }

            if (input.keyDown(Key.S))
            {
                //Activa el comportamiento de la simulacion fisica para la capsula
                capsuleRigidBody.ActivationState = ActivationState.ActiveTag;
                capsuleRigidBody.AngularVelocity = TGCVector3.Empty.ToBsVector;
                capsuleRigidBody.ApplyCentralImpulse(strength * director.ToBsVector);
            }

            if (input.keyDown(Key.A))
            {
                director.TransformCoordinate(TGCMatrix.RotationY(-angle * time));
                character.Transform             = TGCMatrix.Translation(TGCVector3.Empty) * TGCMatrix.RotationY(-angle * time) * new TGCMatrix(capsuleRigidBody.InterpolationWorldTransform);
                capsuleRigidBody.WorldTransform = character.Transform.ToBsMatrix;
            }

            if (input.keyDown(Key.D))
            {
                director.TransformCoordinate(TGCMatrix.RotationY(angle * time));
                character.Transform             = TGCMatrix.Translation(TGCVector3.Empty) * TGCMatrix.RotationY(angle * time) * new TGCMatrix(capsuleRigidBody.InterpolationWorldTransform);
                capsuleRigidBody.WorldTransform = character.Transform.ToBsMatrix;
            }

            if (input.keyPressed(Key.Space))
            {
                //Activa el comportamiento de la simulacion fisica para la capsula
                capsuleRigidBody.ActivationState = ActivationState.ActiveTag;
                capsuleRigidBody.ApplyCentralImpulse(new TGCVector3(0, 80 * strength, 0).ToBsVector);
            }

            #endregion Comoportamiento
        }
Example #3
0
        public void ApplyImpulse(Vector3 impulse)
        {
            Debug.Assert(RigidBody != null);
            RigidBody.SetActivationState(ActivationState.ACTIVE_TAG);
            IndexedVector3 iv3Impulse = impulse;

            RigidBody.ApplyCentralImpulse(ref iv3Impulse);
        }
Example #4
0
 public void throwObject(Vector3 direction)
 {
     if (_pick_constraint != null && _physics_world.world != null)
     {
         _picked_body.ApplyCentralImpulse(direction * 100.0f);
     }
     releaseObject();
 }
Example #5
0
        public void AddPhoton()
        {
            SphereShape ss     = new SphereShape(photonSize);
            RigidBody   Photon = CreateBody(1, ss, new Vector3(10, 10, 0));

            Photon.ApplyCentralImpulse(new Vector3(15, 5, 0));
            Photons.Add(Photon);
        }
Example #6
0
 // This static method is useful for the demo only. It's called by "void GhostObjectsDemo::clientMoveAndDisplay()".
 // Basically we must find a way to "display" the objects inside ghost objects to the user.
 // We choose to apply a vertical impulse to them (just because it's easier).
 // (The strength of the impulse is different depending on the type of ghost object)
 static void ProcessObectsInsideGhostObjects(ObjectArray <CollisionObject> objs, bool isPairCachingGhostObject)
 {
     foreach (CollisionObject co in objs)
     {
         RigidBody b = RigidBody.Upcast(co);
         if (b != null)
         {
             b.Activate();
             IndexedVector3 impulse = isPairCachingGhostObject ? new IndexedVector3(0, 0.5f, 0) : new IndexedVector3(0, 0.25f, 0);
             b.ApplyCentralImpulse(ref impulse);
         }
     }
 }
        public void Update(TgcD3dInput input, float elapsedTime, float timeBetweenFrames)
        {
            dynamicsWorld.StepSimulation(elapsedTime, 10, timeBetweenFrames);

            var strength = 1.50f;
            var angle    = 5;

            if (input.keyDown(Key.W))
            {
                //Activa el comportamiento de la simulacion fisica para la capsula
                dragonBall.ActivationState = ActivationState.ActiveTag;
                dragonBall.ApplyCentralImpulse(-strength * director.ToBulletVector3());
            }

            if (input.keyDown(Key.S))
            {
                //Activa el comportamiento de la simulacion fisica para la capsula
                dragonBall.ActivationState = ActivationState.ActiveTag;
                dragonBall.ApplyCentralImpulse(strength * director.ToBulletVector3());
            }

            if (input.keyDown(Key.A))
            {
                director.TransformCoordinate(TGCMatrix.RotationY(-angle * 0.001f));
            }

            if (input.keyDown(Key.D))
            {
                director.TransformCoordinate(TGCMatrix.RotationY(angle * 0.001f));
            }

            if (input.keyPressed(Key.Space))
            {
                dragonBall.ActivationState = ActivationState.ActiveTag;
                dragonBall.ApplyCentralImpulse(TGCVector3.Up.ToBulletVector3() * 150);
            }
        }
Example #8
0
    //IMPORTANT Time.fixedTime must match the timestep being used here.
    public static List <UnityEngine.Vector3> SimulateBall(BRigidBody ballRb, UnityEngine.Vector3 ballThrowForce, int numberOfSimulationSteps, bool reverseOrder)
    {
        var ballPositions = new List <UnityEngine.Vector3>(numberOfSimulationSteps);

        //Create a World
        Debug.Log("Initialize physics");

        CollisionConfiguration CollisionConf;
        CollisionDispatcher    Dispatcher;
        BroadphaseInterface    Broadphase;
        CollisionWorld         cw;
        ConstraintSolver       Solver;

        BulletSharp.SoftBody.SoftBodyWorldInfo softBodyWorldInfo;

        //This should create a copy of the BPhysicsWorld with the same settings
        BPhysicsWorld bw = BPhysicsWorld.Get();

        bw.CreatePhysicsWorld(out cw, out CollisionConf, out Dispatcher, out Broadphase, out Solver, out softBodyWorldInfo);
        World = (DiscreteDynamicsWorld)cw;

        //Copy all existing rigidbodies in scene
        // IMPORTANT rigidbodies must be added to the offline world in the same order that they are in the source world
        // this is because collisions must be resolved in the same order for the sim to be deterministic
        DiscreteDynamicsWorld sourceWorld = (DiscreteDynamicsWorld)bw.world;

        BulletSharp.RigidBody   bulletBallRb = null;
        BulletSharp.Math.Matrix mm           = BulletSharp.Math.Matrix.Identity;
        for (int i = 0; i < sourceWorld.NumCollisionObjects; i++)
        {
            CollisionObject co = sourceWorld.CollisionObjectArray[i];
            if (co != null && co.UserObject is BRigidBody)
            {
                BRigidBody      rb            = (BRigidBody)co.UserObject;
                float           mass          = rb.isDynamic() ? rb.mass : 0f;
                BCollisionShape existingShape = rb.GetComponent <BCollisionShape>();
                CollisionShape  shape         = null;
                if (existingShape is BSphereShape)
                {
                    shape = ((BSphereShape)existingShape).CopyCollisionShape();
                }
                else if (existingShape is BBoxShape)
                {
                    shape = ((BBoxShape)existingShape).CopyCollisionShape();
                }

                RigidBody bulletRB = null;
                BulletSharp.Math.Vector3 localInertia = new BulletSharp.Math.Vector3();
                rb.CreateOrConfigureRigidBody(ref bulletRB, ref localInertia, shape, null);
                BulletSharp.Math.Vector3    pos = rb.GetCollisionObject().WorldTransform.Origin;
                BulletSharp.Math.Quaternion rot = rb.GetCollisionObject().WorldTransform.GetOrientation();
                BulletSharp.Math.Matrix.AffineTransformation(1f, ref rot, ref pos, out mm);
                bulletRB.WorldTransform = mm;
                World.AddRigidBody(bulletRB, rb.groupsIBelongTo, rb.collisionMask);
                if (rb == ballRb)
                {
                    bulletBallRb = bulletRB;
                    bulletRB.ApplyCentralImpulse(ballThrowForce.ToBullet());
                }
            }
        }

        //Step the simulation numberOfSimulationSteps times
        for (int i = 0; i < numberOfSimulationSteps; i++)
        {
            int numSteps = World.StepSimulation(1f / 60f, 10, 1f / 60f);
            ballPositions.Add(bulletBallRb.WorldTransform.Origin.ToUnity());
        }

        UnityEngine.Debug.Log("ExitPhysics");
        if (World != null)
        {
            //remove/dispose constraints
            int i;
            for (i = World.NumConstraints - 1; i >= 0; i--)
            {
                TypedConstraint constraint = World.GetConstraint(i);
                World.RemoveConstraint(constraint);
                constraint.Dispose();
            }

            //remove the rigidbodies from the dynamics world and delete them
            for (i = World.NumCollisionObjects - 1; i >= 0; i--)
            {
                CollisionObject obj  = World.CollisionObjectArray[i];
                RigidBody       body = obj as RigidBody;
                if (body != null && body.MotionState != null)
                {
                    body.MotionState.Dispose();
                }
                World.RemoveCollisionObject(obj);
                obj.Dispose();
            }

            World.Dispose();
            Broadphase.Dispose();
            Dispatcher.Dispose();
            CollisionConf.Dispose();
        }

        if (Broadphase != null)
        {
            Broadphase.Dispose();
        }
        if (Dispatcher != null)
        {
            Dispatcher.Dispose();
        }
        if (CollisionConf != null)
        {
            CollisionConf.Dispose();
        }

        return(ballPositions);
    }
Example #9
0
 public void ApplyImpulse(Vector3 impulse)
 {
     RigidBody.ActivationState = BulletSharp.ActivationState.ActiveTag;
     RigidBody.ApplyCentralImpulse(impulse);
 }
Example #10
0
        public void Update(TgcD3dInput input, TgcMesh monstruo, TgcMesh monstruoSil)
        {
            monstruoMesh    = monstruo;
            monstruoSilueta = monstruoSil;

            dynamicsWorld.StepSimulation(1 / 60f, 100);

            #region Comportamiento

            if (input.keyDown(Key.Space) && ModoCreativo)
            {
                //Activa el comportamiento de la simulacion fisica para la capsula
                personajeBody.ActivationState = ActivationState.ActiveTag;
                personajeBody.AngularVelocity = TGCVector3.Empty.ToBulletVector3();
                personajeBody.ApplyCentralImpulse(strength * 10 * (new TGCVector3(0, 1, 0)).ToBulletVector3());
            }

            if (input.keyDown(Key.W))
            {
                //Activa el comportamiento de la simulacion fisica para la capsula
                personajeBody.ActivationState = ActivationState.ActiveTag;
                personajeBody.AngularVelocity = TGCVector3.Empty.ToBulletVector3();
                personajeBody.ApplyCentralImpulse(-strength * director.ToBulletVector3());
            }
            if (input.keyUp(Key.W))
            {
                personajeBody.ActivationState = ActivationState.IslandSleeping;
            }
            if (input.keyUp(Key.S))
            {
                personajeBody.ActivationState = ActivationState.IslandSleeping;
            }

            if (input.keyDown(Key.S))
            {
                //Activa el comportamiento de la simulacion fisica para la capsula
                personajeBody.ActivationState = ActivationState.ActiveTag;
                personajeBody.AngularVelocity = TGCVector3.Empty.ToBulletVector3();
                personajeBody.ApplyCentralImpulse(strength * director.ToBulletVector3());
            }

            if (input.keyDown(Key.A))
            {
                director.TransformCoordinate(TGCMatrix.RotationY(-angle * rotationStrength));
                personaje.Transform          = TGCMatrix.Translation(TGCVector3.Empty) * TGCMatrix.RotationY(-angle * rotationStrength) * new TGCMatrix(personajeBody.InterpolationWorldTransform);
                personajeBody.WorldTransform = personaje.Transform.ToBsMatrix;
                personaje.RotateY(-angle * rotationStrength);
                personajeActual.RotarManos(-angle * rotationStrength);
                monstruo.RotateY(-angle * rotationStrength);
                monstruoSilueta.RotateY(-angle * rotationStrength);
            }

            if (input.keyDown(Key.D))
            {
                director.TransformCoordinate(TGCMatrix.RotationY(angle * rotationStrength));
                personaje.Transform          = TGCMatrix.Translation(TGCVector3.Empty) * TGCMatrix.RotationY(angle * rotationStrength) * new TGCMatrix(personajeBody.InterpolationWorldTransform);
                personajeBody.WorldTransform = personaje.Transform.ToBsMatrix;
                personaje.RotateY(angle * rotationStrength);
                personajeActual.RotarManos(angle * rotationStrength);
                monstruo.RotateY(angle * rotationStrength);
                monstruoSilueta.RotateY(angle * rotationStrength);
            }


            #endregion Comportamiento
        }