//remove the collider from the bounding box dict public static void removeCollider(Component.cCollider c) { if (man.boundingBoxes.ContainsKey(c.key)) { man.boundingBoxes[c.key].Remove(c); if (man.boundingBoxes[c.key].Count <= 0) { man.boundingBoxes.Remove(c.key); } } }
//add the collider to the list in the dict public static void addCollider(Component.cCollider c) { //generate the key Point key = getKey(c.rect); //if there is already a list for that key just put the collider in that list ~~ if (man.boundingBoxes.ContainsKey(key)) { man.boundingBoxes[key].Add(c); } else { //~~ otherwise add a new list to that key and add the collider man.boundingBoxes.Add(key, new List <Component.cCollider>()); man.boundingBoxes[key].Add(c); } //set the colliders key to the generated key c.key = key; }
public static Point WillItCollide(Component.cCollider c, int xMove, int yMove) { //assign the movement to a point to use to return Point p = new Point(xMove, yMove); //Create two rects that corrispond to the rect if this move is allowed for each axis Rectangle futureRectX = new Rectangle(c.rect.X + xMove, c.rect.Y, c.rect.Width, c.rect.Height); Rectangle futureRectY = new Rectangle(c.rect.X, c.rect.Y + yMove, c.rect.Width, c.rect.Height); //check if each of those rects will collide bool collideX = man.CheckFutureCollision(futureRectX, c); bool collideY = man.CheckFutureCollision(futureRectY, c); //if the rect that corrisponds to the xmove collides set the xmove to 0 if (collideX) { if (xMove < 0) { p.X = 1; } else { p.X = -1; } } //do the same for the y move if (collideY) { if (yMove < 0) { p.Y = 1; } else { p.Y = -1; } } //return the adjusted movement return(p); }
//this checks if a rect will cause a collision and returns true if it does public Boolean CheckFutureCollision(Rectangle rect, Component.cCollider c) { //get the boundingBoxes Key from the rect Point cKey = getKey(rect); //if the rect is outside the world immediately return true if (c.rRef.tag != "Player" && CheckForLeaveWorld(rect)) { return(true); } //for all of the surrounding bounding boxes check if any of the objects will collide for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { //this generates the key for the surrounding bounding boxes Point key = new Point(cKey.X + i, cKey.Y + j); //this checks if the corrisponding bounding box has any objects in it if (boundingBoxes.ContainsKey(key)) { List <Component.cCollider> box = boundingBoxes[key]; //for every object in the bounding box -- for (int k = 0; k < box.Count; k++) { //-- check if the bounding box collides and is not the object calling this function return true if (box[k].collides(rect, c) && c != box[k] && c.parentState == box[k].parentState) { return(true); } } } } } //if all else fails return false return(false); }
public List <Component.cCollider> CheckCollision(Component.cCollider c, int buffer) { //create a list to hold all of the objects that might be colliding List <Component.cCollider> temp = new List <Component.cCollider>(); //get the key for the collidable that is being checked Point cKey = getKey(c.rect); Rectangle rect = new Rectangle(c.rect.X - buffer, c.rect.Y - buffer, c.rect.Width + (buffer * 2), c.rect.Height + (buffer * 2)); //for all of the surrounding bounding boxes -- for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { //-- generate the key for that bounding box -- Point key = new Point(cKey.X + i, cKey.Y + j); //-- if the key has any objects in it -- if (boundingBoxes.ContainsKey(key)) { //-- for all of said objects ~~ List <Component.cCollider> box = boundingBoxes[key]; for (int k = 0; k < box.Count; k++) { //~~ if the object collides with the calling object and it is not the calling object ~~ if (box[k].collides(rect, c) && c != box[k] && c.parentState == box[k].parentState && c.rRef.active) { //~~ add that object to the list to return temp.Add(box[k]); } } } } } //return all the objects that are colliding return(temp); }
//remove and replace the collider to change which bounding box it is in public static void moveCollider(Component.cCollider c) { removeCollider(c); addCollider(c); }