Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by shreypareek1991 · Jun 12, 2016 at 07:02 AM · distancenormalspathfollowingtangent

Finding distance of player to a path in 3D

I have a path in 3D (sort of a travel in space situation - Go from planet A to planet B). At the beginning of the game I show the path to be followed by the player using a thin line. The player is supposed to stay as close to the line as possible. If they move more than a certain distance x from the path, the player should automatically start moving towards the center.

For example - if the path is circular, and the player moves away from the circle, I want to calculate how far the player is from the path, then find the normal direction of the player to the circle, and push the player along that vector.

How do I find the distance of the player from the path? Keep in mind I need the shortest distance. I can't really find the distance of the player to each point on the path and then use the shortest one; as it will be very expensive. Is there a way to do this? Also, how do I find the direction vector?

Thank you

Comment
Add comment · Show 4
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 Masterio · Jun 12, 2016 at 05:28 PM 0
Share

$$anonymous$$aybe try to iterete over the path points with some step offset like:

int offset = 10;

for( int i = 0; i < n; i+=offset ) {}

Then you will reduce the time if path have a lot of points.

avatar image shreypareek1991 Masterio · Jun 12, 2016 at 09:14 PM 0
Share

Yes, I think if I play with the offset I might be able to get a value that given the perfect balance between accuracy and computation. Thanks

avatar image TonicMind · Jun 12, 2016 at 06:43 PM 0
Share

TBH this is going to require some Calculus. I recall a similar problem from Differential Calculus, and I think the $$anonymous$$ch was just previewing Integral Calculus for us. But its definitely a calculus problem. The cool part about using Calculus for this (as opposed to some witchcraft) is you can get damn accurate values.

avatar image phxvyper · Jun 13, 2016 at 03:05 AM 0
Share

In Calculus, the concept youre talking about is called Optimization. When you optimize in Calculus, you're finding the closest point on a graph. This method is useful if you're using a curved path that uses a graph/function ins$$anonymous$$d of a set of points.

I suggest you check out some videos on this concept, here's a video: https://www.youtube.com/watch?v=QEF6RteZ4FU

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jun 12, 2016 at 10:08 PM

This problem is quite trivial for a single linear line path, but not that easy or even impossible to get an exact point for a curved path depending on where the player is in relation to the curve. A curved path can have certain points which have the same distance to a given point. For example a perfect circle. At the center all points on the path have the same distance.

The usual approach is to simply project your point onto the line / line-segment which gives you the closest point on that line segment. So you just iterate through all line segments and calculate the closest point oo that segment. In the end you just pick the closest point.

Projecting a point onto a vector does not restrict the point to be between the start and end point of that line segment. However by "normalizing" the result in a specific way you get back a float value that is between 0 and 1 when the point is between that line segment. If you clamp that value to the range between 0 and 1 you always get the closest point to that given line segment.

 //C#
 public static Vector3 FindClosestPointOnLineSegment(Vector3 lineStart, Vector3 lineEnd, Vector3 point)
 {
     Vector3 line = lineEnd - lineStart;
     Vector3 dir = point - lineStart;
     float d = Vector3.Dot(line, dir) / line.sqrMagnitude;
     d  = Mathf.Clamp01(d);
     return Vector3.Lerp( lineStart, lineEnd, d);
 }


Keep in mind when the player is on the inside of a curved path the closest point could be behind the player and even further away from the target / end of the path than his current position. You might want to additionally weight the different points you get based on how far they are from the end and pick one that would be a compromise between being close to the path and still heading towards the target. Creating a reliable system highly depends on the possible shapes of your path.

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

Procedural mesh generation - problem with normals. 2 Answers

I Don't Need Tangent Space Normals 0 Answers

World Space Normal -> Tangent Space Normal is surface shader 1 Answer

How to calculate the 3D vector perpendicular to a polygon surface in a specific direction 1 Answer

2D Custom Bouncing Diagonal 0 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