Esempio n. 1
0
        private Vector3 Impulse(PhysObj obj, PhysObj obj1)
        {
            Vector3 impluse;

            impluse = -(((obj.Restitution + 1) / (obj.InvMass + obj1.InvMass)) * (obj.Velocity - obj1.Velocity));

            return(impluse);
        }
Esempio n. 2
0
        private Vector3 Impulse(PhysObj obj, PhysObj obj1)
        {
            Vector3 implutse;

            // ***---> YOUR IMPULSE IMPLEMENTATION HERE! <---***
            //implutse = ... ;

            return(impluse);
        }
Esempio n. 3
0
        private PhysObj freeEnd;        //Object 1

        // --------------------- Attributes ---------------------
        public ElasticForce(PhysObj anchorPoint, PhysObj freeEnd, float restLenght = 1.0f, float elastiConstant = 0.1f, float dampingConstant = 0.01f, string id = "ELASTIC")
        {
            type             = id;
            this.anchorPoint = anchorPoint;
            this.freeEnd     = freeEnd;
            this.k           = elastiConstant;
            this.d           = dampingConstant;
            this.restLenght  = restLenght;
        }
Esempio n. 4
0
        private float restLenght; //r

        #endregion Fields

        #region Constructors

        // --------------------- Attributes ---------------------
        public ElasticForce(PhysObj anchorPoint, PhysObj freeEnd, float restLenght = 1.0f, float elastiConstant = 0.1f, float dampingConstant = 0.01f, string id = "ELASTIC")
        {
            type = id;
            this.anchorPoint = anchorPoint;
            this.freeEnd = freeEnd;
            this.k = elastiConstant;
            this.d = dampingConstant;
            this.restLenght = restLenght;
        }
Esempio n. 5
0
        private Vector3 Impulse(PhysObj obj, PhysObj obj1)
        {
            Vector3 impulse = new Vector3();

            // ***---> YOUR IMPULSE IMPLEMENTATION HERE! <---***
            impulse = -(obj.Restitution + 1) / ((obj.InvMass) + (obj1.InvMass)) * (obj.Velocity - obj1.Velocity);

            return(impulse);
        }
Esempio n. 6
0
        /// <summary>
        /// This method dispose of the bomb, destroying the physics object, and removing the bomb and its mesh from the scenegraph
        /// </summary>
        public void Dispose()
        {
            Physics.RemovePhysObj(physObj);
            physObj = null;

            bombNode.Parent.RemoveChild(bombNode);
            bombNode.DetachAllObjects();
            bombNode.Dispose();
            bombEntity.Dispose();
        }
Esempio n. 7
0
        private bool SphereCollision(PhysObj obj, PhysObj potCollObj, ref float[] times)
        {
            times[0] = dt + 1;
            times[1] = -1;

            bool collision = false;

            Vector3 c = potCollObj.Position - obj.Position;
            float   r = obj.Radius + potCollObj.Radius;

            Vector3 v = potCollObj.Velocity - obj.Velocity;

            if (v != Vector3.ZERO)
            {
                float cDotV = c.DotProduct(v);

                float sqrNormV = v.SquaredLength;

                float numTerm = c.SquaredLength - r * r;

                float delta = cDotV * cDotV - sqrNormV * numTerm;

                if (delta >= 0)
                {
                    float sqrtDelta = Mogre.Math.Sqrt(delta);
                    float time1     = (-cDotV + sqrtDelta) / sqrNormV;
                    float time2     = -(cDotV + sqrtDelta) / sqrNormV;

                    if (time2 < time1)
                    {
                        times[0] = time2;
                        times[1] = time1;
                    }
                    else
                    {
                        times[0] = time1;
                        times[1] = time2;
                    }
                }

                if (times[1] >= 0 && times[0] <= dt)
                {
                    collision = true;
                }
            }
            else
            {
                if (c.Length < r)
                {
                    collision = true;
                }
            }

            return(collision);
        }
Esempio n. 8
0
        private Vector3 Impulse(PhysObj obj, PhysObj obj1)
        {
            Vector3 impluse;

            // ***---> YOUR IMPULSE IMPLEMENTATION HERE! <---***
            //impluse = ... ;

            impluse = -((obj.Restitution + 1) / (obj.InvMass + obj1.InvMass)) * (obj.Velocity - obj1.Velocity);
            // 3 = restitution
            return(impluse);
        }
Esempio n. 9
0
        private void CollisionReactions(PhysObj obj, List <PhysObj> collisionList)
        {
            BoundingSphereSeparation(obj);
            for (int i = 0; i < collisionList.Count; i++)
            {
                Vector3 imp = Impulse(obj, collisionList[i]);

                obj.Velocity += imp * obj.InvMass;

                collisionList[i].Velocity -= imp * collisionList[i].InvMass;
            }
        }
Esempio n. 10
0
        private void GetCollidingObjects(PhysObj obj, List <PhysObj> physObjList, out List <PhysObj> collisionList)
        {
            collisionList = new List <PhysObj>();
            foreach (Contacts contObj in obj.CollisionList)
            {
                PhysObj collObj = physObjList.Find(el => el.ID == contObj.collidingObj.ID);

                if (collObj != null)
                {
                    collisionList.Add(collObj);
                }
            }
        }
Esempio n. 11
0
 private void BoundingSphereSeparation(PhysObj obj)
 {
     foreach (Contacts c in obj.CollisionList)
     {
         Vector3 direction = obj.Position - c.collidingObj.Position;
         float   distance  = direction.Normalise();
         float   radiiSum  = obj.Radius + c.collidingObj.Radius;
         if (distance < radiiSum)
         {
             Vector3 separation = 0.5f * (radiiSum - distance) * direction;
             obj.Position            += separation;
             c.collidingObj.Position -= separation;
         }
     }
 }
Esempio n. 12
0
        private Vector3 Impulse(PhysObj obj, PhysObj obj1)
        {
            Vector3 impulse;
            Vector3 velocity;
            float   mass;
            float   e;

            // ***---> YOUR IMPULSE IMPLEMENTATION HERE! <---***
            //implulse = ... ;
            // (e+1) if e=0 doesn't bounce if e=1 bounces lots
            velocity = (obj.Velocity - obj1.Velocity);
            mass     = (obj.InvMass + obj1.InvMass);
            e        = 0.0f;
            impulse  = -((e + 1) / mass) * velocity;
            return(impulse);
        }
Esempio n. 13
0
        private Vector3 VelVerletSolver(PhysObj obj, float Dt)
        {
            Vector3 newPos       = new Vector3();
            Vector3 finalVel     = new Vector3();
            Vector3 oldResForces = new Vector3();

            oldResForces = obj.ResForces;       //Froces at this frame

            newPos = obj.Position + (obj.Velocity * Dt) + (((Dt * Dt * obj.InvMass) / 2) * oldResForces);

            obj.UpdateResFor(newPos);           //Forces at next frame (access thorugh obj.ResForces)

            finalVel = obj.Velocity + ((Dt * obj.InvMass) / 2) * (oldResForces + obj.ResForces);

            return(finalVel);
        }
Esempio n. 14
0
        private Vector3 VelVerletSolver(PhysObj obj, float Dt)
        {
            Vector3 newPos       = new Vector3();
            Vector3 finalVel     = new Vector3();
            Vector3 oldResForces = new Vector3();

            oldResForces = obj.ResForces;       //Froces at this frame

            // ***---> YOUR POSITION UPDATE IMPLEMENTATION HERE! <---***
            newPos = obj.Position + (obj.Velocity * Dt) + ((Dt * Dt) * (0.5f * obj.InvMass)) * oldResForces;

            obj.UpdateResFor(newPos);           //Forces at next frame (access thorugh obj.ResForces)

            // ***---> YOUR VELOCITY UPDATE IMPLEMENTATION HERE! <---***
            finalVel = obj.Velocity + (Dt * 0.5f * obj.InvMass) * (oldResForces + obj.ResForces);

            return(finalVel);
        }
Esempio n. 15
0
        private Vector3 VelVerletSolver(PhysObj obj, float Dt)
        {
            Vector3 newPos       = new Vector3();
            Vector3 finalVel     = new Vector3();
            Vector3 oldResForces = new Vector3();

            oldResForces = obj.ResForces;       //Froces at this frame

            // ***---> YOUR POSITION UPDATE IMPLEMENTATION HERE! <---***
            // newPos = ... ;

            obj.updateResFor(newPos);           //Forces at next frame (access thorugh obj.ResForces)

            // ***---> YOUR VELOCITY UPDATE IMPLEMENTATION HERE! <---***
            // finalVel = ... ;

            return(finalVel);
        }
Esempio n. 16
0
        private void PlaneCollision(PhysObj obj, Plane plane)
        {
            float normalSpeed = obj.Velocity.DotProduct(plane.normal);
            float potential   = obj.Position.DotProduct(plane.normal);

            if (normalSpeed < 0)
            {
                float t = (obj.Radius - potential - plane.d) / normalSpeed;
                if (t <= dt)
                {
                    obj.Velocity -= (1 + obj.Restitution) * normalSpeed * plane.normal;
                }
            }
            Vector3 pos = obj.Position - obj.Radius * plane.normal;

            if (pos.DotProduct(plane.normal) + plane.d < 0)
            {
                obj.Position = obj.Position - (pos.DotProduct(plane.normal) + plane.d) * plane.normal;
            }
        }
Esempio n. 17
0
        private Vector3 VelVerletSolver(PhysObj obj, float Dt)
        {
            Vector3 newPos       = new Vector3();
            Vector3 finalVel     = new Vector3();
            Vector3 oldResForces = new Vector3();

            oldResForces = obj.ResForces;   //Froces at this frame

            // ***---> YOUR POSITION UPDATE IMPLEMENTATION HERE! <---***
            ///newPos =
            newPos = -((obj.Position) + (obj.Velocity / Dt) + (Dt * 2) / (2 * obj.InvMass) * obj.ResForces);


            obj.UpdateResFor(newPos); //Forces at next frame (access thorugh obj.ResForces)

            // ***---> YOUR VELOCITY UPDATE IMPLEMENTATION HERE! <---***
            ///finalVel =
            finalVel = -(obj.Velocity) + (Dt) / (2 * obj.InvMass) * ((obj.ResForces) + (obj.ResForces + 1));

            return(finalVel);
        }
Esempio n. 18
0
        private Vector3 VelVerletSolver(PhysObj obj, float Dt)
        {
            Vector3 newPos       = new Vector3();
            Vector3 finalVel     = new Vector3();
            Vector3 oldResForces = new Vector3();

            oldResForces = obj.ResForces;       //Froces at this frame

            // ***---> YOUR POSITION UPDATE IMPLEMENTATION HERE! <---***
            //newPos = ... ;
            newPos = (obj.Position + (obj.Velocity * 1) + (1 * 1 + (2 * (1 / obj.InvMass)) * oldResForces));


            obj.UpdateResFor(newPos);           //Forces at next frame (access thorugh obj.ResForces)

            // ***---> YOUR VELOCITY UPDATE IMPLEMENTATION HERE! <---***
            // finalVel = ... ;
            finalVel = (obj.Velocity + (1 / (2 * (1 / obj.InvMass))) * (oldResForces) + obj.ResForces);

            return(finalVel);
        }
Esempio n. 19
0
        /// <summary>
        /// This method detaches the robot node from the scene graph and destroies it and the robot enetity
        /// </summary>
        public void Dispose()
        {
            robotNode.RemoveAllChildren();
            robotNode.Parent.RemoveChild(robotNode);
            robotNode.DetachAllObjects();
            robotNode.Dispose();
            robotEntity.Dispose();

            Physics.RemovePhysObj(physObj);
            physObj = null;
        }
Esempio n. 20
0
 static public void RemovePhysObj(PhysObj physObj)
 {
     toRemove.Add(physObj);
 }
Esempio n. 21
0
        //---------------------------------------- Auxiliary Methods -----------------------------------------

        static public void AddPhysObj(PhysObj physObj)
        {
            physObjsList.Add(physObj);
        }
Esempio n. 22
0
        private void CollisionReactions(PhysObj obj, List<PhysObj> collisionList)
        {
            BoundingSphereSeparation(obj);
            for (int i = 0; i < collisionList.Count; i++)
            {
                Vector3 imp = Impulse(obj, collisionList[i]);

                obj.Velocity += imp * obj.InvMass;

                collisionList[i].Velocity -= imp * collisionList[i].InvMass;
            }
        }
Esempio n. 23
0
 //---------------------------------------- Auxiliary Methods -----------------------------------------
 public static void AddPhysObj(PhysObj physObj)
 {
     physObjsList.Add(physObj);
 }
Esempio n. 24
0
        private Vector3 VelVerletSolver(PhysObj obj, float Dt)
        {
            Vector3 newPos = new Vector3();
            Vector3 finalVel = new Vector3();
            Vector3 oldResForces = new Vector3();

            oldResForces = obj.ResForces; 	//Froces at this frame

            // ***---> YOUR POSITION UPDATE IMPLEMENTATION HERE! <---***
            //newPos = ... ;
            newPos = ( obj.Position + (obj.Velocity*1) + ( 1*1 +  (2* ( 1/obj.InvMass)) * oldResForces));

            obj.UpdateResFor(newPos);		//Forces at next frame (access thorugh obj.ResForces)

            // ***---> YOUR VELOCITY UPDATE IMPLEMENTATION HERE! <---***
            // finalVel = ... ;
            finalVel = (obj.Velocity + (1 / (2 * (1 / obj.InvMass))) * (oldResForces) + obj.ResForces);

            return finalVel;
        }
Esempio n. 25
0
 public static void RemovePhysObj(PhysObj physObj)
 {
     toRemove.Add(physObj);
 }
Esempio n. 26
0
 private void BoundingSphereSeparation(PhysObj obj)
 {
     foreach (Contacts c in obj.CollisionList)
     {
         Vector3 direction = obj.Position - c.collidingObj.Position;
         float distance = direction.Normalise();
         float radiiSum = obj.Radius + c.collidingObj.Radius;
         if (distance < radiiSum)
         {
             Vector3 separation = 0.5f * (radiiSum - distance) * direction;
             obj.Position += separation;
             c.collidingObj.Position -= separation;
         }
     }
 }
Esempio n. 27
0
        /// <summary>
        /// This method loads the mesh and attaches it to a node and to the schenegraph
        /// </summary>
        private void Load()
        {
            robotEntity = mSceneMgr.CreateEntity("robot.mesh");
            robotNode = mSceneMgr.CreateSceneNode();
            robotNode.AttachObject(robotEntity);
            //mSceneMgr.RootSceneNode.AddChild(robotNode);

            controlNode = mSceneMgr.CreateSceneNode();
            controlNode.AddChild(robotNode);
            mSceneMgr.RootSceneNode.AddChild(controlNode);

            float radius = 50;
            controlNode.Position += radius * Vector3.UNIT_Y;
            robotNode.Position -= radius * Vector3.UNIT_Y;

            physObj = new PhysObj(radius, "Robot", 0.1f, 0.2f, 0.5f);
            physObj.SceneNode = controlNode;
            physObj.Position = controlNode.Position;
            physObj.AddForceToList(new WeightForce(physObj.InvMass));
            physObj.AddForceToList(new FrictionForce(physObj));
            Physics.AddPhysObj(physObj);
        }
Esempio n. 28
0
 private Vector3 Impulse(PhysObj obj, PhysObj obj1)
 {
     Vector3 impulse;
     Vector3 velocity;
     float mass;
     float e;
     // ***---> YOUR IMPULSE IMPLEMENTATION HERE! <---***
     //implulse = ... ;
     // (e+1) if e=0 doesn't bounce if e=1 bounces lots
     velocity = (obj.Velocity - obj1.Velocity);
     mass = (obj.InvMass + obj1.InvMass);
     e = 0.0f;
     impulse = -((e+1)/mass)*velocity;
     return impulse;
 }
Esempio n. 29
0
 private void PlaneCollision(PhysObj obj, Plane plane)
 {
     float normalSpeed = obj.Velocity.DotProduct(plane.normal);
     float potential = obj.Position.DotProduct(plane.normal);
     if (normalSpeed < 0)
     {
         float t = (obj.Radius - potential - plane.d) / normalSpeed;
         if (t <= dt)
         {
             obj.Velocity -= (1 + obj.Restitution) * normalSpeed * plane.normal;
         }
     }
     Vector3 pos = obj.Position - obj.Radius * plane.normal;
     if (pos.DotProduct(plane.normal) + plane.d < 0)
     {
         obj.Position = obj.Position - (pos.DotProduct(plane.normal) + plane.d) * plane.normal;
     }
 }
 // -------------------------------- Constructor ----------------------------------
 public FrictionForce(PhysObj obj, string id = "FRICTION")
 {
     type     = id;
     this.obj = obj;
 }
Esempio n. 31
0
 // -------------------------------- Constructor ----------------------------------
 public FrictionForce(PhysObj obj, string id="FRICTION")
 {
     type = id;
     this.obj = obj;
 }
Esempio n. 32
0
        /// <summary>
        /// This method detaches and dispode of all the elements of the compound model
        /// </summary>
        public void Dispose()
        {
            //if (sphere != null)                     // Start removing from the leaves of the sub-graph
            //{
            //    if (sphere.Parent != null)
            //        sphere.Parent.RemoveChild(sphere);
               //     sphere.DetachAllObjects();
             //   sphere.Dispose();
               //     sphereEntity.Dispose();
            //}

            //if (mainHull != null)
            //{
            //    if (mainHull.Parent != null)
            //        mainHull.Parent.RemoveChild(mainHull);
               //     mainHull.DetachAllObjects();
               //     mainHull.Dispose();
               //     hullEntity.Dispose();
               // }

            if (model != null)                      // Stop removing with the sub-graph root
            {
                if (model.Parent != null)
                    model.Parent.RemoveChild(model);
                model.Dispose();
                Physics.RemovePhysObj(physObj);
                physObj = null;
            }
        }
Esempio n. 33
0
        /// <summary>
        /// This method loads the nodes and entities needed by the compound model
        /// </summary>
        protected override void LoadModelElements()
        {
            hullGroup = mSceneMgr.CreateSceneNode();
            wheelGroup = mSceneMgr.CreateSceneNode();
            gunGroup = mSceneMgr.CreateSceneNode();

            mainHull = mSceneMgr.CreateSceneNode();
            hullEntity = mSceneMgr.CreateEntity("Main.mesh");
            hullEntity.GetMesh().BuildEdgeList();

            sphere = mSceneMgr.CreateSceneNode();
            sphereEntity = mSceneMgr.CreateEntity("Sphere.mesh");
            sphereEntity.GetMesh().BuildEdgeList();

            powerCells = mSceneMgr.CreateSceneNode();
            powerCellsEntity = mSceneMgr.CreateEntity("PowerCells.mesh");
            powerCellsEntity.GetMesh().BuildEdgeList();

            model = mSceneMgr.CreateSceneNode();

            float radius = 50;
            model.Position += radius * Vector3.UNIT_Y;
            hullGroup.Position -= radius * Vector3.UNIT_Y;

            physObj = new PhysObj(radius, "PlayerModel", 0.1f, 0.21f, 0.1f);
            physObj.SceneNode = model;
            physObj.Position = model.Position;
            physObj.AddForceToList(new WeightForce(physObj.InvMass));
            physObj.AddForceToList(new FrictionForce(physObj));
            Physics.AddPhysObj(physObj);
        }
Esempio n. 34
0
        private void GetCollidingObjects(PhysObj obj, List<PhysObj> physObjList, out List<PhysObj> collisionList)
        {
            collisionList = new List<PhysObj>();
            foreach (Contacts contObj in obj.CollisionList)
            {
                PhysObj collObj = physObjList.Find(el => el.ID == contObj.collidingObj.ID);

                if (collObj != null)
                    collisionList.Add(collObj);
            }
        }
Esempio n. 35
0
        /// <summary>
        /// This method load the bomb mesh and its physics object
        /// </summary>
        private void Load()
        {
            removeMe = false;
            bombEntity = mSceneMgr.CreateEntity("Bomb.mesh");

            bombNode = mSceneMgr.CreateSceneNode();
            bombNode.Scale(2, 2, 2);
            bombNode.AttachObject(bombEntity);
            mSceneMgr.RootSceneNode.AddChild(bombNode);

            physObj = new PhysObj(10, "Bomb", 0.1f, 0.5f);
            physObj.SceneNode = bombNode;
            physObj.AddForceToList(new WeightForce(physObj.InvMass));

            Physics.AddPhysObj(physObj);
        }
Esempio n. 36
0
        private bool SphereCollision(PhysObj obj, PhysObj potCollObj,ref float[] times)
        {
            times[0] = dt + 1;
            times[1] = -1;

            bool collision = false;

            Vector3 c = potCollObj.Position - obj.Position;
            float r = obj.Radius + potCollObj.Radius;

            Vector3 v = potCollObj.Velocity - obj.Velocity;

            if (v != Vector3.ZERO)
            {
                float cDotV = c.DotProduct(v);

                float sqrNormV = v.SquaredLength;

                float numTerm = c.SquaredLength - r * r;

                float delta = cDotV * cDotV - sqrNormV * numTerm;

                if (delta >= 0)
                {
                    float sqrtDelta = Mogre.Math.Sqrt(delta);
                    float time1 = (-cDotV + sqrtDelta) / sqrNormV;
                    float time2 = -(cDotV + sqrtDelta) / sqrNormV;

                    if (time2 < time1)
                    {
                        times[0] = time2;
                        times[1] = time1;
                    }
                    else
                    {
                        times[0] = time1;
                        times[1] = time2;
                    }
                }

                if (times[1] >= 0 && times[0] <= dt)
                {
                    collision = true;
                }
            }
            else
            {
                if(c.Length<r)
                    collision = true;
            }

            return collision;
        }