I am trying to implement a system, where the cursor (in my case the player's avatar) is always about 1.5 seconds (a configurable interval) behind where the mouse actually is. If I change the cursor's location every time through, I get an emulated mouse cursor with no delay, I'm wondering if there are any pre-canned type of solutions I can throw at this before I "roll-my-own." Source code or even just basic steps you've used would be helpful.


2 Answers 2


I don't think there are any pre-canned solutions; at least I haven't seen any. But here is the logic to such a thing.

What you essentially want is a queue of mouse positions and timestamps. So define an object, let's call it MousePositionSnapshot, which has two members, time and position (or separate x,y if you want). And then define your queue of MousePositionSnapshots. Each update, make a new MousePositionSnapshot with the current timestamp and the current position of the mouse, and push it onto the head of the queue. Then peek at the tail of the queue to see if the timestamp is delayed enough for you, and if so, pop it off the end of the queue and move the emulated cursor to the given position.

Or, if you have a fixed framerate, you could skip the timestamp since you know how far apart each update is, and just keep your mouse lag in terms of number of updates; if the queue is smaller in length than that amount of updates then don't pop-and-move, otherwise just pop and move the emulated mouse each frame.

By the way, Queue may already be implemented in your language of choice; it is part of the C++ standard library and of course exists in java.util.Queue for Java.


Ricket's answer was my first idea too. If you draw all the mouse pointers in your queue you get a nice trail as well. Another option would be to linearly interpolate your detected mouse position and the actual mouseposition, that way it will gravitate towards the mouseposition with a delay, but it won't actually follow the exact path the mouse travelled. They both achieve the same (configurable delay for the mouse to catch up) but in a decidedly different visual fashion.

  • \$\begingroup\$ I think that the interpolation option might produce a rather strange effect because the "velocity" of the delayed cursor wouldn't necessarily map to the original velocity. That could make it significantly more difficult for the user (player) to predict the future position of the cursor. \$\endgroup\$ Commented Aug 18, 2010 at 23:57
  • \$\begingroup\$ Could you expand on This linear interpolation? That sounds like it might be more like what I'm after. \$\endgroup\$
    – Nate
    Commented Aug 19, 2010 at 0:20
  • \$\begingroup\$ 'Interpolation' just means "computing intermediate values." So you store points at some interval, and when you want to compute a new location you find the point "sooner" than your current point and the point "later", and do a weighted blend between the two points' positions based on the time differential. \$\endgroup\$ Commented Aug 19, 2010 at 0:38
  • \$\begingroup\$ @Mike - the future position would always be closer to the mouse than the current position as it moves from current to mouse position. It's like a springy connection without overshoot. Like I said, it depends on what OP is after, but it should work well enough. \$\endgroup\$
    – Kaj
    Commented Aug 19, 2010 at 1:20
  • \$\begingroup\$ @Nate - for example visibleMousePos = prevMousepos * someFloatFactor + (1-someFloatFactor) * actualMousePos); prevMousePos = actualMousePos (or visibleMousePos, both can work) - with someFloatFactor being between 0 and 1. \$\endgroup\$
    – Kaj
    Commented Aug 19, 2010 at 1:23

You must log in to answer this question.

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