Hello Guest

Author Topic: Two Questions (Camera / Tilebased Movement)  (Read 16626 times)

fryedrycestyle

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Two Questions (Camera / Tilebased Movement)
« Reply #15 on: January 20, 2013, 10:53:30 pm »
Hey, so I'm still having some minor annoyances with the camera:

If you look at this gallery you'll see what I mean:

http://minus.com/mbrRDDKMQAHr2L

Essentially I have this code to check the bounds of the map (so the camera doesn't pass them):

Code: [Select]
void pCamera()
{

tk2dCamera.inst.mainCamera.transform.position = new Vector3(this.transform.position.x - tk2dCamera.inst.nativeResolutionWidth / 2,
this.transform.position.y - tk2dCamera.inst.nativeResolutionHeight / 2, tk2dCamera.inst.transform.position.z);

if (tk2dCamera.inst.transform.position.x < 0)
tk2dCamera.inst.transform.position = new Vector3(map.width - map.partitionSizeX * map.width, tk2dCamera.inst.transform.position.y, tk2dCamera.inst.transform.position.z);
if (tk2dCamera.inst.transform.position.y < 0)
tk2dCamera.inst.transform.position = new Vector3(tk2dCamera.inst.transform.position.x, map.height - map.partitionSizeY * map.height, tk2dCamera.inst.transform.position.z);

if (tk2dCamera.inst.transform.position.x > map.partitionSizeX * map.width - tk2dCamera.inst.nativeResolutionWidth)
tk2dCamera.inst.transform.position = new Vector3(map.partitionSizeX * map.width - tk2dCamera.inst.nativeResolutionWidth, tk2dCamera.inst.transform.position.y, tk2dCamera.inst.transform.position.z);
if (tk2dCamera.inst.transform.position.y > map.partitionSizeY * map.height - tk2dCamera.inst.nativeResolutionHeight)
tk2dCamera.inst.transform.position = new Vector3(tk2dCamera.inst.transform.position.x, map.partitionSizeY * map.height - tk2dCamera.inst.nativeResolutionHeight, tk2dCamera.inst.transform.position.z);
}

As you can see in the linked image gallery, when I go to the bottom (and also the left hand side of the map), I do not see the complete tiles on the edges of the tile map. However, when I go to right (and also the top of the map) I see past the edge of the tilemap.

Any idea for possible fixes?

Thanks again!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Two Questions (Camera / Tilebased Movement)
« Reply #16 on: January 22, 2013, 11:27:05 am »
This is likely to be because your calculation is perhaps ignoring the tile offset? How is the anchor set up on the sprite? Is it bottom left?

It almost looks like the whole thing is offset by 1/2 a tile. You can easily fix this by offsetting by half a tile dimension, or change the offset in the tile itself. The first solution is probably quicker to implement.

You can get the bounds by looking in tileMap.data.tileSize.

fryedrycestyle

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Two Questions (Camera / Tilebased Movement)
« Reply #17 on: January 22, 2013, 10:59:19 pm »
It was an issue with the tile's anchors. Derp.

Thanks though!

fryedrycestyle

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Two Questions (Camera / Tilebased Movement)
« Reply #18 on: February 04, 2013, 03:25:16 am »
Sorry for reviving the thread but I've upgraded to 1.9 and now my camera is acting up again. Essentially the problem from a few posts up (seeing past the edge of the map) has become true again... but only on the X axis.

Code: [Select]
tk2dCamera.inst.mainCamera.transform.position = new Vector3(this.transform.position.x - tk2dCamera.inst.nativeResolutionWidth / 2,
this.transform.position.y - tk2dCamera.inst.nativeResolutionHeight / 2, tk2dCamera.inst.transform.position.z);


if (tk2dCamera.inst.transform.position.x < 0)
tk2dCamera.inst.transform.position = new Vector3(0, tk2dCamera.inst.transform.position.y, tk2dCamera.inst.transform.position.z);
if (tk2dCamera.inst.transform.position.y < 0)
tk2dCamera.inst.transform.position = new Vector3(tk2dCamera.inst.transform.position.x, 0, tk2dCamera.inst.transform.position.z);

if (tk2dCamera.inst.transform.position.x > map.data.tileSize.x * map.width - tk2dCamera.inst.nativeResolutionWidth)
tk2dCamera.inst.transform.position = new Vector3(map.data.tileSize.x * map.width - tk2dCamera.inst.nativeResolutionWidth, tk2dCamera.inst.transform.position.y, tk2dCamera.inst.transform.position.z);

if (tk2dCamera.inst.transform.position.y > map.data.tileSize.y * map.height - tk2dCamera.inst.nativeResolutionHeight)
tk2dCamera.inst.transform.position = new Vector3(tk2dCamera.inst.transform.position.x, map.data.tileSize.y * map.height - tk2dCamera.inst.nativeResolutionHeight, tk2dCamera.inst.transform.position.z);

The code is pretty much the same, just using tile data instead of partition size. Either way it has not made a difference which I use. My tile sheet sprites are set to have a lower left anchor as suggested, but like I said the problem has popped up again, and only on the x-axis.

Any suggestions?

Thanks again.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Two Questions (Camera / Tilebased Movement)
« Reply #19 on: February 04, 2013, 09:52:00 am »
Can you set up a simple test case for me? I'd like to try and work out what has changed (nothing should have changed and thats why I worry slightly). support@unikronsoftware.com if you can.

fryedrycestyle

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Two Questions (Camera / Tilebased Movement)
« Reply #20 on: February 05, 2013, 01:49:49 am »
Sure, I can try to send you something tomorrow.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Two Questions (Camera / Tilebased Movement)
« Reply #21 on: February 05, 2013, 06:38:10 pm »
Thanks for your sample, it would have taken a LONG time to work out otherwise.

The reason the sample worked before, is likely that your "Force Editor Resolution" was set to the same aspect ratio as the native resolution. The calculation is still correct, just that the fitting algorithm now displays a bit more in the horizontal axis than before.

If you set auto-scale to "Stretch to Fit" your image should be correct, though it will be stretched horizontally to fit the screen.

You can get the exact "extents" of the visible screen at the current adjusted resolution using tk2dCamera.ScreenExtents. Your code modified to work at any resolution and/or scaling option is below:

Code: [Select]
tk2dCamera.inst.mainCamera.transform.position = new Vector3(this.transform.position.x - tk2dCamera.inst.nativeResolutionWidth / 2,
this.transform.position.y - tk2dCamera.inst.nativeResolutionHeight / 2, tk2dCamera.inst.transform.position.z);

Rect extents = tk2dCamera.inst.ScreenExtents;
if (tk2dCamera.inst.transform.position.x < -extents.xMin)
tk2dCamera.inst.transform.position = new Vector3(-extents.xMin, tk2dCamera.inst.transform.position.y, tk2dCamera.inst.transform.position.z);
if (tk2dCamera.inst.transform.position.y < -extents.yMax)
tk2dCamera.inst.transform.position = new Vector3(tk2dCamera.inst.transform.position.x, -extents.yMax, tk2dCamera.inst.transform.position.z);

if (tk2dCamera.inst.transform.position.x > map.data.tileSize.x * map.width - extents.xMax)
tk2dCamera.inst.transform.position = new Vector3(map.data.tileSize.x * map.width - extents.xMax, tk2dCamera.inst.transform.position.y, tk2dCamera.inst.transform.position.z);

if (tk2dCamera.inst.transform.position.y > map.data.tileSize.y * map.height - extents.yMin)
tk2dCamera.inst.transform.position = new Vector3(tk2dCamera.inst.transform.position.x, map.data.tileSize.y * map.height - extents.yMin, tk2dCamera.inst.transform.position.z);

Also note you should cache tk2dCamera.inst.transform.position and use it instead of accessing it every time

fryedrycestyle

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Two Questions (Camera / Tilebased Movement)
« Reply #22 on: February 06, 2013, 02:51:54 am »
Excellent, thanks! And yes you're probably right about the caching.