How to clamp camera?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

How to clamp camera?

Post by Gunroar:Cannon() »

Just wondering what methods there are to make a camera that follows a player to stop following the player at a certain place (like next to a wall)? I use camerax but the methods should work for all if you have any clues...
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: How to clamp camera?

Post by GVovkiv »

use coordinates of player to check that?
like regular collisions checks, but for camera?
if player inside some zone, then camera should stop following, and when player leaves it, then continue?
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to clamp camera?

Post by darkfrei »

Gunroar:Cannon() wrote: Sun Oct 03, 2021 12:11 am Just wondering what methods there are to make a camera that follows a player to stop following the player at a certain place (like next to a wall)? I use camerax but the methods should work for all if you have any clues...

Code: Select all

if camera_following then
   -- do following
else
  -- do not following
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: How to clamp camera?

Post by Gunroar:Cannon() »

So like collision detection? Would I have to do anything with the scale value? Any resources for how to clamp are also welcome.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: How to clamp camera?

Post by GVovkiv »

Would I have to do anything with the scale value?
you need is X and Y of something that should be followed with camera (for example, player)
game window width and height (if your game window can be resized) or game's render width and height if, for example, your game uses option to change resolution of game which will be scaled to
and, maybe, scale value (if you have one)
with X and Y of player and with resolution\window size divined by in-game scale value (if have one) you can determine where bounds of camera, x, y, etc
Now with that you can create some "collisions" which you will use to determinate when you camera should stop and when continue to follow something
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: How to clamp camera?

Post by Gunroar:Cannon() »

It seems StalkerX has a setBounds(x,y,w,h) method, but it only works properly when the scale is 1? Problem?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
RNavega
Party member
Posts: 235
Joined: Sun Aug 16, 2020 1:28 pm

Re: How to clamp camera?

Post by RNavega »

Formally I think this is called a 'filter'.
You have an input 2D point, scale, rotation etc. that you want the camera to assume for the current frame, you can call this the "frame transform". Before applying this frame transform to the camera, you send it to that filter function and it will return a transform, which can be the same one (pass-through, no changes) or it can be a new transform with some components constrained according to some rules that you decided.

You get to decide what kind of constraints you want the camera transform to follow, and program the filter function to enforce them: boundary constraints (camera center is limited to the region of a rectangle), or collision, or a preset path for an "on-rails" camera, or the camera needs to always include 'these' objects on screen (so its scale and position are constrained to include those objects) etc.

Code: Select all

local frameCameraTransform = camera.createTransform(x, y, scale, rotation)

local constrainedCameraTransform = camera.constrain(frameCameraTransform, gameContext)

camera.setTransform(constrainedCameraTransform)
Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests