Mouse callbacks and functions

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
MordaBest
Prole
Posts: 2
Joined: Sun Feb 03, 2019 3:54 pm

Mouse callbacks and functions

Post by MordaBest » Sun Feb 03, 2019 6:03 pm

Hello fellow developers,
I recently hit a strange problem with the mouse functions setPosition and setRelativeMode. I am trying to make a decent camera/cursor transitions between first person and third person view in my game. This is the way it's supposed to work:

1. By default, you have 3rd person/top view. The camera follows you and tracks the camera using the simple formula:
camera.x, camera.y = Hero.x - love.graphics.getWidth()*2/3 + love.mouse.getX()/3, ...

2. When you press MB2, you get first person view (doom-like pseudo 3d). Mouse enters relative mode, camera position becomes
camera.x, camera.y = x - love.graphics.getWidth()/2, ...

3. While holding MB2 and moving mouse left or right, camera view effectively circles around the player using several trigonometry functions. dy parameter in Camera coordinates change so they stay on a circle.

4. When releasing MB2, view changes to 3rd person, camera is placed using the first formula while cursor is placed on camera coordinates. This is done so when you spin your mouse in 1st person, the angle of your turn persists, as in you face north, enter 1st person, spin 180, enter 3rd person - and your character now faces south.

There is a small but annoying issue though. On step 4, camera flinches for just one frame, as if the mouse retained the coordinates it had in relative mode, then on next frame it moves using the first formula as it should. I tried to insert the formula in different places in mouse callback functions, update loop, even tried to put it in love.run. Is there a way to smooth out this transition or simply fix it somehow? The mousereleased callback in its latest form:

Code: Select all

function love.mousereleased(x, y, button)
  if button == 2 then 
    love.mouse.setRelativeMode(false)
    love.mouse.setPosition(camera.x + width/2, camera.y + height/2)
    camera.x = Hero.x - width*2/3 + love.mouse.getX()/3
    camera.y = Hero.y - height*2/3 + love.mouse.getY()/3
    camera.state = "top"
  end
end

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests