public override bool Collide(Vector2 thisOffset, CollisionBox box, Vector2 otherOffset) { // might run into the +1 problem again so chekc here to make sure if (!CheckCollisionMasks(box)) { return(false); } var boxPosition = box.AnchorPoint + box.AnchorObject.Position + otherOffset; var topLeftCorner = _anchorPoint + _anchorObject.Position + thisOffset; var topRightCorner = _anchorPoint + _anchorObject.Position + thisOffset + new Vector2(_width, 0); var bottomLeftCorner = _anchorPoint + _anchorObject.Position + thisOffset + new Vector2(0, _height); var bottomRightCorner = _anchorPoint + _anchorObject.Position + thisOffset + new Vector2(_width, _height); if (box is CircleCollisionBox) { var radius = ((CircleCollisionBox)box).Radius; if (boxPosition.Y >= topLeftCorner.Y && boxPosition.Y <= bottomLeftCorner.Y && boxPosition.X <= topRightCorner.X && boxPosition.X >= topLeftCorner.X) { return(true); } if (Vector2.Distance(topLeftCorner, boxPosition) <= radius || Vector2.Distance(topRightCorner, boxPosition) <= radius || Vector2.Distance(bottomLeftCorner, boxPosition) <= radius || Vector2.Distance(bottomRightCorner, boxPosition) <= radius) { return(true); } if (topLeftCorner.Y - boxPosition.Y <= radius && boxPosition.X >= topLeftCorner.X && boxPosition.X <= topRightCorner.X) { return(true); } if (boxPosition.Y - bottomRightCorner.Y <= radius && boxPosition.X >= topLeftCorner.X && boxPosition.X <= topRightCorner.X) { return(true); } if (topLeftCorner.X - boxPosition.X <= radius && boxPosition.Y >= topLeftCorner.Y && boxPosition.Y <= bottomRightCorner.Y) { return(true); } if (boxPosition.X - topRightCorner.X <= radius && boxPosition.Y >= topLeftCorner.Y && boxPosition.Y <= bottomRightCorner.Y) { return(true); } } else if (box is RectangleCollisionBox) { if ((topRightCorner.X <= (box.AnchorPoint.X + box.AnchorObject.Position.X + otherOffset.X)) || (box.AnchorPoint.X + box.AnchorObject.Position.X + otherOffset.X + ((RectangleCollisionBox)box).Width <= topLeftCorner.X) || (bottomLeftCorner.Y <= (box.AnchorPoint.Y + box.AnchorObject.Position.Y + otherOffset.Y)) || (box.AnchorPoint.Y + box.AnchorObject.Position.Y + otherOffset.Y + ((RectangleCollisionBox)box).Height <= topLeftCorner.Y)) { return(false); } else { return(true); } } return(false); }
public override Vector2 ReboundInShallowest(Vector2 thisOffset, CollisionBox box, Vector2 otherOffset) { // might run into the +1 problem again so chekc here to make sure float xDist = 0; float yDist = 0; float reboundXDirection = 0; float reboundYDirection = 0; if (box is RectangleCollisionBox) { var currentBoxX = _anchorPoint.X + _anchorObject.Position.X + thisOffset.X; var boxX = box.AnchorPoint.X + box.AnchorObject.Position.X + otherOffset.X; if (currentBoxX < boxX) { // this means that other box is entirely within this box in the x direction if (currentBoxX + _width > boxX + ((RectangleCollisionBox)box).Width) { xDist = 0; reboundXDirection = 0; } else { xDist = boxX + ((RectangleCollisionBox)box).Width - currentBoxX; reboundXDirection = -1; } } else { // this means that this box is entirely within the width of the other in the x direction if (boxX + ((RectangleCollisionBox)box).Width > currentBoxX + _width) { xDist = 0; reboundXDirection = 0; } else { xDist = boxX + ((RectangleCollisionBox)box).Width - currentBoxX; reboundXDirection = -1; } } var currentBoxY = _anchorPoint.Y + _anchorObject.Position.Y + thisOffset.Y; var boxY = box.AnchorPoint.Y + box.AnchorObject.Position.Y + otherOffset.Y; if (currentBoxY < boxY) // this means that current is above other { // this means that other box is entirely within this box in the y direction if (currentBoxY + _height > boxY + ((RectangleCollisionBox)box).Height) { yDist = 0; reboundYDirection = 0; } else { yDist = currentBoxY + _height - boxY; reboundYDirection = -1; } } else { // this means that this box is entirely within the width of the other in the y direction if (boxY + ((RectangleCollisionBox)box).Height > currentBoxY + _height) { yDist = 0; reboundYDirection = 0; } else { yDist = boxY + ((RectangleCollisionBox)box).Height - currentBoxY; reboundYDirection = -1; } } } else if (box is CircleCollisionBox) { var currentBoxX = _anchorPoint.X + _anchorObject.Position.X + thisOffset.X; var currentBoxY = _anchorPoint.Y + _anchorObject.Position.Y + thisOffset.Y; var boxCenter = ((CircleCollisionBox)box).AnchorPoint + box.AnchorObject.Position + otherOffset; var boxRadius = ((CircleCollisionBox)box).Radius; if (boxCenter.X - boxRadius >= currentBoxX && boxCenter.X + boxRadius <= currentBoxX + _width) { xDist = 0; reboundXDirection = 0; } else if (boxCenter.X - boxRadius < currentBoxX) { xDist = boxCenter.X + boxRadius - currentBoxX; reboundXDirection = -1; } else { xDist = currentBoxX + _width - (boxCenter.X - boxRadius); reboundXDirection = 1; } if (boxCenter.Y - boxRadius >= currentBoxY && boxCenter.Y + boxRadius <= currentBoxY + _height) { yDist = 0; reboundYDirection = 0; } else if (boxCenter.Y - boxRadius < currentBoxY) { yDist = boxCenter.Y + boxRadius - currentBoxY; reboundYDirection = -1; } else { yDist = currentBoxY + _height - (boxCenter.Y - boxRadius); reboundYDirection = 1; } } if (xDist < yDist && xDist > 0) { return(new Vector2(xDist * reboundXDirection, 0)); } else { if (yDist > 0) { return(new Vector2(0, yDist * reboundYDirection)); } } return(new Vector2(0, 0)); }
public override bool Collide(Vector2 thisOffset, CollisionBox box, Vector2 otherOffset) { // might run into the +1 problem again so chekc here to make sure if (!CheckCollisionMasks(box)) { return(false); } if (box is CircleCollisionBox) { var radCombine = _radius + ((CircleCollisionBox)box).Radius; var distanceBetweenCenters = Vector2.Distance(_anchorPoint + _anchorObject.Position + thisOffset, box.AnchorPoint + box.AnchorObject.Position + otherOffset); if (distanceBetweenCenters <= radCombine) { return(true); } } else if (box is RectangleCollisionBox) { var width = ((RectangleCollisionBox)box).Width; var height = ((RectangleCollisionBox)box).Height; var topLeftCorner = box.AnchorPoint + box.AnchorObject.Position + otherOffset; var topRightCorner = box.AnchorPoint + box.AnchorObject.Position + otherOffset + new Vector2(width, 0); var bottomLeftCorner = box.AnchorPoint + box.AnchorObject.Position + otherOffset + new Vector2(0, height); var bottomRightCorner = box.AnchorPoint + box.AnchorObject.Position + otherOffset + new Vector2(width, height); var position = _anchorPoint + _anchorObject.Position + thisOffset; if (position.Y >= topLeftCorner.Y && position.Y <= bottomLeftCorner.Y && position.X <= topRightCorner.X && position.X >= topLeftCorner.X) { return(true); } if (Vector2.Distance(topLeftCorner, position) <= _radius || Vector2.Distance(topRightCorner, position) <= _radius || Vector2.Distance(bottomLeftCorner, position) <= _radius || Vector2.Distance(bottomRightCorner, position) <= _radius) { return(true); } if (topLeftCorner.Y - position.Y <= _radius && position.X >= topLeftCorner.X && position.X <= topRightCorner.X) { return(true); } if (position.Y - bottomRightCorner.Y <= _radius && position.X >= topLeftCorner.X && position.X <= topRightCorner.X) { return(true); } if (topLeftCorner.X - position.X <= _radius && position.Y >= topLeftCorner.Y && position.Y <= bottomRightCorner.Y) { return(true); } if (position.X - topRightCorner.X <= _radius && position.Y >= topLeftCorner.Y && position.Y <= bottomRightCorner.Y) { return(true); } } return(false); }
public override Vector2 ReboundInShallowest(Vector2 thisOffset, CollisionBox box, Vector2 otherOffset) { // might run into the +1 problem again so chekc here to make sure float xDist = 0; float yDist = 0; float reboundXDirection = 0; float reboundYDirection = 0; if (box is CircleCollisionBox) { var currentCenter = _anchorPoint + _anchorObject.Position + thisOffset; var boxCenter = box.AnchorPoint + box.AnchorObject.Position + otherOffset; var boxRadius = ((CircleCollisionBox)box).Radius; var distance = Vector2.Distance(currentCenter, boxCenter); // other circle is complete inside this one if (distance + boxRadius > 0 && distance < _radius) { // not sure what to do here } else if (distance + _radius > 0 && distance < boxRadius) // this is completely inside other? { // not sure what to do here either } if (currentCenter.X <= boxCenter.X) { xDist = currentCenter.X + _radius - (boxCenter.X - boxRadius); reboundXDirection = -1; } else { xDist = boxCenter.X + boxRadius - (currentCenter.X - _radius); reboundXDirection = 1; } if (currentCenter.Y <= boxCenter.Y) { yDist = currentCenter.Y + _radius - (boxCenter.Y - boxRadius); reboundYDirection = 1; } else { yDist = boxCenter.Y + boxRadius - (currentCenter.Y - _radius); reboundYDirection = -1; } } else if (box is RectangleCollisionBox) { var currentCenter = _anchorPoint + _anchorObject.Position + thisOffset; var boxLocation = box.AnchorPoint + box.AnchorObject.Position + otherOffset; var boxWidth = ((RectangleCollisionBox)box).Width; var boxHeight = ((RectangleCollisionBox)box).Height; // this means that square is inside circle on x if (boxLocation.X >= currentCenter.X - _radius && boxLocation.X + boxWidth <= currentCenter.X + _radius) { xDist = 0; reboundXDirection = 0; } else if (boxLocation.X < currentCenter.X - _radius) { xDist = boxLocation.X + boxWidth - (currentCenter.X - _radius); reboundXDirection = -1; } else { xDist = currentCenter.X + _radius - boxLocation.X; reboundXDirection = 1; } if (boxLocation.Y >= currentCenter.Y - _radius && boxLocation.Y + boxHeight <= currentCenter.Y + _radius) { yDist = 0; reboundYDirection = 0; } else if (boxLocation.Y < currentCenter.Y - _radius) { yDist = boxLocation.Y + boxHeight - (currentCenter.Y - _radius); reboundYDirection = -1; } else { yDist = currentCenter.Y + _radius - boxLocation.Y; reboundYDirection = 1; } } if (xDist < yDist && xDist > 0) { return(new Vector2(xDist * reboundXDirection, 0)); } else { if (yDist > 0) { return(new Vector2(0, yDist * reboundYDirection)); } } return(new Vector2(0, 0)); }
public bool CheckCollisionMasks(CollisionBox box) { return(((_collisionMask & box.AnchorObject.MaskType) == box.AnchorObject.MaskType) && ((box.CollisionMask & _anchorObject.MaskType) == _anchorObject.MaskType)); }
public abstract List <Directions> GetCollisionDirections(Vector2 thisOffset, CollisionBox box, Vector2 otherOffset);
public abstract Vector2 ReboundInDirection(Vector2 thisOffset, CollisionBox box, Vector2 otherOffset, Directions dir);
public abstract Vector2 ReboundInShallowest(Vector2 thisOffset, CollisionBox box, Vector2 otherOffset);
public abstract bool Collide(Vector2 thisOffset, CollisionBox otherBox, Vector2 otherOffset);