public static SpatialGrid GetInstance()
 {
     if (instance == null)
     {
         instance = new SpatialGrid(0, 0, 1);
     }
     return(instance);
 }
 public void SetDimensions(int gridWidth, int gridHeight, float gridSize)
 {
     if (setup)
     {
         throw new Exception("Dimensions already set!");
     }
     instance       = new SpatialGrid(gridWidth, gridHeight, gridSize);
     instance.setup = true;
 }
Esempio n. 3
0
 /// <summary>
 /// Rotates the shape
 /// </summary>
 /// <param name="angle">Number of degrees to rotate</param>
 protected void Rotate(float angle)
 {
     SpatialGrid.GetInstance().RemoveObject(this);
     for (int i = 0; i < sourceVertices.Length; i++)
     {
         sourceVertices[i] = RotateAroundPoint(sourceVertices[i], centre, Vector3.Backward, angle);
     }
     BoundingBox = new BoundingRectangle(sourceVertices);
     TriangulatePoly();
     SpatialGrid.GetInstance().AddObject(this);
 }
        public void initSimulation()
        {
            theParticles = new List <BlobParticle>();
            theSprings   = new List <Spring>();

            //TODO: SHOULD BE DONE SOMEWHERE ELSE!!!
            SpatialGrid.GetInstance().SetDimensions(800, 600, disconnectThreshold);

            connected = new Spring[maxParticles + 1][];
            for (int i = 0; i < maxParticles; i++)
            {
                connected[i] = new Spring[maxParticles + 1];
            }
        }
Esempio n. 5
0
 public override void Draw()
 {
     SpatialGrid.GetInstance().RemoveObject(this);
     // Apply Velocity
     for (int i = 0; i < sourceVertices.Length; i++)
     {
         sourceVertices[i] += this.velocity;
     }
     BoundingBox = new BoundingRectangle(sourceVertices);
     TriangulatePoly();
     SpatialGrid.GetInstance().AddObject(this);
     // Call Parent Draw Methods
     base.Draw();
 }
        public void addParticles(int num)
        {
            for (int i = 0; i < num; i++)
            {
                Random RandomGenerator = new Random();

                float   randomXVelocity = 5;
                Vector2 mousePos        = camera.MouseToWorld();
                float   xPos            = mousePos.X;
                float   yPos            = mousePos.Y;

                if (xPos < drawingOffset)
                {
                    xPos = drawingOffset + 1;
                }
                else if (xPos > 800 - drawingOffset)
                {
                    xPos = 800 - drawingOffset - 1;
                }

                if (yPos < 0)
                {
                    yPos = 1;
                }
                else if (yPos > 500)
                {
                    yPos = 499;
                }

                BlobParticle theParticle = new BlobParticle(Game, new Vector2(xPos, yPos), theSprite, PhysicsOverlord.GetInstance().GetID(), particleRadius);
                theParticle.colour   = Color.Red;
                theParticle.velocity = new Vector2(5, 5);

                theParticles.Add(theParticle);
                SpatialGrid.GetInstance().AddObject(theParticle);

                particleCount++;
                currentNumParticles--;
            }
        }
        /*
         * Currently Being Performed in a ... really odd fashion
         */
        public virtual void UpdatePoint()
        {
            doCollisionCheck();

            oldPosition = Position;

            SpatialGrid.GetInstance().RemoveObject(this);
            velocity += force;

            if (IKPoint != null)
            {
                IKPoint.update();
            }

            velocity *= 0.95f;
            Position += velocity;

            if (Position.Y > 500)
            {
                Position.Y = 500;
                velocity.Y = 0;
            }
            else if (Position.Y < 0)
            {
                Position.Y = 0;
                velocity.Y = 0;
            }

            if (Position.X > 800)
            {
                Position.X  = 800;
                velocity.X *= -0.5f;
            }
            else if (Position.X < 0)
            {
                Position.X  = 0;
                velocity.X *= -0.5f;
            }
            SpatialGrid.GetInstance().AddObject(this);
        }
        public void moveParticles()
        {
            BlobParticle         theParticle;
            PhysicsObject        neighbourObject;
            Vector2              differenceVector;
            int                  neighourCount;
            int                  j;
            float                distance;
            List <PhysicsObject> neighbours;

            for (int i = 0; i < particleCount; i++)
            {
                theParticle = theParticles[i];
                SpatialGrid.GetInstance().RemoveObject(theParticle);
                theParticle.oldPosition = theParticle.Position;
                theParticle.Position   += theParticle.velocity;

                // Apply Gravity
                theParticle.applyForce(gravity);

                simpleCollisionDetection(theParticle);
                SpatialGrid.GetInstance().AddObject(theParticle);

                //BEGIN CHECK SPRINGS
                neighbours    = SpatialGrid.GetInstance().GetNeighbours(theParticle);
                neighourCount = neighbours.Count;
                for (j = 0; j < neighourCount; j++)
                {
                    neighbourObject = neighbours[j];
                    // If PARTICLE
                    if (neighbourObject.type == PhysicsObjectType.potBlobParticle)
                    {
                        differenceVector = theParticle.Position - ((BlobParticle)neighbourObject).Position;
                        distance         = differenceVector.Length();

                        if (distance <= threshold && connected[theParticle.id][((BlobParticle)neighbourObject).id] == null)
                        {
                            theParticle.addNeighbour(((BlobParticle)neighbourObject));
                            ((BlobParticle)neighbourObject).addNeighbour(theParticle);

                            connected[theParticle.id][((BlobParticle)neighbourObject).id] = new Spring(theParticle, ((BlobParticle)neighbourObject), springStiffness, springLength, springFriction);
                        }
                        else if (distance > disconnectThreshold && connected[theParticle.id][((BlobParticle)neighbourObject).id] != null)
                        {
                            theParticle.removeNeighbour(((BlobParticle)neighbourObject));
                            ((BlobParticle)neighbourObject).removeNeighbour(theParticle);
                            connected[theParticle.id][((BlobParticle)neighbourObject).id] = null;
                        }

                        if (connected[theParticle.id][((BlobParticle)neighbourObject).id] != null)
                        {
                            //connected[theParticle.id][neighbourParticle.id].viscoSolve();
                            connected[theParticle.id][((BlobParticle)neighbourObject).id].solve();
                        }
                    }
                    else if (neighbourObject.type == PhysicsObjectType.potStaticBody)
                    {
                        // IF STATIC BODY
                        Vector2 result;
                        if ((result = ((StaticBody)neighbourObject).Collides(theParticle)) != Vector2.Zero)
                        {
                            // Collision Code from 'Devin Horsman' Message
                            float kr       = 0.5f;
                            float friction = 1f;

                            //Vector2 tempVel = theParticle.oldPosition - theParticle.Position;
                            Vector2 tempVel   = theParticle.velocity;
                            Vector2 velocityN = Vector2.Dot(tempVel, result) * result;
                            Vector2 velocityT = tempVel - velocityN;

                            // Check if object is moving towards wall.
                            //if (Vector2.Dot(theParticle.velocity, result) < 0)
                            //{
                            SpatialGrid.GetInstance().RemoveObject(theParticle);
                            // No Friction
                            Vector2 newVel = velocityT - kr * velocityN;

                            // Fake Friction

                            /*Vector2 newVTan = Vector2.Max(Vector2.Zero, velocityT - velocityT * friction);
                             * Vector2 newVel = newVTan + kr * velocityN;*/

                            theParticle.velocity = newVel;
                            theParticle.Position = theParticle.oldPosition;
                            float check = Vector2.Dot(tempVel, result);

                            //if (check < theParticle.radius/2)
                            //{
                            Vector2 contactForce = -Vector2.Dot(result, theParticle.velocity) * result;
                            //theParticle.applyForce(contactForce);
                            theParticle.velocity = contactForce;
                            //}
                            // Friction
                            //if (velocityT > new Vector2(5,5))
                            //{
                            //Vector2 frictionForce = -friction * Vector2.Dot(-result, theParticle.velocity) * velocityT;
                            //theParticle.applyForce(frictionForce);
                            //}

                            SpatialGrid.GetInstance().AddObject(theParticle);
                            //}
                        }
                    }
                }
                //END CHECK SPRINGS
            }
        }
 public override void NewBody(PhysicsObject newbody)
 {
     bodies.Add(newbody.id, newbody);
     SpatialGrid.GetInstance().AddObject(newbody);
 }
        public void doCollisionCheck()
        {
            List <PhysicsObject> neighbours;
            PhysicsObject        neighbourObject;
            int neighbourCount;

            int particleCount = 0;

            neighbours     = SpatialGrid.GetInstance().GetPNeighbours(this);
            neighbourCount = neighbours.Count;

            for (int j = 0; j < neighbourCount; j++)
            {
                neighbourObject = neighbours[j];
                // IF STATIC BODY
                Vector2 result;
                if (neighbourObject.type == PhysicsObjectType.potStaticBody)
                {
                    if ((result = ((StaticBody)neighbourObject).Collides(this)) != Vector2.Zero)
                    {
                        // ATTEMPTED NEW COLLISION CODE
                        float kr       = 0f;
                        float friction = 1f;

                        Vector2 tempVel = this.velocity;
                        //Vector2 tempVel = this.velocity;
                        Vector2 velocityN = Vector2.Dot(tempVel, result) * result;
                        Vector2 velocityT = tempVel - velocityN;

                        // Check if object is moving towards wall.
                        //if (Vector2.Dot(tempVel, result) < 0)
                        //{
                        SpatialGrid.GetInstance().RemoveObject(this);
                        // No Friction
                        //Vector2 newVel = velocityT - kr * velocityN;

                        // Fake Friction
                        Vector2 newVTan = Vector2.Max(Vector2.Zero, velocityT - velocityT * friction);
                        Vector2 newVel  = newVTan + kr * velocityN;

                        this.velocity = newVel;
                        this.Position = this.oldPosition;

                        //if (Vector2.Dot(tempVel, result) < 1)
                        //{
                        Vector2 contactForce = -(Vector2.Dot(result, Position - oldPosition) * result);
                        //this.velocity = contactForce;
                        this.ApplyForce(contactForce);
                        //}
                        this.Position += this.velocity;
                        SpatialGrid.GetInstance().AddObject(this);
                        //}
                        //status = ParasiteStatus.crawling;
                    }
                }
                else if (neighbourObject.type == PhysicsObjectType.potBlobParticle)
                {
                    // IF COLLIDING : DISABLE GRAVITY
                    relativeGravity = new Vector2(0, 1.5f);
                    particleCount++;
                }
                else
                {
                    // relativeGravity = new Vector2(0, 0.9f);
                }
            }

            if (particleCount == 0)
            {
                relativeGravity = new Vector2(0, 0.9f);
                status          = ParasiteStatus.crawling;
            }
            else
            {
                this.velocity *= 0.93f;
                status         = ParasiteStatus.swimming;
            }
        }