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
1
Question by rageingnonsense · Nov 12, 2015 at 05:40 PM · vector3vector2orthographicconversionprojection

Project a Vector3 onto a plane, orthographically

How would I go about doing this? I have a Vector3 point, and a Vector3 normal defining the plane's direction. When I google this, I find an awful lot that takes projection into account, and camera stuff. But I don't need this for anything visual. I just need to convert from 3D to 2D.

If I was using Vector3.up, this would be easy; I would just 0 out the Y coordinate. But if I have a normal like (0.8, 0.1, 0.1), I don't know what to do.

Ultimately, I want to project four vectors from 3D space to 2D space so I can perform a line segment intersection test on them in 2D.

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

2 Replies

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

Answer by Bunny83 · Nov 12, 2015 at 06:16 PM

You simply project your vector onto the normal and subtract the result from your original vector. That will give you the projected vector on the plane. If you want to project a point to a plane you first need to calculate the relative vector from a point on that plane.

 Vector3 planeOrigin;
 Vector3 planeNormal;
 Vector3 point;
 
 Vector3 v = point - planeOrigin;
 Vector3 d = Vector3.Project(v, planeNormal.normalized);
 Vector3 projectedPoint = point - d;

Comment
Add comment · Show 3 · 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 rageingnonsense · Nov 12, 2015 at 06:23 PM 0
Share

Thanks for the reply! As I read your code sample though, it makes me realize that I am missing a step. In order to do the 2D line calculation, I need one of the axes to be 0. It seems to me that the best way to do that would be to add an additional step to rotate the projected point to be oriented with Vector3.up. so for instance, I project my vectors, but then after I project them on the plane, I rotate the entire plane. How would I do that?

EDIT: n/m. It is a bit out of scope of the question, and I found a function that I forgot I had for line segment intersection tests on a plane that does not care about the orientation of the plane. the projection works though.

avatar image Whitecold rageingnonsense · Nov 12, 2015 at 07:05 PM 0
Share

If you want to do the rotation you need to define an up direction on your plane. Then you can calculate the rotation using Quaternions using your up direction and your plane normal. Using the inverse you rotate your projected vector back into the xy plane. You can also rotate the vector directly and then cast it to Vector2, simply ignoring the z component.

http://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html http://docs.unity3d.com/ScriptReference/Quaternion.Inverse.html

avatar image Bunny83 rageingnonsense · Nov 13, 2015 at 05:39 AM 2
Share

Well, if you want to transform your point into a seperate 2d space you might simply want to use an empty gameobject that is rotated so one axis is aligned with your normal. That way you can use the other two axis as your 2d space. The advantage is at you can simply use InverseTransformPoint to transform a world space point into local space. In local space you can simply "null" the normal axis just like you mentioned in your question.

For example the easiest approach is to use the z axis as normal. Here you can simply use LookRotation.

 Transform emptyGO;
 Vector3 normal;
 Vector3 planeOrigin;
 Vector3 point;
 
 emptyGO.position = planeOrigin;
 emptyGO.rotation = Quaternion.LookRotation(normal, Vector3.right);
 
 Vector3 localPoint = emptyGO.InverseTransformPoint(point);
 localPoint.z = 0; // project to the plane.

Inside the local space of the transform the plane actually isn't rotated. The local coordinates behave like it's not rotated at all. The local coordinates are in the local x-y plane and represent your 2d coordinates. If you need a point back in world space, just transform it back:

 Vector3 pos = emptyGO.TransformPoint(localPoint);

That will give you the proper 3d point in the world for the given 2d local point.

avatar image
1

Answer by elenzil · Nov 12, 2015 at 06:18 PM

Your Vector3 represents a point in world-space, and you want to project it directly onto a plane ?

so say,

vP = the Vector3 of the point on the plane.

vN = the Vector3 of the normal of the plane.

vQ = the Vector3 of the point you want to project.

create a new vector, vPQ that's the vec from vP to vQ: vPQ = vQ - vP.

now project vPQ onto the normal: dot = vPQ DOT vN. vQN = vN * dot.

vQN is now the projection of your point onto the normal. now we subtract vQN from vQ to pull that vec down onto the plane:

vAnswer = vQ - vQN.

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

36 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

Related Questions

Prevent implicit cast from Vector3-Vector2 1 Answer

Should I move an object using Transform.translate, or by changing the velocity of the Rigidbody with a Vector2/3 each frame? 0 Answers

Why the object being moved is faster than the rest? 1 Answer

Why does this code not work? 1 Answer

Access 2 of the 3 components of Vector3 (Vector3.xy?) 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