/// <summary> /// Detects the Entity Collision /// Returns 0 if no Collision /// </summary> protected int CollisionDetection(Vector2f vEntityPosition, uint uLength, uint uHeight) { Vector2f vEntityPos = vEntityPosition - MainMap.GetTileMapPosition(); vEntityPositionBottomLeft.Y = vEntityPos.Y + uHeight; vEntityPositionTopRight.X = vEntityPos.X + uLength; int iTileNearY = (int)vEntityPos.Y / 50; int iTileNearX = (int)vEntityPos.X / 50; if (iTileNearY < 0) { iTileNearY++; } if (iTileNearX < 0) { iTileNearX++; } for (int y = iTileNearY; y < iTileNearY + 2; y++) { for (int x = iTileNearX; x < iTileNearX + 2; x++) { // COLLISIONDETECTION ON ENTITY BORDER if (TileArrayCreation.CollisionReturner(x, y)) { if (((vEntityPos.Y < (y + 1) * 50 && vEntityPos.Y > y * 50 - 1) || (vEntityPos.Y < y * 50 && vEntityPos.Y > (y - 1) * 50))) { if (vEntityPos.X <= (x + 1) * 50 && vEntityPos.X >= x * 50) { return(1); } else if (vEntityPositionTopRight.X >= x * 50 && vEntityPositionTopRight.X <= (x + 1) * 50) { return(2); } } if (((vEntityPos.X < (x + 1) * 50 && vEntityPos.X > x * 50 - 1) || (vEntityPositionTopRight.X > x * 50 && vEntityPositionTopRight.X < (x + 1) * 50))) { if (vEntityPos.Y <= (y + 1) * 50 && vEntityPos.Y >= y * 50) { return(3); } else if (vEntityPositionBottomLeft.Y >= y * 50 && vEntityPositionBottomLeft.Y <= (y + 1) * 50) { return(4); } } } } } return(0); }
/// <summary> /// Returns a bool for specified x and y coordinates. If those coordinates are unused, a false is returned. /// </summary> /// <param name="xCoord"></param> /// <param name="yCoord"></param> /// <returns></returns> public bool GetCollisionAt(int xCoord, int yCoord) { return(TileArrayCreation.CollisionReturner(xCoord, yCoord)); }
/// <summary> /// Updates possible directions of movement based on Collisiondetection and relocates Entity if Borders were crossed /// </summary> /// <param name="vEntityPosition">Position to be checked Collision with Tiles</param> /// <param name="up">Disallows Character's up Movement</param> /// <param name="down">Disallows Character's down Movement</param> /// <param name="right">Disallows Character's right Movement</param> /// <param name="left">Disallows Character's left Movement</param> /// <param name="SizeX">Width of the Character</param> /// <param name="SizeY">Height of the Character</param> protected void CollisionDetection(ref Vector2f vEntityPosition, ref bool up, ref bool down, ref bool right, ref bool left, float SizeX, float SizeY) { float Size; if (SizeX >= SizeY) { Size = SizeX; } else { Size = SizeY; } vEntityPositionBottomLeft.Y = vEntityPosition.Y + Size; vEntityPositionTopRight.X = vEntityPosition.X + Size; PlayerTileCollision = false; int iTileNearY = (int)vEntityPosition.Y / 50 - 1; int iTileNearX = (int)vEntityPosition.X / 50 - 1; if (iTileNearY < 0) { iTileNearY++; } if (iTileNearX < 0) { iTileNearX++; } for (int y = iTileNearY; y < iTileNearY + 3; y++) { for (int x = iTileNearX; x < iTileNearX + 3; x++) { //COLLISIONDETECTION ON CHARACTERSPRITE BORDER if (TileArrayCreation.CollisionReturner(x, y)) { if (((vEntityPosition.Y < (y + 1) * 50 && vEntityPosition.Y > y * 50 - 1) || (vEntityPosition.Y < y * 50 && vEntityPosition.Y > (y - 1) * 50))) { if (vEntityPosition.X <= (x + 1) * 50 && vEntityPosition.X >= x * 50) { left = true; vChracterPositionSpace.X = (x + 1) * 50; PlayerTileCollision = true; } else if (vEntityPositionTopRight.X >= x * 50 && vEntityPositionTopRight.X <= (x + 1) * 50) { right = true; vChracterPositionSpace.X = (x - 1) * 50; PlayerTileCollision = true; } } if (((vEntityPosition.X < (x + 1) * 50 && vEntityPosition.X > x * 50 - 1) || (vEntityPositionTopRight.X > x * 50 && vEntityPositionTopRight.X < (x + 1) * 50))) { if (vEntityPosition.Y <= (y + 1) * 50 && vEntityPosition.Y >= y * 50) { up = true; vChracterPositionSpace.Y = (y + 1) * 50; PlayerTileCollision = true; } else if (vEntityPositionBottomLeft.Y >= y * 50 && vEntityPositionBottomLeft.Y <= (y + 1) * 50) { down = true; vChracterPositionSpace.Y = (y - 1) * 50; PlayerTileCollision = true; } } } //REPLACEMENT OF PLAYERLOCATION IN CASE OF CROSSING BORDER OF OBJECT if (PlayerTileCollision) { if (up && right) { if (vEntityPosition.X - vChracterPositionSpace.X < vChracterPositionSpace.Y - vEntityPosition.Y) { vEntityPosition.X = vChracterPositionSpace.X; } else { vEntityPosition.Y = vChracterPositionSpace.Y; } break; } if (up && left) { if (vChracterPositionSpace.X - vEntityPosition.X < vChracterPositionSpace.Y - vEntityPosition.Y) { vEntityPosition.X = vChracterPositionSpace.X; } else { vEntityPosition.Y = vChracterPositionSpace.Y; } break; } if (down && left) { if (vChracterPositionSpace.X - vEntityPosition.X < vEntityPosition.Y - vChracterPositionSpace.Y) { vEntityPosition.X = vChracterPositionSpace.X; } else { vEntityPosition.Y = vChracterPositionSpace.Y; } break; } if (down && right) { if (vEntityPosition.X - vChracterPositionSpace.X < vEntityPosition.Y - vChracterPositionSpace.Y) { vEntityPosition.X = vChracterPositionSpace.X; } else { vEntityPosition.Y = vChracterPositionSpace.Y; } break; } } } } }