Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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. –  Mike Strobel Aug 18 '10 at 23:57
    
Could you expand on This linear interpolation? That sounds like it might be more like what I'm after. –  Nate Aug 19 '10 at 0:20
    
'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. –  dash-tom-bang Aug 19 '10 at 0:38
    
@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. –  Kaj Aug 19 '10 at 1:20
    
@Nate - for example visibleMousePos = prevMousepos * someFloatFactor + (1-someFloatFactor) * actualMousePos); prevMousePos = actualMousePos (or visibleMousePos, both can work) - with someFloatFactor being between 0 and 1. –  Kaj Aug 19 '10 at 1:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.