Creating a menu background which "moves" based on the position of the mouse taking into consideration the screen edges

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
Chantz
Prole
Posts: 2
Joined: Fri Jan 13, 2017 11:03 pm

Creating a menu background which "moves" based on the position of the mouse taking into consideration the screen edges

Post by Chantz »

hi all, I'm creating a game and I'm stuck with an issue with the main menu.

I'm using Gamera to create a world space 2048x1536 in size ((1024x762)*2), and have a viewport/screen of the aforementioned 1024 resolution. I'm having a problem with the screen hitting the sides of the world before the mouse reaches the end of the world, which I know is in some way because there's 512 pixels on either side of the screen that aren't being taken into account.

I'm getting a percentage for my x and y by taking the mouse's position on the screen and dividing it by the width of the screen, which in turn gives me a decimal between 0 and 1 for it's location within the screen. I then take this percentage and multiply it by the size of the world, which returns the x position in the world space in relation to the mouse's position on the screen space. I then cam:setPosition(foundX, foundY), and it moves my camera around the world space based on the mouse's position.

however, as mentioned before, :setPosition is in relation to the center of the screen. in this specific case, I have 512 pixels to either side that will hit first, causing an unappealing stop when I get near an edge. my goal was to have it slowly head towards the edge, while just barely not usually reaching it (as in you're at 1% or 99% of the screen). the code I'm using here is below:

Code: Select all

  menuWidth, menuHeight = love.graphics.getWidth(), love.graphics.getHeight()
  mouseW, mouseH = love.mouse.getPosition()
  menuLeft, menuTop, menuCamWidth, menuCamHeight = menuCam:getWorld()
  screenLeft, screenTop, screenCamWidth, screenCamHeight = menuCam:getVisible()
  screenRight, screenBottom = screenLeft + 1024, screenTop + 768
  camXPercent, camYPercent = (mouseW/menuWidth), (mouseH/menuHeight)
  modCamX, modCamY = (menuCamWidth * camXPercent), (menuCamHeight * camYPercent)
  menuCam:setPosition(modCamX, modCamY)
I've tried many things to get this to work properly, including finding the x/y position of the camera, then dividing that by the center of the world (on the x/y axis respectively), then taking .75/that value I just found, then multiplying it by the original final product to get a dynamic modifier, but that only worked for the right/bottom sides of the screen. that's only one of the many, many things I've tried.

if anybody has any ideas on what to try, I will gladly test them!
Xugro
Party member
Posts: 114
Joined: Wed Sep 29, 2010 8:14 pm

Re: Creating a menu background which "moves" based on the position of the mouse taking into consideration the screen edg

Post by Xugro »

Here is a quick demo on how to do this.

Image by Francisco Osorio under CC BY 2.0
Attachments
scrolling_background.love
(1.72 MiB) Downloaded 148 times
Chantz
Prole
Posts: 2
Joined: Fri Jan 13, 2017 11:03 pm

Re: Creating a menu background which "moves" based on the position of the mouse taking into consideration the screen edg

Post by Chantz »

wow, I wasn't aware of just how simple of a solution was out there. it's now working perfectly, and I can now remove Gamera from the menu and gain a bit of performance. thank you very much!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 5 guests