3
\$\begingroup\$

I have two points, one is moving and one is stationary. I would like to know if the moving point has reached that point or not. I tried to calculate the distance between two vectors and the distance never reaches 0.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ It will likely never be exactly equal to 0, especially if you're using floats. Pick a minimum distance and check to see if the distance in less than that minimum. \$\endgroup\$
    – House
    Oct 14, 2012 at 20:33

3 Answers 3

4
\$\begingroup\$

You have one of these problems:

  • a point doesn't even try to reach another point, and in 3D world it's very unlikely that two points hit each other (just like it's unlikely that one of hundreds of live threating comets will actually hit Earth)
  • the point has velocities set to reach another point, but another point also has some velocity, and therefore will always run away (this reminds me of Achilles and turtle paradox)
  • the point has velocities set in another point's direction, but it goes through it between checks, and if velocities are updated then, it just starts to oscillate the target point, never reaching it
  • float rounding makes point A be 0.00000000001 pixels away from point B, but never exactly 0.

Solution is not to treat points as points, but as physical objects, giving them some size, e.g. spheres. You could assume a point is a sphere of 1 pixel diameter (0.5 px radius), it would mean you need to check if distance is < .5+.5 that is if (distance < 1) not if (distance == 0).

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

Check if the two points are close enough.

Lets say the speed of point A (moving point) is speedA and that the last frame was delta milliseconds long.

If A is moving towards B (stationary point) and is currently in a distance smaller than speedA * delta / 1000.0 it will reach the stationary point in the next frame.

You need to check if the distance between the moving point and the stationary point is less than the size of one step for the moving point.

distance(A, B) <= speedA * delta / 1000.0

If that happens the moving point will reach the stationary point in the next frame.

If this never happens, it means that point A is not moving towards point B or that it is moving in a speed so slow that it will not allow it to reach point B in a finite time such (C/n^2) where n is the current frame index and C is a constant.

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

The reason why this is not working is because of your understanding of the problem. You are considering the two objects to be two points. In games the position of an object in world space is most commonly a vector quantity.

Instead you should consider these two "points" as vectors. Next you can follow a few simple steps to arrive at a solution.

  1. You take either vector and subtract the other from it. This creates a vector based at the origin.
  2. The magnitude of this vector will then allow you to determine whether the two obejcts are occupying the same space.
  3. In order to do this you compare the magnitude to the a value commonly called r. r represents the lee-way that must be considered when performing any calculation on floats. Because we do not have infinite space to hold a float value there will always be rounding errors in calculations with floats.

The pseudocode for this would be as such:

float r= 0.0000000001f;   // the lee-way for our program
Vector3 a,                // your first vector
        b,                // your second vector
        c;                // a temporary container for the vector originating
                          //   at the origin
c= a-b;
if (c.Magnitude < r)
{
    // they are occupying the same space
}

I hope this helps.

\$\endgroup\$

You must log in to answer this question.

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