Twitchy sprite movement

I'm having an issue with sprite movement, and I'm not sure how to deal with it.

When I set a destination for my player sprite (by clicking the screen), it sets the player moving in that direction. Unfortunately, the player sort of oscillates. He sort of shivers along the path.

I've set up a test project using just graphics primitives (graphics.fillRect(x,y,w,h)), and it's really easy to see. Here's an example:

My code looks like this

public void tick() {

if (InputHandler.leftClick.heldDown) {
dest.setLocation(InputHandler.leftClick.xPos, InputHandler.leftClick.yPos);
}

direction.setLocation(dest.getX() - position.x, dest.getY() - position.y);
direction.normalize();
}


This is a really really basic movement system because I'm having the same issues elsewhere and I wanted to work on them in a more controlled environment.

Can anyone help?

Well he appears to be "overshooting" the target. Basically the size of your discrete step (velocity) is probably too large.

Try reducing velocity by a lot (by the way, if velocity is a scalar quantity, it should actually be called speed). Try increasing it a bit until he is moving at the correct speed, but not overshooting the destination point.

When the distance to the destination is very close (ie smaller) than your velocity step size, you can either reduce velocity drastically, so he creeps into the final spot, or just set his final position at the destination. That'll prevent overshoot.

• Well, its 6.0 currently, and when I reduce it to 1.0 it still does it, just very slowly. If it were an overshoot, it wouldn't twitch on the path, would it? – BattleBarnes Sep 4 '13 at 1:34
• If you're moving 6 units a step, and the last step is say, 4 units away, then you will end up jumping 2 units over. On the next step, he will try to jump back, (-2 units back) but he will end up again +4 units over. Hence shivering. – bobobobo Sep 4 '13 at 1:35
• Are there integers involved somewhere? – bobobobo Sep 4 '13 at 1:37
• That makes sense, but I think there must be some additional problem - I just set the speed to 0.1 and it shivered the whole way and when it finally landed it shivered forever. I don't doubt you're assessment is right, but slowing it down alone isn't the solution, I think. – BattleBarnes Sep 4 '13 at 1:39
• The only integers are at the draw method, g.fillRect((int) position.x, (int) position.y, 64, 64); – BattleBarnes Sep 4 '13 at 1:40

I had a similar problem not too long ago. Here is the question (which also holds answer):

Jittery Movement, Uncontrollably Rotating + Front of Sprite

It's using the SFML 2.0 framework for C# but it should be translatable.