[SOLVED] How to get mouse position even if no input is detected?

Hi!

I’d like to make the “magic circle” follow the mouse position even when no input is detected.

The problem here is that the camera is auto-scrolling, so the physical cursor can “move” without triggering any input. In the meantime, the magic circle (supposed to follow the mouse position) stays immobile until I move or click to trigger the power (= input detected).
image

Is there a way to get the mouse position even if no input is detected? :thinking:

you need to add (or possibly subtract) the position of the camera until input is detected. I had a recent similar problem and that was the solution.

2 Likes

I came to the same conclusion, which leads me to my second problem :slight_smile: (I didn’t want to include it in my 1st message before knowing if there was a simple solution)

Since I need to know precisely when the input is detected (or not), I tried to use a boolean to do so.

Set to false by default in the update function:

function update(self, dt)
	gp.is_input = false
end

and set to true when input is detected:

function on_input(self, action_id, action)
	gp.is_input = true
end

But the parameter remains set to false (like if the update function has priority over the on_input function… or something. I don’t know)

So I’m doing something wrong.

Is there a simple way to know if no input is detected?

I used to know this, but I don’t remember now…

Yes, and you are on the right track. You need to check if the mouse’s position has changed since last frame.

I believe this can be done by checking that action.dx and action.dy are both exactly 0. But you can also try storeing the mouse’s position as a variable, and checking it each frame.

1 Like

That’s what I needed to know :slight_smile: Thanks for the answers!

Well done for getting it sorted! Luckily, I was facing this exact problem just last week.

1 Like

Now it works as intended and seems super obvious, but it required a few “die & retry” iterations to reach this “stupid & simple” solution :slight_smile: (took the wrong path 2-3 times)

In case someone encounters this little specific (but annoying) issue and finds this post:


In the camera script, you calculate the camera position delta (between current and old… old = previous frame):

function update(self, dt)
	gp.camPos_delta = gp.camPos - gp.camPos_old
	gp.camPos_old = gp.camPos
end

Then in the script of the game object you want to adjust the position, you add this delta:

function update(self, dt)
	self.pos.x, self.pos.y = self.pos.x + gp.camPos_delta.x, self.pos.y + gp.camPos_delta.y
	go.set_position(self.pos)
end

In the end, you don’t have to check if the mouse is moving or not (which, for some reason, was not 100% accurate and generated sub-issues) since the “camPos_delta” makes the adjustment automatically.

1 Like

For what it’s worth, the “how to get mouse position even if no input is detected” issue was not solved at all :grin: (I realized that recently: it works on mobile -which is pretty normal- but not on desktop)

The answer lies here: