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 have a texture drawn on screen. Whenever I click on the viewport it remembers its destination and starts moving towards it, until it is there. This is the code I am using therefore:

//Check if destination was reached
if ((currentDestination - position).Length() <= movementSpeed)
     // Stop moving
     position = currentDestination;                        
     playerState = PlayerState.Idle;
     reachedDestination = true;

// If destination is not reached
    Vector2 direction = (currentDestination - position); // gives the direction needed to travel from position to target
    angle = (float)(Math.Atan2(direction.Y, direction.X) + MathHelper.PiOver2);

    direction.Normalize(); // we need it as a unit vector

    position += direction * movementSpeed;
    // The Y coordinate stays the same in this case
    position.Y = 455;

It does work, but the closer the texture is to the destination, the slower it gets. Why is that?

share|improve this question
Are you sure the normalize step is working right? Because he symptoms you describe sure sound like the vector isn't normalized (ie. the magnitude is greater when the destination is further away) –  jhocking Mar 13 at 16:52
Can we assume that movementSpeed isn't changing? If it's being changed elsewhere, becoming smaller, that would cause the speed of the texture to be affected. –  Neeko Mar 13 at 16:52
@jhocking Your hunch makes sense, but I'd be very surprised if Vector2.Normalize wasn't working (that's an XNA provided method, not user). Regardless, definitely worth debugging and making sure a unit vector is being set. –  Neeko Mar 13 at 16:55
movementSpeed is never changed. direction before Normalization is Vector2(330:133) after normalization it is Vector2(0,9275043:0,3738123). –  bodycountPP Mar 13 at 17:06
I missed the bit mentioned in awsump's answer, that it's not actually moving toward the destination point. The question is misleading, since you say it's moving toward the destination but, well, no it's not; you should probably change that. –  jhocking Mar 13 at 17:16

1 Answer 1

up vote 5 down vote accepted

I believe this is the line giving you trouble.

position.Y = 455;

The way you're doing this, the X component of the velocity is getting smaller. direction is always changing. Even though the distance is closing on the X axis, it stays the same on the Y-axis. So, when it's normalized, the Y distance is relatively greater, so X will get smaller. Something along the lines of this would work.

Vector2 alignedDestination = new Vector2(currentDestination.X, 455); //Added line
Vector2 direction = (alignedDestination - position); // gives the direction needed to travel from position to target
share|improve this answer
aha I missed that bit, it's not actually moving toward the destination point. which would also create problems with the destination reached code –  jhocking Mar 13 at 17:14

Your Answer


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.