Hello Guest

Author Topic: tk2dCamera.ScreenExtents incorrect when ZoomFactor not 1?  (Read 5845 times)

Velvety

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Velvety Couch Games
tk2dCamera.ScreenExtents incorrect when ZoomFactor not 1?
« on: August 21, 2013, 05:20:46 am »
I'm not sure if I am missing something here or if there is actually a problem.  I am changing my tk2dCamera.ZoomFactor to zoom in and out of my scene. This works fine.  I am also moving my tk2dCamera position around to pan the scene.  This also works fine.

But when I use tk2dCamera.ScreenExtents to keep the camera within a bounds I am running into a problem.  tk2dCamera.ScreenExtents seem to be correct when my ZoomFactor is 1.  However, when I zoom in or out (ZoomFactor becomes something like 0.625 or 1.25, etc) the tk2dCamera.ScreenExtents no longer seems accurate and my bounds are not restrained correctly.

I've tested this by creating a cube with the same size of the ScreenExtents and positioning it center with the camera. At ZoomFactor 1 the tk2dCamera.ScreenExtents returns 16 width x 12 height and a cube of the same size matches the camera bounds I see in the editor's scene view.  But if my ZoomFactor is 0.625 tk2dCamera.ScreenExtents returns 21.33 width x 16 height and this does NOT match a cube of the same size.  A cube closer to 25.6 x 19.2 looks accurate.

Am I missing something fundamental here or is tk2dCamera.ScreenExtents not returning the actual correct size of the camera bounds?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dCamera.ScreenExtents incorrect when ZoomFactor not 1?
« Reply #1 on: August 21, 2013, 11:54:20 am »
I can't reproduce this. It seems to work fine for me.
Please refer to attached unitypackage.

Velvety

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Velvety Couch Games
Re: tk2dCamera.ScreenExtents incorrect when ZoomFactor not 1?
« Reply #2 on: August 21, 2013, 03:44:02 pm »
Thanks, you are right it does work in your package.  After seeing that I did some more digging.  My problem may have something to do with how I am trying to constrain my camera within bounds.

Here is what I'm doing and what I've found:
1. In my Update() loop I process my mouse scrolling and zoom the camera ZoomFactor accordingly
2. After zooming I immediately check my bounds and correct the camera if it is outside of the bounds (when I do this all the calculations seem correct and the camera should be correctly adjusted. I even create a cube with the camera extents at this point to visually see that it is within the bounds - looks correct)
3. Shortly afterwards something calls tk2dCamera.OnPreCull() and that calls UpdateCameraMatrix() which changes the camera extents.  This changes the camera to display areas outside of my bounds. I am not sure what is causing this, any insights into that?
« Last Edit: August 21, 2013, 04:05:29 pm by Velvety »

Velvety

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Velvety Couch Games
Re: tk2dCamera.ScreenExtents incorrect when ZoomFactor not 1?
« Reply #3 on: August 21, 2013, 04:42:13 pm »
So it looks like OnPreCull() is a Unity thing that is called before any camera determines what objects to draw in the frame.

Since I am only trying to call my bounds fixing function once for every zooming change, I am not catching the case of OnPreCull() being called afterwards and changing the extents again.  It seems like the extents are being updated every frame because they are recalculated from OnPreCull(), is that right?  I guess I will need to call my bounds fixing function every time that the extents change, so every frame.  I will give that a try and see if it fixes my issue.

If you have any suggestions on how I might call my bounds fixing function less often let me know.  Thanks.
« Last Edit: August 21, 2013, 04:57:33 pm by Velvety »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dCamera.ScreenExtents incorrect when ZoomFactor not 1?
« Reply #4 on: August 21, 2013, 05:00:00 pm »
Ok. I can see how this can break with what you're doing. Can you try calling tk2dCamera.UpdateCameraMatrix before your code?

Velvety

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Velvety Couch Games
Re: tk2dCamera.ScreenExtents incorrect when ZoomFactor not 1?
« Reply #5 on: August 21, 2013, 05:06:56 pm »
Ya, that might be a cleaner way of fixing it for me.  Thanks for the suggestion I'll give it a try tonight when I get home! Will that be wasteful though, since tk2dCamera.UpdateCameraMatrix will be called at least twice per frame now?  Is there any kind of "isDirty" flag that is set to prevent it from reupdating the matrix multiple times?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dCamera.ScreenExtents incorrect when ZoomFactor not 1?
« Reply #6 on: August 21, 2013, 05:12:16 pm »
No there isn't at the moment, but I'll probably add one to identify current frame.

You only have one camera though, and UpdateCameraMatrix doesn't really do all that much, so you should be fine calling it more than once :)