Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
  • Help Room /
avatar image
0
Question by lucas_la · Sep 04, 2021 at 08:37 PM · axisplanepolygon

Align polygon in arbitrary plane to xy world plane

Hi, I have a list of points in the 3D space that represents the vertices of a polygon (points are ordered). The points are coplanar, but they are in an arbitrary plane. I need to align these points so that they are on plane xy of the world. I want to remove the 3rd dimension, but I cant just project them removing the z axis because I want to keep the angles and distances between the points).

I thought I would be able to do it in a simple way, but I'm not having the result I intended. What I did:

I transladed the points of my polygon to the origin subtracting the first point (vertices[0]) from all other points, then used other two points of my list of points to estimate the plane normal using:

Vector3 A = vertices[1]; Vector3 B = vertices[2]; Vector3 planeNormal = Vector3.Cross(A, B) / Vector3.Magnitude(Vector3.Cross(A, B))

then, with this normal, I intended to align it with the world z-axis.

I created some spheres inside a gameObject (objectSpheres) with my vertices, and I set the local axis of the object in the same direction of the plane normal (example image 1):

objSpheres.transform.rotation = Quaternion.LookRotation(planeNormal);

and by pausing the Unity game scene, I can select objSpheres and go to the Inspector and set the local angles in Transform Rotation x,y and z to zero, and then my spheres are aligned. That's the result I want, I can see my spheres aligned with the plane (example image 2).

But when I try to set these values by code with

objSpheres.transform.localEulerAngles = new Vector3(0, 0, 0);

it doesnt work at all, looks like I only transform the objSpheres axis, but the other spheres do not move (example image 3). What am I doing wrong?alt text

ex.png (184.2 kB)
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 lucas_la · Sep 04, 2021 at 09:51 PM

I just figured out. I will publish it here to help others that may have a similar problem.

Here is the function to align a list of vertices (considering they are colinear in an arbitrary plane) into the XY plane. You may want to set the reduced dimension to zero to avoid the floating point approximation to zero.

     List<Vector3> alignVerticesOnPlane(List<Vector3> vertices)
     {
         List<Vector3> verticesOnPlane = new List<Vector3>();
 
         //1. Subtract vertices[0] from all points
         for (int i = 0; i < vertices.Count; i++)
             verticesOnPlane.Add(vertices[i] - vertices[0]);
 
         //2. Find the normalized plane normal
         Vector3 A = verticesOnPlane[1];
         Vector3 B = verticesOnPlane[2];
         Vector3 n = (Vector3.Cross(A, B) / Vector3.Magnitude(Vector3.Cross(A, B)));
 
         //3. Create a quaternion with the normal of the plane and the direction you want it to face
         Quaternion rotation = Quaternion.FromToRotation(n, Vector3.forward);    //change Vector3.forward to align with other planes
 
         //4. Apply the quaternion rotation to all points
         for (int i = 0; i < verticesOnPlane.Count; i++)
             verticesOnPlane[i] = rotation * verticesOnPlane[i];
 
         return verticesOnPlane;     //  =)
     }
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

169 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

Related Questions

Rotation is starting at 0, how do I use Math.Clamp() on it? 1 Answer

Get angle of rotation around local axis 1 Answer

Can only go horizontally while walking on walls, and not vertically 0 Answers

How can I add Z axis on input manager? 0 Answers

Parent-Child axial reference problem 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