Help with Camera boundaries

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Help with Camera boundaries

Post 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
Attachments
WarOfNations.zip
(719.84 KiB) Downloaded 99 times
k.png
k.png (59.12 KiB) Viewed 2345 times
"I don't know."
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Re: Help with Camera boundaries

Post by BruceTheGoose »

Help anyone?
"I don't know."
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Help with Camera boundaries

Post 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.
Attachments
main.lua
(1.26 KiB) Downloaded 86 times
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Help with Camera boundaries

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Help with Camera boundaries

Post 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)
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests