• 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 OllieCJ · Feb 13, 2018 at 07:31 PM · localpositionlocalrotationworld spacelocalspaceworld coordinates

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!

Comment
Add comment · Show 4
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 TreyH · Feb 13, 2018 at 07:50 PM 1
Share

Three points on a plane can only determine 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

avatar image OllieCJ TreyH · Feb 13, 2018 at 10:04 PM 0
Share

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.

avatar image Bunny83 · Feb 14, 2018 at 02:14 AM 0
Share

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;
 }

avatar image TreyH · Feb 14, 2018 at 03:28 AM 0
Share

Oh, so you're trying to basically tilt the plane that those points are on and the circle with it?

1 Reply

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

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:

Comment
Add comment · Show 1 · 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 OllieCJ · Feb 14, 2018 at 02:15 PM 0
Share

This is superb, precisely what I was after - thank you!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

79 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

Related Questions

Random.insideUnitSphere doesn't work as expected 0 Answers

Move camera along 2 axes in WorldSpace 2 Answers

Script won't set right rotation? 1 Answer

[Vectors] Moving towards a specific point on the XY plane by only rotating about object's local Y axis and translating along its Z axis 0 Answers

Please help me convert this script from using position to rotation? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges