Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrMelSE · Mar 23, 2019 at 10:11 PM · physicsdistancefast

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Mar 23, 2019 at 10:41 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by timoffex · Mar 24, 2019 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: alt text

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.


distance.jpg (29.4 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrMelSE · Mar 24, 2019 at 09:47 AM 0
Share

Thank you! Exactly what I was looking for.

avatar image
0

Answer by highpockets · Mar 23, 2019 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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Mar 23, 2019 at 11:47 PM 0
Share

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

avatar image highpockets xxmariofer · Mar 24, 2019 at 07:45 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

174 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a limit on the distance of raycast? 2 Answers

make projectiles always hit target 1 Answer

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

Keep a certain distance between two RigidBodies 2 Answers

Update speed and physics makes my rigidbody jiggle 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges