public void AddObject(PhysicsObject obj)
        {
            if (!setup)
            {
                throw new Exception("SetDimensions must be called first!");
            }

            if (obj.type == PhysicsObjectType.potBlobParticle)
            {
                int x = (int)(((BlobParticle)obj).Position.X / gridSize);
                int y = (int)(((BlobParticle)obj).Position.Y / gridSize);
                if (grid[x][y].ContainsKey(obj.id))
                {
                    RemoveObject(obj);
                }
                grid[x][y].Add(obj.id, obj);
            }
            else if (obj.type == PhysicsObjectType.potStaticBody)
            {
                //here we have to calculate all the gridsquares that the object may occupy
                //and add a reference to the object to all grid squares
                StaticBody body   = (StaticBody)obj;
                int        xStart = (int)Math.Floor(body.BoundingBox.l / gridSize);
                int        yStart = (int)Math.Floor(body.BoundingBox.t / gridSize);
                //throw new Exception(body.x.ToString());
                if (body.BoundingBox.r > gridSize || body.BoundingBox.b > gridSize)
                {
                    //calculate the number of squares this body is going to cover
                    int xEnd = (int)(Math.Floor(body.BoundingBox.r / gridSize));        // CHANGED - Used to have xStart + ... why ?
                    int yEnd = (int)(Math.Floor(body.BoundingBox.b / gridSize));
                    // throw new Exception(xStart.ToString() + " to " + xEnd.ToString() + " " + yEnd.ToString());
                    for (int i = xStart; i < xEnd; i++)
                    {
                        for (int j = yStart; j < yEnd; j++)
                        {
                            grid[i][j].Add(obj.id, obj);
                        }
                    }
                }
                else //the body is not bigger than a single gridsquare, so it only needs to be added to one.
                {
                    grid[xStart][yStart].Add(obj.id, obj);
                }
            }
            else if (obj.type == PhysicsObjectType.potParasiteBodyPart)
            {
                int x = (int)Math.Floor(((ParasiteBodyPart)obj).Position.X / gridSize);
                int y = (int)Math.Floor(((ParasiteBodyPart)obj).Position.Y / gridSize);
                if (grid[x][y].ContainsKey(obj.id))
                {
                    RemoveObject(obj);
                }
                // grid[x][y].Add(obj.id, obj);
            }
            else
            {
                throw new Exception("OMFG DIDNT ADD ANYTHING TO THE GRID F**K!");
            }
        }
        public void RemoveObject(PhysicsObject obj)
        {
            if (!setup)
            {
                throw new Exception("SetDimensions must be called first!");
            }

            if (obj.type == PhysicsObjectType.potBlobParticle)
            {
                int x = (int)(((BlobParticle)obj).Position.X / gridSize);
                int y = (int)(((BlobParticle)obj).Position.Y / gridSize);
                grid[x][y].Remove(((BlobParticle)obj).id);
            }
            else if (obj.type == PhysicsObjectType.potStaticBody)
            {
                StaticBody body   = (StaticBody)obj;
                int        xStart = (int)Math.Floor(body.BoundingBox.l / gridSize);
                int        yStart = (int)Math.Floor(body.BoundingBox.t / gridSize);
                //th    row new Exception(body.x.ToString());
                if (body.BoundingBox.r > gridSize || body.BoundingBox.b > gridSize)
                {
                    //calculate the number of squares this body is going to cover
                    int xEnd = (int)(Math.Floor(body.BoundingBox.r / gridSize));
                    int yEnd = (int)(Math.Floor(body.BoundingBox.b / gridSize));
                    // throw new Exception(xStart.ToString() + " to " + xEnd.ToString() + " " + yEnd.ToString());
                    for (int i = xStart; i < xEnd; i++)
                    {
                        for (int j = yStart; j < yEnd; j++)
                        {
                            grid[i][j].Remove(obj.id);
                        }
                    }
                }
                else //the body is not bigger than a single gridsquare, so it only needs to be added to one.
                {
                    grid[xStart][yStart].Add(obj.id, obj);
                }
            }
            else if (obj.type == PhysicsObjectType.potParasiteBodyPart)
            {
                int x = (int)Math.Floor(((ParasiteBodyPart)obj).Position.X / gridSize);
                int y = (int)Math.Floor(((ParasiteBodyPart)obj).Position.Y / gridSize);
                //grid[x][y].Remove(((ParasiteBodyPart)obj).id);
            }
        }
Exemple #3
0
        /// <summary>
        /// Creates a new Static Body
        /// </summary>
        /// <param name="colour">The Colour of the Static Body</param>
        /// <param name="vertices">The vertices that make up the shape</param>
        public virtual void NewBody(Color colour, params Vector2[] vertices)
        {
            StaticBody sb = new StaticBody(Game, PhysicsOverlord.GetInstance().GetID(), GraphicsDevice, colour, vertices);

            NewBody(sb);
        }