1
\$\begingroup\$

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
if(!reachedDestination)
{    
    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?

\$\endgroup\$
5
  • \$\begingroup\$ 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) \$\endgroup\$
    – jhocking
    Mar 13, 2014 at 16:52
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 13, 2014 at 16:52
  • \$\begingroup\$ @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. \$\endgroup\$ Mar 13, 2014 at 16:55
  • \$\begingroup\$ movementSpeed is never changed. direction before Normalization is Vector2(330:133) after normalization it is Vector2(0,9275043:0,3738123). \$\endgroup\$ Mar 13, 2014 at 17:06
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – jhocking
    Mar 13, 2014 at 17:16

1 Answer 1

5
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ aha I missed that bit, it's not actually moving toward the destination point. which would also create problems with the destination reached code \$\endgroup\$
    – jhocking
    Mar 13, 2014 at 17:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .