Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Fusyion · Jun 17, 2014 at 11:01 AM · colliderpositionmathlinestrigonometry

How do I calculate how far along a point is in relation to a line, when the point is not on the line?

I have a 2D plane with a texture on it. By clicking on the texture I can select a colour by making use of Raycasting and RaycastHit.textureCoord.

As part of a virtual reality pointer (in combination with the Razer Hydra) I want to use a collider instead. It's a small sphere that intersects with surfaces, and by clicking I can select objects. Because I can't Raycast in this case, how would I go about getting an x and y coordinate, where the sphere intersects the plane? The goal is to get values between 0 and 1: bottom left is 0,0, and top right is 1,1. This also has to work when the plane is scaled, rotated, and moved.

One way I tried to approach it is this:

In this example image the red rectangle is the selection plane, the yellow sphere is the selection collider, and the four purple spheres are the Transform handles as children of the rectangle game object. (Ignore the two-tone blue background.)

alt text

  1. Add four Transforms as children of the plane, placing them on the top, right, bottom, and left edges. Shown in the image as the purple spheres.

  2. To calculate the x coordinate I took the distance from the left sphere to the collider (left-collider) and added the distance from the rightsphere to the collider (right-collider). Then I took the distance left-collider and divided it by the sum of the two distances.

  3. I repeated this for the y coordinate by taking the top and bottom colliders.

In the following example point p is 1.5 units away from point l, and 4.5 units away from point r. Adding these distances we get 6: 1.5 + 4.5. Dividing 1.5 by 6 gives us 0.25, which is correct.

alt text

The problem is that this works fine when point p is relatively in line with the line left-right, but when point p is higher or lower, and thus farther away from both left and right, the proportion is no longer correct.

My question is as follows:

  1. How can I calculate how far along a point is along an axis when the point is not actually on the axis?

  2. Is there a better/easier way to calculate this point than using two or more handles as anchors?

Thank you very much for your time. I hope I explained my question well enough, but in case I haven't, I'd be happy to elaborate.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Jun 17, 2014 at 12:23 PM

The common math solution is to use local space. Assume that left-most blue ball has its Z-axis facing right, directly along the plane. So it can "count as" the plane's personal (000,) while it's axis are the plane's personal xyz.

If we can put something in that ball's localSpace coords, then z will be distance along the plane, and x will be L/R from the center of the plane.

Use: Vector3 pLocal = leftBall.InverseTransformPoint(selectedPoint);

(and look up InverseTransform examples, since there are always some tricky bits.)

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 Fusyion · Jun 17, 2014 at 12:50 PM 0
Share

This solved my problem perfectly! I now divide the distance bottom-point by bottom-top (after getting them through InverseTransformPoint()) which gives me a value between 0 and 1. For the other axis I do the same, but between the points left and right. Thank you so much!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 Answers

Camera rotation around player while following. 6 Answers

Dose any one know how to send object current postion to preivues posistion 1 Answer

How to get the start, end, radius and direction of a CapsuleCollider in world space? 0 Answers

Make a navmesh object start moving in a circle around an object 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