I am trying to find out a formula that calculates the actual position of a player using his speed and a destination position.

These are the informations I have:

  • Vector3 (X,Y,Z) : Current position
  • Vector3 (X,Y,Z) : Destination position
  • float : Speed

I can manage to calculate the angle between these two vectors, but I can't find an efficient formula to calculate the exact position using the speed at each loop. Do you have an idea?


I finally found a formula, but it's not really working as I want...

private void WalkNew()
    float speed = 0.1f;
    float distX = this.DestinationPosition.X - this.Position.X;
    float distZ = this.DestinationPosition.Z - this.Position.Z;
    float distance = (float)Math.Sqrt((distX * distX) + (distZ * distZ)); // distance between A and B positions

    float time = distance / speed;
    float currentTime = Time.GetTickFrom(this.lastMoveTime);
    this.lastMoveTime = Time.GetTick();
    float percentage = currentTime / time;

    if (this.Position.IsInCircle(this.DestinationPosition, 0.1f))
        // We arrived
        this.Position.X += distX / percentage;
        this.Position.Z += distZ / percentage;

Any thoughts about this formula ?


  • \$\begingroup\$ Dividing by a percentage is not appropriate here. Why not use the formula given by Bálint in the answer below? It will work correctly, even with fractional speeds. \$\endgroup\$ – DMGregory Feb 23 '17 at 17:28

You subtract the current position from the destination position, this way you get a vector pointing to the destination.

You need to normalize this vector (divide it with it's length) and multiple with the speed. You get a velocity vector this way.

Finally you add this to the current position to get the new position.

In pseudo code:

currPos += normalize(dest - currPos) * speed

This way you don't need to uses that many costy function, such as atan2, cos or sin, only a square root.

  • \$\begingroup\$ Thanks for your answer, that's much easier than my old formula using cos and sin. Thank you, I still have an other question about the speed. My speed is between 0 and 1 (current speed is 0.1) so I shouldn't multiply but divide the normalized vector right? \$\endgroup\$ – Eastrall Feb 20 '17 at 19:40
  • \$\begingroup\$ @Eastrall Why is your speed between 0 and 1? You generally want to avoid division, because if by any chance speed becomes 0, then it'll probably throw an error \$\endgroup\$ – Bálint Feb 21 '17 at 7:12
  • \$\begingroup\$ I choose 0 and 1 because I thought it would be simple to implement this kind of formula. Maybe it is better to multiply it by 100 to obtain a % ? like: 0.1 would be 10%; 0 => 0% ; 1 => 100% ; etc... \$\endgroup\$ – Eastrall Feb 21 '17 at 8:23
  • \$\begingroup\$ I've edit my main post with the formula \$\endgroup\$ – Eastrall Feb 23 '17 at 16:43

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.