- Home /
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.
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)?
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.
Yeah, but each circle isn't a game object with a transform component etc. so I can't use transform.localRotation = x.
Find the epicenter of the vertices and instantiate a circle there.
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?
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.
Well this code is used to generate meshes so I can't exactly do that.
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;
}
Your answer
Follow this Question
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