I have this code:
foreach (CollisionTiles tile in map.CollisionTiles)
{
player.CollisionBox(tile);
foreach (Enemy enemy in enemys)
enemy.CollisionBox(tile);
}
This is the CollisionBox()
function:
public void CollisionBox(CollisionTiles tile)
{
int x = rectangle.X / Map.Size;
int y = rectangle.Y / Map.Size;
int tileX = (int)tile.Position.X;
int tileY = (int)tile.Position.Y;
if (tileX >= x - 1 && tileX <= x + 2 && tileY >= y - 1 && tileY <= y + 3)
{
Collision(tile.Rectangle, Map.Width, Map.Height);
tile.Collide = true;
}
}
These two lines appear to be the problem:
int x = rectangle.X / Map.Size;
int y = rectangle.Y / Map.Size;
I guess because it loops through them on every tile...
The Collision() function just check the face's of the two triangles (both player/enemy and the tiles) with these functions:
public static bool TouchTopOf(this Rectangle r1, Rectangle r2)
{
return (r1.Bottom >= r2.Top && r1.Bottom <= r2.Top + (r2.Height / 2));
}
public static bool TouchBottomOf(this Rectangle r1, Rectangle r2)
{
return (r1.Top >= r2.Bottom - (r2.Height / 2));
}
public static bool TouchLeftOf(this Rectangle r1, Rectangle r2)
{
return (r1.Top <= r2.Bottom - (r2.Height / 2) &&
r1.Bottom >= r2.Top + (r2.Height / 2) &&
r1.Right >= r2.Left &&
r1.Right <= r2.Left + (r2.Width / 4));
}
public static bool TouchRightOf(this Rectangle r1, Rectangle r2)
{
return (r1.Top <= r2.Bottom - (r2.Height / 2) &&
r1.Bottom >= r2.Top + (r2.Height / 2) &&
r1.Left >= r2.Right - (r2.Width / 4) &&
r1.Left <= r2.Right);
}
When there is more then one monster with the player I get a massive FPSdrop, and it's getting worse when more monsters get added. What should I do to fix that problem?