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
Help with Camera boundaries
- BruceTheGoose
- Citizen
- Posts: 76
- Joined: Sat Sep 20, 2014 2:54 pm
Help with Camera boundaries
- Attachments
-
- WarOfNations.zip
- (719.84 KiB) Downloaded 99 times
"I don't know."
- BruceTheGoose
- Citizen
- Posts: 76
- Joined: Sat Sep 20, 2014 2:54 pm
Re: Help with Camera boundaries
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.
Look at the main.lua I've uploaded here.
Still have issues, looking into it now.
- Attachments
-
- main.lua
- (1.26 KiB) Downloaded 86 times
Re: Help with Camera boundaries
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.
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.
Check out my blog on gamedev
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Help with Camera boundaries
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)
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)
When I write def I mean function.
Who is online
Users browsing this forum: Google [Bot] and 2 guests