Page 1 of 1

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

Posted: Fri Jan 13, 2017 11:19 pm
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!

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

Posted: Sat Jan 14, 2017 1:06 am
by Xugro
Here is a quick demo on how to do this.

Image by Francisco Osorio under CC BY 2.0

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

Posted: Sat Jan 14, 2017 5:10 am
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!