Hello Guest

Author Topic: Clamping tk2dCamera to the bounds of a tk2dTilemap  (Read 4799 times)

andrew2110

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Clamping tk2dCamera to the bounds of a tk2dTilemap
« on: July 24, 2014, 02:26:00 pm »
As the title suggests, I'm looking for a way to clamp the position of my camera so it never leaves the bounds of the tile map. At present, the game follows around an avatar but when the avatar reaches the edges of the map obviously you can see off the side of the tilemap.

It's because of a lack of understanding over everything I'm having some issues of this. Through trial and error I worked out that to get the current tile my avatar is occupying I had to do:

int tileX = (int) (pos.x/2.5);
int tileY = (int) (pos.y/2.5);

This works ok, I'd be happier if I knew why I divided by 2.5 to get the current tileX and tileY. My tile size in pixels is 80 x 80, the cameras orthographic size is 10, zoom factor 1. I can't quite figure out from those numbers where the 2.5 comes from, but if I manually move my avatar to tile 64x64 and observe its X,Y position, I divide that number by 2.5 to get to 64,64

With that in mind, I started trying to clamp the camera to the bounds of the tilemap with:

camPos.x =  Mathf.Clamp(camPos.x, 0.0f+(Screen.width/2*(1/this.globalCamera.orthographicSize)*(1/2.5f)), 2.5f*(this.explorer.tileMap.width-1) );
camPos.y =  Mathf.Clamp(camPos.y, 0.0f+(Screen.height/2*(1/this.globalCamera.orthographicSize)*(1/2.5f)), 2.5f*(this.explorer.tileMap.height-1) );

(trying to restrict from going too far left, too far down to start off with). Any advice anyone could offer would be greatly appreciated!
      


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Clamping tk2dCamera to the bounds of a tk2dTilemap
« Reply #1 on: July 25, 2014, 06:58:23 pm »
It'll be much more robust if you work this out based on world space bounds.

First you need the world space bound of the tilemap. The 2 relevant bits for this are:
      Vector3 p0 = tileMapData.tileOrigin;
      Vector3 p1 = new Vector3(p0.x + tileMapData.tileSize.x * tileMap.width, p0.y + tileMapData.tileSize.y * tileMap.height, 0);
Where p0 = bottom left, and p1 is the top right extent of the tilemap.
This will work regardless of the size of the sprite in the collection, etc...

Tilemap.ScreenExtents when the camera is at 0, 0 will return the world space min and max xy values it can see. You just need to work out the position of the camera so that it doesn't go beyond p0 and p1.

andrew2110

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Clamping tk2dCamera to the bounds of a tk2dTilemap
« Reply #2 on: July 26, 2014, 10:33:53 am »
Thank you! This now works perfectly :)

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
Re: Clamping tk2dCamera to the bounds of a tk2dTilemap
« Reply #3 on: July 08, 2015, 06:44:08 pm »
I'm not able to get this working correctly. I obtain p0 and p1 just as you have it, but I'm not understanding how they come into play. I sort of get how ScreenExtents is used to determine which minimum values to use to clamp the x and y axis of the camera. I guess I don't see how to obtain the maximum values. 

Does anyone have a full working example of this?

« Last Edit: July 08, 2015, 07:02:08 pm by Neeko »