0
\$\begingroup\$

I'm using pointer locking for a 2D game (where you perform calculations based on the delta in mouse positions from frame to frame). I'm having some basic math/logic issues with accomplishing what I want.

I'd like the player to be able to send the character in the direction they last moved their mouse (so if they stop moving the mouse, the character continues to travel in the direction they last moved in).

The issue I'm experiencing is the movementx and movementy values are integers. If the player moves the mouse quickly so that from one mousemove event to the next many pixels are traversed, the direction to move the character is reasonably precise. Every other scenario results in the last movement having movementx or movementy only being -1/0/+1. If the character only moves based on the last movement of the mouse, it very often only moves in one of 8 directions.

How can I be more precise than 8 directions even on slow/short mouse movements while ensuring the last bit of movement of the mouse is always what's being used?

My first thought was to average out the last n movement values, but that can easily result in the character not always moving in the direction the mouse moved last. I've not really had too many other thoughts though, so I came here for assistance.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

The whole setup seems a bit strange.

Is the speed always a fixed number, and what you're dealing with now is how to choose direction? Or is the speed dependent on the movement as well?

If the latter case (speed dependent on mouse movement): You should either make this "physical", i.e let the delta mouse movement result in acceleration, or link delta position to delta mouse, and just stop it when mouse stopped moving (i e skip your idea).

If the first case (fixed speed): As I said, I find this user interface really strange, and I think your problem is manifestation of that.

Either way - you've encountered a common problem where you estimate angle from a short distance (it's even more evident that there is a problem if you consider the "singularity" when delta = 0).

Perhaps an easy way out would be to choose the last value where the magnitude is larger than some fixed value?

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .