1
\$\begingroup\$

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:

enter image description here

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();
    position.add(direction.multiply(velocity));
}

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.

The weirdest part about this is that when the player arrives, he still just sits on one point, shivering madly.

Can anyone help?

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – BattleBarnes Sep 4 '13 at 1:34
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – bobobobo Sep 4 '13 at 1:35
  • 3
    \$\begingroup\$ Are there integers involved somewhere? \$\endgroup\$ – bobobobo Sep 4 '13 at 1:37
  • \$\begingroup\$ 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. \$\endgroup\$ – BattleBarnes Sep 4 '13 at 1:39
  • \$\begingroup\$ The only integers are at the draw method, g.fillRect((int) position.x, (int) position.y, 64, 64); \$\endgroup\$ – BattleBarnes Sep 4 '13 at 1:40
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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