- Home /

# Closest point that has been between two objects

I've created a live size shooting simulator (using projectors and cameras), all working well, but in which looks like a simple function, giving me a really hard time..

an object is moving at a speed of 26 units/second (m/s). I'm shooting a sphere at it, moving at 400 units/second(m/s).

I need to account for lead etc, which is the way I have to do if I shoot at the range. I got all that working, it's as in real life.

Now I want to check, if I miss, how far off was I?

The problem is that the framerate even using FixedUpdate for such fast encounter is not enough. At an fixedupdate of 1ms that still result in 400mm of movement of the shot and a 26mm factor on the target.

So, what other options do I have for this task?

400 m/s is not fast, is really really fast, well increasing the FixedTimeStep so mucho should not be an option, since is not only increasing the amount of times fixedupdate is being called, but also increasing all the physics update, like checking for collisions 10 times more often for example, (it will not run in all pcs because it will be performance intensive) you should calculate the closest point between 2 objects yourself at the start / end of the movement(not checking distance each frame) there is nothing faster than a fixedupdate with such a low timestep

**Answer** by timoffex
·
Mar 24 at 01:57 AM

When two point-like objects are moving at constant speed, computing their closest distance is equivalent to finding the minimum value of a quadratic polynomial. You can compute the closest distance with the following formula (written in the form of code for clarity)

```
Vector3 posDiff = pos1 - pos2; // difference in initial position
Vector3 velDiff = vel1 - vel2; // difference in initial velocity
float dot = Vector3.Dot(posDiff, velDiff); // helper variable
float minT = -dot / velDiff.sqrMagnitude;
float minDist = Mathf.Sqrt(posDiff.sqrMagnitude + 2 * dot * minT + velDiff.sqrMagnitude * minT * minT);
```

The difference in position between the two objects at any time t is given by posDiff + t * velDiff

You want to find the smallest magnitude of the above expression over all values of t. For this, you just need a little bit of calculus. Recall that the square magnitude of a vector is the same as the dot product of the vector with itself. Let ∆ denote posDiff and ∆ with a dot over it denote velDiff. Now you can compute:

This means that the square distance is a quadratic function of time. In other words, it's a parabola. Then you can simply use the quadratic formula to find the t for which it is minimized (ax^2 + bx + c is minimized by x = -b / 2a for positive a) and plug that in.

This is only the closest distance between two points moving at constant velocity. It gets complicated for other shapes that aren't simply spheres. For spheres, note that the distance between two spheres is just the distance between their centers minus the sum of their radii.

**Answer** by highpockets
·
Mar 23 at 11:09 PM

You can use Vector3.Project(). The normal direction will be the direction the bullet is travelling and the other vector will be position of the target. The returned vector is the position at which the bullet passed the target. So Vector3.Distance() will give you the distance between the 2 points as a float.

Hope that’s helpful.

Cheers

this can not be dont like this since both objects are moving, the nearest position between 2 objects will totally depend on the velocity of the objects, and almost never will be the "closest trayectory" point.i am not that good at mathematics but knowing closest point between 2 moving objects looks complex, maybe generating some sort of algorithm that trys to get closer and closer to that solution would be my approach

I wasn’t suggesting to just take the target position when the shot was fired. If the target is moving at 26 units a second and the bullet, at 400 units per second. And you know the distance from the gun to the target when the shot is fired, say 100 units for sake of argument. We know that the bullet will reach the target position (when the shot was fired) if it is on target in approximately 0.25 seconds, at this time the target will have moved an additional 26 units/ 4 = 6.5 units. It won’t be exact, but if we get the target position and do the calculation at this point in time, it will be a super close approximation. With a bit more math you can likely find a more precise time to do the calculation, but the difference I would think would be negligible.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

How to get an accurate collision distance (get closest points on one axis)? 0 Answers

Is Raycast relatived to screen resolution? 1 Answer

How do i get the Y Velocity of my character? 1 Answer

Get distance from all objects in an Collider[] and compare 1 Answer

Problems with fast moving colliders 2 Answers