Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 globalenemy · Jun 19, 2016 at 02:03 PM · circles

Possible resolutions of a circle

Greetings,

I've created a script to make a Circle as a mesh. The script, at the moment, has a slider that controls the 'resolution' of the circle between 3 and 72. The resolution of a circle is equal to the number of it's edges.

Now this is all working really fine. For the exception that several resolutions don't make a circle.

Like this one, with a resolution of '46'

alt text

I think it's down to the way I find the positions for the vertices of the circle. I just rotate a Vector3 clockwise with Quaternion.AngleAxis. Where the Angle is equal to 360 / Resolution.

Again. some resolutions do work. Like 3, 4, 6, 8, 12, 18, 36 or 72.

Can you help me find a workarround for this problem? I'm willing to provide more info if needed.

Edit: Constructor of the Circle

 public Circle(float radius, float border, int offset, int resolution, Direction direction) {
    Radius = radius;
    Border = border;
    Offset = offset;
    Resolution = resolution;
    Direction = direction;
 
    Vector3 oPoint;
    Vector3 rPoint;
    Vector3 dir;
    switch (Direction) {
       default:
          oPoint = Vector3.zero;
          rPoint = Vector3.zero;
          dir = Vector3.forward;
          break;
       case Direction.Back:
          oPoint = new Vector3(Radius + 0.1f, 0.0f, 0.0f);
          rPoint = new Vector3(Radius, 0.0f, 0.0f);
          dir = Vector3.forward;
          break;
       case Direction.Forward:
          oPoint = new Vector3(-(Radius + 0.1f), 0.0f, 0.0f);
          rPoint = new Vector3(-(Radius), 0.0f, 0.0f);
          dir = Vector3.back;
          break;
       case Direction.Left:
          oPoint = new Vector3(0.0f, 0.0f, Radius + 0.1f);
          rPoint = new Vector3(0.0f, 0.0f, Radius);
          dir = Vector3.right;
          break;
       case Direction.Right:
          oPoint = new Vector3(0.0f, 0.0f, -(Radius + 0.1f));
          rPoint = new Vector3(0.0f, 0.0f, -(Radius));
          dir = Vector3.left;
          break;
       case Direction.Up:
          oPoint = new Vector3(Radius + 0.1f, 0.0f, 0.0f);
          rPoint = new Vector3(Radius, 0.0f, 0.0f);
          dir = Vector3.down;
          break;
       case Direction.Down:
          oPoint = new Vector3(-(Radius + 0.1f), 0.0f, 0.0f);
          rPoint = new Vector3(-(Radius), 0.0f, 0.0f);
          dir = Vector3.up;
          break;
    }
 
    OffsetVector = Quaternion.AngleAxis(Offset, dir) * oPoint;
    Vertices = new Vector3[((Resolution + 1) * 2) + 1];
    int cVerts = Resolution + 1;
    float step = 360 / Resolution;
    Vertices[0] = Vector3.zero;
    float lerp = Border / Radius;
    for (int i = 1; i < Vertices.Length; i++) {
       Quaternion rot = Quaternion.AngleAxis(Offset - (step * (i - 1)), dir);
       if (i < (cVerts + 1)) Vertices[i] = rot * rPoint;
       else Vertices[i] = Vector3.Lerp(Vertices[i - cVerts], Vertices[0], lerp);
    }
 }

incompletecircle.png (27.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

2 Replies

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

Answer by Bonfire-Boy · Jun 19, 2016 at 03:03 PM

The fact that it works ok for resolutions that divide exactly into 360 is hinting that the issue is a floating point to integer conversion.

I think the specific issue in your case is your calculation of step, where you're using ints to compute a float.

float step = 360 / Resolution;

360/46 = 7.826087... but When Resolution = 46, the above code does an integer division and then casts it to a float, resulting in 7 (the true answer, but truncated). This is throwing all your angles out. When Resolution divides exactly into 360, it's not a problem because the truncation makes no difference.

Just change it to

float step = 360f / Resolution;

and a floating-point division will be performed giving you the correct angle.

And then try to get into the habit of always putting the f at the end of floating point literals.

Comment
Add comment · Show 2 · 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 Fire_Cube · Jun 19, 2016 at 03:20 PM 1
Share

you stole my answer :'(

avatar image globalenemy · Jun 19, 2016 at 03:21 PM 0
Share

wow. didn't expect it to be that easy. ^^ Ty sir!

avatar image
1

Answer by Fire_Cube · Jun 19, 2016 at 02:20 PM

Maybe if you provide the script, because the problem is that the angles are too small, since there are 46 triangles making the circle. If you can't provide the script, try looking around that.

Comment
Add comment · Show 2 · 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 globalenemy · Jun 19, 2016 at 02:26 PM 0
Share

added some code to the opener. this should suffice

avatar image Fire_Cube · Jun 19, 2016 at 03:01 PM 1
Share

I'm not sure, but I think for the resolution, you use the int / operator, ins$$anonymous$$d of the float one, try to do 300f / Resoltuion.

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

45 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

Related Questions

character controller runing in circles 0 Answers

Spawn objects in a circle with an even spacing in between? 1 Answer

How do you render points as circles rather than squares? 2 Answers

How to draw a circle around a moving game object 1 Answer

Three Vector3 points, find circle 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