Hello Guest

Author Topic: How do you clamp the X and Y of a scene viewable area?  (Read 7785 times)

Disastercake

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Disastercake
How do you clamp the X and Y of a scene viewable area?
« on: May 14, 2012, 07:47:06 am »
I'm trying to set up a viewing clamp on the top, bottom, left, and right of my 2D world map.  I want the user to be able to zoom in and out ( which is completed), and pan the camera (completed).  But I don't want them to see outside a specific boundary.  How would the math and programming look behind calculating if the user is seeing outside of the level boundary?  Since the user can zoom in and out that starts to get a little complicated, and I'm not sure how to approach this.

Any help would be greatly appreciated.


The information I've gathered is the position of the camera for each side at the largest and smallest zoom.  I imagine I need to have some sort of calculation that can figure the in-between numbers that are acceptable.

Side: Smallest /// Largest
Top (Y): 11.65808 /// 6.277246
Bottom (Y): -9.491721 /// -0.1629976
Left (X): 18.12113 /// 7.855795
Right (X): -18.29644 /// -6.274414

I also wonder if there's a more simple ray casting that can be used.  Like making 4 boxes that surround the viewable area, and if the camera corners ray cast into any of them, the camera snaps back to where it should be depending on the box it ray cast on.

However, I'm not sure how the code would look for this raycasting.
Working on Soul Saga, an upcoming anime adventure RPG game by Disastercake.
www.disastercake.com

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #1 on: May 14, 2012, 08:57:40 am »
What do you mean by "don't want them to see outside a specific boundary". Do you mean the camera should get clamped at that point or somehow the data gets clipped? What you'd do for both these options would be different. You can work out all the extreme values in script, but I need more info to suggest anything specific :)

Disastercake

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Disastercake
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #2 on: May 15, 2012, 12:07:08 am »
I just want the camera to get clamped at a certain point.

If you need anymore info let me know =)
Working on Soul Saga, an upcoming anime adventure RPG game by Disastercake.
www.disastercake.com

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #3 on: May 15, 2012, 09:41:33 am »
You'll need to know the extents of your camera.

If you're using a normal ortho camera, then you can work this out by
y = camera.size = top of camera
x = camera.size * camera.aspect = right extent of camera

So with this, you can work out your initial playing field, and then make sure than when you resize your camera, that you will keep within these bounds. You simply use the same math again with the resized camera to work out the extents of this secondary camera, so it fits the region specified by the first.

Hope that makes sense :)

Disastercake

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Disastercake
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #4 on: May 19, 2012, 03:52:34 am »
This was a very good break down.  I was able to get the system up and running once I scratched my head over it for a while.  Thanks, mate!
Working on Soul Saga, an upcoming anime adventure RPG game by Disastercake.
www.disastercake.com