Page 1 of 1

Help developing a camera that moves with mouse

Posted: Fri Feb 17, 2017 10:06 pm
by cemcmd
Hello, I am trying to make a scrolling like system. A bit like moving a window around on Windows. I tried to make one, but I always got results I didn't like. Can I see some code samples for something that does this? It would be very usefull.

Re: Help developing a scrolling like system

Posted: Fri Feb 17, 2017 11:27 pm
by zorg
Moving windows around on the desktop is not scrolling; scrolling is when content is bigger than a window, for example, and you have to move the "camera" to see everything (whether by mouse or by scroll-bars or key input or whatever, doesn't matter).

So yeah, a bit more information would be nice, before we could help you. :3

Re: Help developing a scrolling like system

Posted: Fri Feb 17, 2017 11:38 pm
by cemcmd
Well I want to have the ability to move a camera around using the mouse.

Re: Help developing a scrolling like system

Posted: Sat Feb 18, 2017 12:39 am
by MrFariator
Please specify further; how do you want to move the camera around with mouse inputs?

Move it when cursor touches the edges of the screen, move it when holding down a button and moving the cursor about ("panning" as it is called in plenty of programs), move the camera based on mouse strokes (like how a FPS game might do it), or something else?

Whatever the case may be, I recommend taking a look at love.graphics.translate for one way to do it. Basically just store the camera coordinates somehow, change them based on the desired inputs, and then inside love.draw:

Code: Select all

love.graphics.push()
love.graphics.translate ( cameraX, cameraY )
-- ...Draw stuff
love.graphics.pop()
Of course, you can always look into some libraries that provide camera functionality.

Re: Help developing a camera that moves with mouse

Posted: Sat Feb 18, 2017 12:43 am
by cemcmd
Well I wanted the camera to move around when mouse 1 is down. A bit like dragging a window around. Where you're mouse doesn't represent the direction of where the camera will go, but instead where it is dragged to.

Re: Help developing a camera that moves with mouse

Posted: Sat Feb 18, 2017 1:48 am
by Positive07

Code: Select all

local w, h
local tx, ty = 0, 0
local circles = {}

love.load = function ()
   w, h = love.graphics.getDimensions()
   tx, ty = -w/2, -h/2

   for i=1, 100 do
      table.insert(circles, {
         x = love.math.random(0, w * 2),
         y = love.math.random(0, h * 2),
         r = love.math.random(0, 100)
      })
   end
end

love.mousemoved = function (x, y, dx, dy)
   if love.mouse.isDown(1) then
      tx = math.min(0, math.max(tx + dx, -w))
      ty = math.min(0, math.max(ty + dy, -h))
   end
end

love.draw = function ()
   love.graphics.translate(tx, ty)
   
   love.graphics.setColor(0, 255, 0, 100)
   for _, circle in ipairs(circles) do
      love.graphics.circle("fill", circle.x, circle.y, circle.r)
      love.graphics.circle("line", circle.x, circle.y, circle.r)
   end
end

Re: Help developing a camera that moves with mouse

Posted: Sat Feb 18, 2017 3:13 am
by cemcmd
All I see is a blank screen

Re: Help developing a camera that moves with mouse

Posted: Sat Feb 18, 2017 4:48 am
by Positive07
Then you are doing something wrong

Re: Help developing a camera that moves with mouse

Posted: Sat Feb 18, 2017 6:02 pm
by cemcmd
I got it now, I wasn't running the code independently from my other.

Or, as its own program.