Skip to main content
replaced http://gamedev.stackexchange.com/ with https://gamedev.stackexchange.com/
Source Link

I have a tilemap which moves

First of all you start from wrong way. Player should move inside world and not world should mo around player. It's not a Futurama. In other words do

player.Position.X += dx

instead of

world.Position.X -= dx

It's mutch cleaner solution.

Next. Let's you have viewRect that is your visible part of world

Rectangle viewRect = new Rectangle(0, 0, screen_width, screen_height);

And you have your player position of tyte Vector2. You can simply calculate current view:

Rectangle currentViewRect = new Rectangle(
    viewRect.X + player.Position.X + drawOffset.X,
    viewRect.Y + player.Position.Y + drawOffset.Y,
    viewRect.Width,
    viewRect.Height);

Then you can determine visible tiles like I in my answer on thisthis question.

In the same way you can determine tiles that you characted potentialy can intersetc and check only them. You use grid for keep tiles, so if you have coord in pixels you can find coord of grid cell.

Vector2 minTileCoord = new Vector2(
    (int)player.Position.X / tileWidth,
    (int)player.Position.Y / tileHeight);
Vector2 maxTileCoord = new Vector2(
    (int)(player.Position.X + player.Width / tileWidth,
    (int)(player.Position.Y + player.Height) / tileHeight);

And now you need only check tiles from maxTileCoord.X to maxTileCoord.Y by X and maxTileCoord.Y to maxTileCoord.Y by Y axis.

I have a tilemap which moves

First of all you start from wrong way. Player should move inside world and not world should mo around player. It's not a Futurama. In other words do

player.Position.X += dx

instead of

world.Position.X -= dx

It's mutch cleaner solution.

Next. Let's you have viewRect that is your visible part of world

Rectangle viewRect = new Rectangle(0, 0, screen_width, screen_height);

And you have your player position of tyte Vector2. You can simply calculate current view:

Rectangle currentViewRect = new Rectangle(
    viewRect.X + player.Position.X + drawOffset.X,
    viewRect.Y + player.Position.Y + drawOffset.Y,
    viewRect.Width,
    viewRect.Height);

Then you can determine visible tiles like I in my answer on this question.

In the same way you can determine tiles that you characted potentialy can intersetc and check only them. You use grid for keep tiles, so if you have coord in pixels you can find coord of grid cell.

Vector2 minTileCoord = new Vector2(
    (int)player.Position.X / tileWidth,
    (int)player.Position.Y / tileHeight);
Vector2 maxTileCoord = new Vector2(
    (int)(player.Position.X + player.Width / tileWidth,
    (int)(player.Position.Y + player.Height) / tileHeight);

And now you need only check tiles from maxTileCoord.X to maxTileCoord.Y by X and maxTileCoord.Y to maxTileCoord.Y by Y axis.

I have a tilemap which moves

First of all you start from wrong way. Player should move inside world and not world should mo around player. It's not a Futurama. In other words do

player.Position.X += dx

instead of

world.Position.X -= dx

It's mutch cleaner solution.

Next. Let's you have viewRect that is your visible part of world

Rectangle viewRect = new Rectangle(0, 0, screen_width, screen_height);

And you have your player position of tyte Vector2. You can simply calculate current view:

Rectangle currentViewRect = new Rectangle(
    viewRect.X + player.Position.X + drawOffset.X,
    viewRect.Y + player.Position.Y + drawOffset.Y,
    viewRect.Width,
    viewRect.Height);

Then you can determine visible tiles like I in my answer on this question.

In the same way you can determine tiles that you characted potentialy can intersetc and check only them. You use grid for keep tiles, so if you have coord in pixels you can find coord of grid cell.

Vector2 minTileCoord = new Vector2(
    (int)player.Position.X / tileWidth,
    (int)player.Position.Y / tileHeight);
Vector2 maxTileCoord = new Vector2(
    (int)(player.Position.X + player.Width / tileWidth,
    (int)(player.Position.Y + player.Height) / tileHeight);

And now you need only check tiles from maxTileCoord.X to maxTileCoord.Y by X and maxTileCoord.Y to maxTileCoord.Y by Y axis.

Source Link
user52551
user52551

I have a tilemap which moves

First of all you start from wrong way. Player should move inside world and not world should mo around player. It's not a Futurama. In other words do

player.Position.X += dx

instead of

world.Position.X -= dx

It's mutch cleaner solution.

Next. Let's you have viewRect that is your visible part of world

Rectangle viewRect = new Rectangle(0, 0, screen_width, screen_height);

And you have your player position of tyte Vector2. You can simply calculate current view:

Rectangle currentViewRect = new Rectangle(
    viewRect.X + player.Position.X + drawOffset.X,
    viewRect.Y + player.Position.Y + drawOffset.Y,
    viewRect.Width,
    viewRect.Height);

Then you can determine visible tiles like I in my answer on this question.

In the same way you can determine tiles that you characted potentialy can intersetc and check only them. You use grid for keep tiles, so if you have coord in pixels you can find coord of grid cell.

Vector2 minTileCoord = new Vector2(
    (int)player.Position.X / tileWidth,
    (int)player.Position.Y / tileHeight);
Vector2 maxTileCoord = new Vector2(
    (int)(player.Position.X + player.Width / tileWidth,
    (int)(player.Position.Y + player.Height) / tileHeight);

And now you need only check tiles from maxTileCoord.X to maxTileCoord.Y by X and maxTileCoord.Y to maxTileCoord.Y by Y axis.