Page 1 of 1
Help with Camera boundaries
Posted: Fri Mar 27, 2015 2:02 am
by BruceTheGoose
I need help setting boundaries for the camera. The problem is that you can zoom in and out which breaks the boundaries. I don't want the camera to go over the width of the map where the background is exposed. I can't think of the math for this at the moment, so if someone could help me out I would really appreciate it.
Controls:
Mouse Wheel Up/Down - Zoom In/Out
WASD - Move around the map
Re: Help with Camera boundaries
Posted: Thu Apr 02, 2015 1:24 am
by BruceTheGoose
Help anyone?
Re: Help with Camera boundaries
Posted: Thu Apr 02, 2015 2:54 am
by arampl
Oh, sorry. I've tried it without luck, then forgot about it.
Look at the main.lua I've uploaded here.
Still have issues, looking into it now.
Re: Help with Camera boundaries
Posted: Thu Apr 02, 2015 7:17 am
by micha
I cannot provide the fixed code for you, but here is what you can do to fix the issue:
First, get used to the fact that you have two types of coordinates: world-coordinates and screen-coordinates. These can even have different units (for example meters or tiles vs. pixels). For camera implementation and for implementing a mouse-interface, it is very useful to implement functions that convert between these two coordinates: worldToScreen gives you the position (in pixels) of a specific point in the game-world. screenToWorld gives you the position (in world coordinates) of a specific pixel on screen. If you have translation and scaling only (no rotation), then the conversion between the two is simply multiplying by the scale and adding the translation (in the correct order).
Now for the camera constraints, you put the four screen corners into screenToWorld and check if these coordinates are inside the game world. If they are not, then move the camera. The exact amount you need to move the camera can be calculated from the relation world-screen again.
I know, this explanation might be vague and unspecific, so please go ahead and ask, if you are stuck.
Re: Help with Camera boundaries
Posted: Thu Apr 02, 2015 8:05 am
by kikito
Hi there,
My camera library, [wiki]gamera[/wiki], does this out of the box (it also does it when there's scale and rotation).
If you don't want to use my lib directly, you can read the code to see how it does it (the relevant functions are
adjustPosition and adjustScale)