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 /
avatar image
0
Question by Chimera3D · Feb 01, 2015 at 03:45 AM · rotationcirclecylinder

Determine Rotation Between Two Points

I have two positions and build circles around them then rotate one of those circles so that one would face the other then connect the vertices of the two circles to make a cylinder. The only problems is the circle is rotated so that the vertices don't correspond properly anymore in some cases. Here's the code that creates the circle vertices.

         Quaternion orientation = Quaternion.FromToRotation(Vector3.up, (circleCenter1 - circleCenter2).normalized);
         Vector3 zDir = orientation * new Vector3(0,0,1);
         Vector3 xDir = orientation * new Vector3(1,0,0);
         float angleStep = Mathf.PI * 2.0f / vertexCount;
         float angle = 0;
         for(int i = 0; i < vertexCount; i++){
             Vector3 norm = Mathf.Cos(angle) * xDir + Mathf.Sin (angle) * zDir;
             Vector3 newVec3 = norm * radius + center;
             vertices.Add (newVec3);
             angle += angleStep;
         }
 

And here's an image of the issue.

alt text

My question is how can I rotate the circle so that the vertices correspond (i.e. a line segment going from vertex1 of circle1 to vertex1 of circle2 is parallel to the vector circleCenter1 - circleCenter2)?

twisted-cylinders.png (25.0 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jpthek9 · Feb 01, 2015 at 04:10 AM

Step 1. Rotate the circle

 transform.localRotation = Quaternion.RotateTowards(circle1.transform.rotation, circle2.transform.rotation, 5);

Step 2. $$$

That's assuming the circles match up if they're the same rotation. If they don't, then you need to find out what rotation they need to match up meaning that given a corresponding pair of vertices and that's A LOT of maths I'm not entitled to explain. Hopefully you don't and you can instead just use the Quaternion.RotateTowards() method.

Comment
Add comment · Show 10 · 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 Chimera3D · Feb 01, 2015 at 04:47 AM 0
Share

Yeah, but each circle isn't a game object with a transform component etc. so I can't use transform.localRotation = x.

avatar image jpthek9 · Feb 01, 2015 at 04:55 AM 0
Share

Find the epicenter of the vertices and instantiate a circle there.

avatar image Chimera3D · Feb 01, 2015 at 05:12 AM 0
Share

I'm a little confused. The circle vertices are just stored in an array of Vector3's and and the white lines/green spheres above are rendered using gizmos so that I can see the results, there's no actual "circle" game object. What do you mean by "instantiate a circle there"? And by "epicenter" do you mean barycenter?

avatar image jpthek9 · Feb 01, 2015 at 05:34 AM 0
Share

Well childrens' local positions are automatically calculated. The math gets pretty intense and I've only implemented my own transform system in 2D - haven't thought about it too much in 3D. Your best bet if you don't want to reinvent the wheel is to instantiate objects as representations of the vertices and circle then do everything you need with those.

avatar image Chimera3D · Feb 01, 2015 at 05:40 AM 0
Share

Well this code is used to generate meshes so I can't exactly do that.

Show more comments
avatar image
0

Answer by dethtoll · Feb 01, 2015 at 08:01 AM

Just requires a bit of 3D math. This could be a decent interview question ;)

Here's the code:

  Vector3 circleCenter1 = // Center of first circle
  Vector3 circleCenter2 = // Center of second circle
     
  Vector3 vert1 = // Any of the vertices on the first circle
  Vector3 vert2 = // The vertex on the second circle that is expected to *correspond* (align) with vert1
 
  // Compute normal of the circles
  Vector3 circleNormal = Vector3.Normalize(circleCenter2 - circleCenter1);
 
  // Project vert1 on to circle2 (simple plane projection)
  Vector3 vert1OnCircle2 = vert1 + Vector3.Dot(circleCenter2 - vert1, circleNormal) * circleNormal;
 
  // Compute local rotation to rotate vert1 on to vert1OnCircle2
  Quaternion rotation = Quaternion.FromToRotation(vert2 - circleCenter2, vert1OnCircle2 - circleCenter2);
 
  // Now rotate all vertices on circle2 
  for (int i = 0; i < vertexCount; ++i)
  {
      Vector3 vert = circle2Verts[i];
      Vector3 newVert = circleCenter2 + rotation * (vert - circleCenter2);
      circle2Verts[i] = newVert;
  }
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

21 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

Related Questions

Rotate GameObjects to define a cylinder 1 Answer

Cylinder touch rotation 1 Answer

cylinder pulling mechanism 0 Answers

Generating a procedural cylindrical mesh following a path 1 Answer

How can I create objects that are placed along the circumference of a circle,and then expand and contract the circle 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