Help developing a camera that moves with mouse

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
cemcmd
Prole
Posts: 6
Joined: Sat Dec 03, 2016 12:07 am

Help developing a camera that moves with mouse

Post 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.
Last edited by cemcmd on Sat Feb 18, 2017 12:12 am, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Help developing a scrolling like system

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
cemcmd
Prole
Posts: 6
Joined: Sat Dec 03, 2016 12:07 am

Re: Help developing a scrolling like system

Post by cemcmd »

Well I want to have the ability to move a camera around using the mouse.
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: Help developing a scrolling like system

Post 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.
cemcmd
Prole
Posts: 6
Joined: Sat Dec 03, 2016 12:07 am

Re: Help developing a camera that moves with mouse

Post 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.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Help developing a camera that moves with mouse

Post 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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
cemcmd
Prole
Posts: 6
Joined: Sat Dec 03, 2016 12:07 am

Re: Help developing a camera that moves with mouse

Post by cemcmd »

All I see is a blank screen
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Help developing a camera that moves with mouse

Post by Positive07 »

Then you are doing something wrong
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
cemcmd
Prole
Posts: 6
Joined: Sat Dec 03, 2016 12:07 am

Re: Help developing a camera that moves with mouse

Post by cemcmd »

I got it now, I wasn't running the code independently from my other.

Or, as its own program.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest