Smooth mouse motion

Started by
3 comments, last by walle 19 years, 4 months ago
Hello. I'm trying to get a smooth mouse motion in SDL for moving a paddle. Perhaps even timebased movment. Ideas anyone? //walle
Advertisement
What are you using right now?
Rob Loach [Website] [Projects] [Contact]
At first I put the paddle exactly where the mouse was, but it was to fast. Then I thought to move the paddle in the same speed towards were the mouse was, I checked if the paddle were to the right or the left to the paddle and moved left or right acordingly the mouse were over the paddle I didn't move. But it doesn't feel good. I've seen people use a mouse sensitivity variable, but I havn't got it to work.

Hope it makes any sense at all, just out of bed =)

///walle
You could use the frame time and multiply your mouse delta values by frame time and pixels per second, e.g.
static const float pixelsPerSecond = WindowHeight / 4.0;int newY = mouseDeltaY * frameTimeInSeconds * pixelsPerSecond;playerPaddle.SetNewY(newY);

Something along that line should be good, I guess. You don't even need to check every frame - sum up delta values until frameTime > threshold, with threshold being at least 0.02s (50 FPS) because sometimes movement will be jerky (e.g. abrupt position changes if frameTime varies too much).
To get it really smooth, you can sample the last 4 - 8 frames and interpolate frame times.

Good luck,
Pat.
I can't really get it to work...it is for a breakout style of game, so I changed the window hight to window width and such...but the paddle just sitts at one corner all the time.
What did you mean with "frameTimeInSeconds"? How do you get it?

//walle

This topic is closed to new replies.

Advertisement