Reputation: 559
I'm trying to make a 2D tile based platformer in monogame. In fulscreen mode, when too many tiles are on screen the frame rate goes down and the game is moving slow. I don't know what causes these slow downs. If this is the update, the rendering of the tiles, or even the collision detection. Maybe some one can help me here to improve my code.
for each tile I have an own tile class which stores the animation, position, type and so on. In my Level class I have a list of tiles. Updates are made this way:
private void UpdateTiles()
{
Rectangle oldViewArea = Basic.camera.OldViewArea;
Rectangle viewArea = Basic.camera.ViewArea;
if (viewArea != oldViewArea)
{
tiles.Clear();
float vaWidth = viewArea.X + viewArea.Width;
float vaHeight = viewArea.Y + viewArea.Height;
int xStart = viewArea.X / Consts.TileWidth;
int yStart = viewArea.Y / Consts.TileHeight;
int xEnd = ((int)Math.Ceiling(vaWidth) / Consts.TileWidth) + 1;
int yEnd = ((int)Math.Ceiling(vaHeight) / Consts.TileHeight) + 1;
//clamp cam boundaries
//if (xStart < 0) xStart = 0;
//if (yStart < 0) yStart = 0;
//if (xEnd > currentMap.GetLength(1)) xEnd = currentMap.GetLength(1);
//if (yEnd > currentMap.GetLength(0)) yEnd = currentMap.GetLength(0);
for (int y = yStart; y < yEnd; ++y)
{
for (int x = xStart; x < xEnd; ++x)
{
AddTile(y, x);
}
}
}
}
private void AddTile(int y, int x)
{
int id = currentMap[y, x];
if (id > 0)
{
Rectangle location = new Rectangle(x * Consts.TileWidth, y * Consts.TileHeight, Consts.TileWidth, Consts.TileHeight);
Tile tile = new Tile(Textures.tiles, location, (TileType)id, x, y);
tiles.Add(tile);
}
}
My Render method is as follows:
private void RenderTiles()
{
for (int i = 0; i < tiles.Count; i++)
{
tiles[i].Render();
}
}
There is nothing special behind. Only a simple spriteBatch Draw:
Basic.spriteBatch.Draw(
texture,
location,
sourceRect,
Color.White
);
And this is my collision detection in the player class. It gets triggered once when the player moves or every update for ground detection. Vector2 v here is the velocity that would be added. When the player is moving on the x-axis, the Y component is 0. Otherwise when checking for ground collision, component X is 0.
private void Move(Vector2 v)
{
bool collidedX = false;
bool collidedY = false;
Rectangle collider = boundingBox;
if (v.X > 0)
{
collider.X += (int)Math.Ceiling(v.X);
}
else if (v.X < 0)
{
collider.X += (int)(v.X - offsetX);
}
collider.Y += (int)Math.Ceiling(v.Y);
IEnumerable<Entity> collidingTiles = GameScreen.level.tiles.Where(e => e.Collision &&
e.BoundingBox.Intersects(collider));
if (collidingTiles.Any())
{
foreach (Entity e in collidingTiles)
{
if (v.X != 0)
{
collidedX = CollisionX(boundingBox, e.BoundingBox, v);
}
if (v.Y != 0)
{
collidedY = CollisionY(boundingBox, e.BoundingBox, v);
}
}
}
if (!collidedX)
{
position.X += v.X;
}
if (!collidedY)
{
if (v.Y != 0)
{
isOnGround = false;
}
position.Y += v.Y;
}
}
private bool CollisionX(Rectangle r1, Rectangle r2, Vector2 v)
{
if (r1.CollidedWithLeftOf(r2, v))
{
position.X = r2.X - r1.Width - offsetX;
velocity.X = 0;
animType = AnimationType.Idle;
return true;
}
if (r1.CollidedWithRightOf(r2, v))
{
position.X = r2.X + r2.Width - offsetX;
velocity.X = 0;
animType = AnimationType.Idle;
return true;
}
return false;
}
private bool CollisionY(Rectangle r1, Rectangle r2, Vector2 v)
{
if (r1.CollidedWithBottomOf(r2, v))
{
if (velocity.Y < 0) //velocity.Y = 1f;
{
velocity.Y = 0;
}
return true;
}
if (r1.CollidedWithTopOf(r2, v))
{
isOnGround = true;
velocity.Y = 0;
position.Y = r2.Y - r1.Height - offsetY;
return true;
}
return false;
}
And the collision detection itself:
public static bool CollidedWithTopOf(this Rectangle r1, Rectangle r2, Vector2 v)
{
return (r1.Bottom + v.Y > r2.Top &&
r1.Top < r2.Top &&
r1.Right > r2.Left &&
r1.Left < r2.Right);
}
public static bool CollidedWithBottomOf(this Rectangle r1, Rectangle r2, Vector2 v)
{
return (r1.Top + v.Y < r2.Bottom &&
r1.Bottom > r2.Bottom &&
r1.Right > r2.Left &&
r1.Left < r2.Right);
}
public static bool CollidedWithLeftOf(this Rectangle r1, Rectangle r2, Vector2 v)
{
return (r1.Right + v.X > r2.Left &&
r1.Left < r2.Left &&
r1.Bottom > r2.Top &&
r1.Top < r2.Bottom);
}
public static bool CollidedWithRightOf(this Rectangle r1, Rectangle r2, Vector2 v)
{
return (r1.Left + v.X < r2.Right &&
r1.Right > r2.Right &&
r1.Bottom > r2.Top &&
r1.Top < r2.Bottom);
}
The slowdowns start when there are only about 40 tiles on screen. I don't know why this is happening. I guess either my Update method costs too much, clearing the list of tile classes and building it up - or my collision detection is very slow. Maybe someone could help how to improve the code.
Thank you very much!
EDIT: I just implemented a FrameCounter. It seems that the UpdateTiles method causes the framedrop. If I turn if (viewArea != oldViewArea)
to if (true)
, I get contant slowdowns. The framerate sometimes is only 2!
Upvotes: 0
Views: 45