Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Lepanto7 · Apr 02, 2016 at 10:35 PM · scripting problemmathradar

How to map square coordinates to a circle?

Hey.

I have a square 2D minimap. I want to make a circular version of that minimap, so I want to map the original square minimap's X and Y coordinates onto an inscribed circle.

How does one map a square XY coordinate onto a circle of equal dimensions? The original square coordinates are stored in a Vector2 "pos", and the new circle coordinates are stored in a Vector2 "newPos".

I had tried the following function. But I must have made some error, as it's returning undefined numbers instead of proper coordinates.

     public Vector2 squareToCirclePosition(Vector2 pos)
     {
         Vector2 newPos = new Vector2(0,0);
         newPos.x = pos.x * Mathf.Sqrt(1 - ((Mathf.Pow(pos.y, 2F)) / 2));
         newPos.y = pos.y * Mathf.Sqrt(1 - ((Mathf.Pow(pos.x, 2F)) / 2));
         ...
         return newPos;
     }

Any ideas here? Sorry if I'm clueless at math. And scripting.

Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by Eno-Khaon · Apr 03, 2016 at 12:12 AM

There's one main, basic problem here: A square does not have equal dimensions to a circle.

That won't prevent you from displaying your minimap in a circular area, but you may want to ask yourself a question about what you're looking to do:

Are you sure you want to convert these distances in this manner? If you try and make use of the entire area of a square, compressed into the shape of a circle, anything not exactly on a cardinal direction on your minimap will be at a skewed relative location because rather than a 1:1 ratio of object position to map position, it will be at a 1:sqrt(x*x+y*y) ratio, depending on its position around the circle.

It may be more prudent to simply keep track of your minimap in the shape of a circle exclusively, instead. This would provide a (potentially) equidistant range on your minimap without favoring diagonals (not counting a square/rectangular minimap where that's intended) by basing what's displayed on it purely on its distance from the player.

If you do wish to proceed with remapping a square to a circle as-is, that is done by dividing the position of the object displayed by the maximum length to reach the edge of the minimap (i.e. an enemy at position (0.2, 0.4) on a -1 to 1 range minimap remaps to (0.1789, 0.3578) by dividing both side by 1.1180, the length of the line found by dividing 1 by the greater of the x or y value)

If you're absolutely sure you want to do this, remapping the vectors would be done as follows:

 Vector2 SquareToCircle(Vector2 input)
 {
     if(input.sqrMagnitude == 0)
     {
         return Vector2.zero; // Avoid division by 0
     }
     Vector2 output;
     float multiplier = 1.0f / Mathf.Max(input.x, input.y);
     output = input * multiplier; // Extend the line to the edge of the square
     float divisor = Mathf.Sqrt((output.x * output.x) + (output.y * output.y));
     output = input / divisor; // Contract the original point away from the circle's edge
     return output;
 }

Edit: The simplest alternative, if you simply wish to use a circular shape, is to assemble the square map, but then cull contents outside the circular shape.

Comment
Add comment · Show 4 · 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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What's with this absurd index array input? 1 Answer

(Scripting) Getting a circle of points in an array 1 Answer

How do I get this code to run specified amount of times 1 Answer

Change orbit while rotating. 2 Answers

Check whether something is inside a cone of raycasts 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