Skip to main content
Tweeted twitter.com/#!/StackGameDev/status/145372864428769281
edited title
Link
user1430
user1430

Moving from Vector2 to Vector2, when "arriving" the Why does my sprite jitters aroundjitter when it arrives at its destination?

Source Link

Moving from Vector2 to Vector2, when "arriving" the sprite jitters around

I have just started looking into XNA for WP7 - and I am definitely the sort of person who likes to find answers to my questions without begging for help!

I searched here to find an answer but I found it hard to word my question, so apologies if it has been asked.

Basically I have a sprite, which I am moving to and from set locations on the screen. I have defined these locations and set up the methods to detect the input on the screen (I only want to be able to swipe left and right to select the location).

The sprite moves to the correct location when I swipe, but sometimes when it arrives at its location, it jitters very quickly backwards and forwards. My guess is that because of the maths used to calculate the vectors, it never actually hits the exact location, so keeps moving.

My theory was to set up some sort of boundary, where if the sprite was detected within x amount of pixels of the actual location, the sprite is snapped to the correct location. Would this be a good way of doing something? I imagine it isn't the most elegant solution as it will have that snapping motion towards the end, or am I just over-exaggerating what that will look like in my mind?

can anyone suggest a way of getting around this jumpy sprite? Code that i think is relevant below :)

Initialising:

player = new Player();
playerMoveSpeed = 12.0f;
player.Position = positionsSet.getLocation(1);

I defined my 4 locations like this:

Vector2 position1 = new Vector2(10.0f, 30.0f);
Vector2 position2 = new Vector2(210.0f, 30.0f);

My calculating the movement happens like this and is where I believe the stuttering to occur:

Vector2 direction = goTo - player.Position;
direction.Normalize();
player.Position = player.Position + direction * playerMoveSpeed;

Oh, and to get there I call this method (the gametime parameter is something I was trying, its not actually being used at the moment :) ) :

if(player.Position != goTo)
moveToLocation(goTo,gameTime);

Any advice would be massively appreciated guys and girls, I think I am getting there, but it is just annoying me now :)