- Home /
Can I use the local x,y transforms of a plane?
Hi Unity,
I'm in the process of writing a script that calculates 3D circles from 3 points. As these points are coplanar I'm trying to move these to a new x,y coordinate system on a Plane where I'll be able to calculate the circles centre and radius.
Currently, this looks a bit like this: private void CalcCircle()
{
plane.Set3Points(points[0], points[1], points[2]);
for (int i = 0; i < points.Length; i++)
{
localPoints[i] = /* calculate points[i] as local point on plane */;
}
FindCircle(localPoints[0], localPoints[1], localPoints[2], out centre, out radius);
/* convert centre back to world coords */
}
If anyone is interested, I can post the code for finding the circle :) Thanks in advance!
Three points on a plane can only deter$$anonymous$$e a circle, and that is with the requirement that those 3 points do not lie on the same line. You cannot get a unique sphere from 3 coplanar points; that requires 4 points.
edit Derivations:
3-Point circle: http://mathforum.org/library/drmath/view/55233.html
4-Point sphere: https://math.stackexchange.com/questions/894794/sphere-equation-given-4-points
Thankyou for your answer @TreyH , I am aware of the above; what I'm doing is creating a circle in 3D space, so one that can be at any orientation (think a frisbee on a tilt).
In order to do that I'm using a calculation based on a 2D circle, then rotating it; to do this I'm trying to create a new coordinate system based on the plane created by three points, I hope that clarifies my question.
Note that if you really want to use 2d coordinates all you need is to define two basis vectors of your 2d space. A plane by itself is not a coordinate system. As first basis vector you can simply use one of the relative vectors between two of your points. To get the second you just use the cross product between your first basis vector and your normal vector. To get the normal vector you just take the cross product between your two relative vectors between your 3 points.
Once you have your two basic vectors you simply convert all points into relative points (use the first point as origin) and project them onto your basis vectors using the dot product. The two numbers you get from each 3d point are your 2d coordinates. To get back 3d coordinates, just multiply each component with the corresponding basis vector.
var v0 = points[1] - points[0];
var v1 = points[2] - points[0];
var n = Vector3.Cross(v0, v1).normalized;
// 2d system
var bx = v0.normalized;
var by = Vector3.Cross(v0, n).normalized;
var origin = points[0];
// 3d --> 2d
for (int i = 0; i < points.Length; i++)
{
var relativeP = points[i] - origin;
localPoints[i] = new Vector2(Vector3.Dot(bx, relativeP), Vector3.Dot(by, relativeP));
}
// 2d --> 3d
for (int i = 0; i < points.Length; i++)
{
var p = localPoints[i];
points[i] = origin + bx * p.x + by * p.y;
}
Oh, so you're trying to basically tilt the plane that those points are on and the circle with it?
Answer by Bunny83 · Feb 14, 2018 at 01:54 AM
You shouldn't bother with converting the points into 2d coordinates. Just using some vector math you can directly calculate the circle center point. In the process you also get the normal of the plane the circle is located in. I quickly created this method:
public static Vector3 CircleCenter(Vector3 aP0, Vector3 aP1, Vector3 aP2, out Vector3 normal)
{
// two circle chords
var v1 = aP1 - aP0;
var v2 = aP2 - aP0;
normal = Vector3.Cross(v1,v2);
if (normal.sqrMagnitude < 0.00001f)
return Vector3.one * float.NaN;
normal.Normalize();
// perpendicular of both chords
var p1 = Vector3.Cross(v1, normal).normalized;
var p2 = Vector3.Cross(v2, normal).normalized;
// distance between the chord midpoints
var r = (v1 - v2) * 0.5f;
// center angle between the two perpendiculars
var c = Vector3.Angle(p1, p2);
// angle between first perpendicular and chord midpoint vector
var a = Vector3.Angle(r, p1);
// law of sine to calculate length of p2
var d = r.magnitude * Mathf.Sin(a * Mathf.Deg2Rad) / Mathf.Sin(c*Mathf.Deg2Rad);
if (Vector3.Dot(v1, aP2-aP1)>0)
return aP0 + v2 * 0.5f - p2 * d;
return aP0 + v2 * 0.5f + p2 * d;
}
It returns the 3d center position of the circle as well as the plane normal the circle is located in. I simply used it like this inside Update: p0, p1, p2 and C are Transform references to 4 sphere objects.
// inside update
Vector3 norm;
var center = CircleCenter(p0.position, p1.position, p2.position, out norm);
if (!float.IsNaN(center.x))
{
C.position = center;
var r = p0.position - center;
int count = 100;
var q = Quaternion.AngleAxis(360f/count, norm);
for(int i = 0; i < count; i++)
{
var p = r;
r = q * r;
Debug.DrawLine(center+p,center+r, Color.yellow);
}
}
The result looks like this:
Your answer
Follow this Question
Related Questions
Random.insideUnitSphere doesn't work as expected 0 Answers
Changing scale of unit 0 Answers
Move camera along 2 axes in WorldSpace 2 Answers
How can I get an OverlapBox with the exact same size and position as a BoxCollider? 1 Answer
How can I get Rigidbody.MoveRotation to rotate in world space on two axes? 1 Answer