Slowdown in 2D tile based platformer

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!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật