Adding Sensivity to a 2D Game?

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
User avatar
pyxledev
Prole
Posts: 27
Joined: Tue Jul 26, 2022 2:19 pm
Contact:

Adding Sensivity to a 2D Game?

Post by pyxledev »

Basically, I want to add a sensivity option that would allow the mouse cursor to move in higher speeds.

It might sound weird for a 2D project, but my game is a top-down shooter and you may sometimes need to move your cursor faster. It might be an enemy coming up from the top left, or just the fact that you have a big monitor.

Here is the current code I have for it. Very laggy, and doesn't even work when VSync is off.

Code: Select all

--(Settings.sensivity varies between 1-x)
local mx, my = love.mouse.getPosition()
mx = mx + (mx-self.oldX)*Settings.sensivity
my = my + (my-self.oldY)*Settings.sensivity
love.mouse.setPosition(mx, my)
self.oldX, self.oldY = love.mouse.getPosition()
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: Adding Sensivity to a 2D Game?

Post by MrFariator »

I think love.mousemoved callback might work better for this purpose, since you're interested in receiving updates on how much the mouse moves on x and y axes whenever inputs are polled (see love.run for example when event polling happens), and applying a multiplier on it. This way, you could just keep track of in-game mouse position in some variables, without resorting to using love.mouse.get/setPosition every frame.

Essentially the issue with your current code, as I see it, is that since you're dealing with (possibly non-integer) multipliers, the deltas from frame-to-frame might be too small. The wiki page for love.mouse.getPosition warns that any coordinates set are floored, so when passing numbers with small enough differences the resulting positions might not end up changing at all. For this reason, using your own coordinate representation, with room for decimal values might work out better. You might just have to round these coordinates, when rendering any targeting visuals, or spawning player bullets.

Could also consider setting relative mode, if need be.
User avatar
pyxledev
Prole
Posts: 27
Joined: Tue Jul 26, 2022 2:19 pm
Contact:

Re: Adding Sensivity to a 2D Game?

Post by pyxledev »

MrFariator wrote: Mon Jun 17, 2024 9:13 pm I think love.mousemoved callback might work better for this purpose, since you're interested in receiving updates on how much the mouse moves on x and y axes whenever inputs are polled (see love.run for example when event polling happens), and applying a multiplier on it. This way, you could just keep track of in-game mouse position in some variables, without resorting to using love.mouse.get/setPosition every frame.

Essentially the issue with your current code, as I see it, is that since you're dealing with (possibly non-integer) multipliers, the deltas from frame-to-frame might be too small. The wiki page for love.mouse.getPosition warns that any coordinates set are floored, so when passing numbers with small enough differences the resulting positions might not end up changing at all. For this reason, using your own coordinate representation, with room for decimal values might work out better. You might just have to round these coordinates, when rendering any targeting visuals, or spawning player bullets.

Could also consider setting relative mode, if need be.
Got it working. Thanks :)
imhabbit
Prole
Posts: 1
Joined: Fri Jun 21, 2024 5:04 pm

Re: Adding Sensivity to a 2D Game?

Post by imhabbit »

Oh thanks as well. I struggled with sensitivity with my 2d project.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests