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 satchel82 · Jul 28, 2016 at 10:24 AM · mathnewbiegeometrytheory

Finding Point on a Bounds (2D Rectangle) using its Centre and an Angle.

Sorry if this is a lame post. I cannot describe what I am trying to achieve as I lack the terms. This means Google is no good to me, so I ended up here.

I am not necessarily looking for copy and paste code. I was just wondering if someone could help me. In my side project using Unity for the first time, I managed to collect together the Bounds of all the elements I want. So I know the centre and I know an arbitrary angle from a controller for example.

I cannot figure out the next step on how to reach the Red Circle Point and would appreciate any advice.

Diagram

  1. Bounds (Blue)

  2. Bounds Centre (Green)

  3. Any Angle (Purple)

  4. What I need (Red) as a Vector.

untitled.png (1.5 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

1 Reply

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

Answer by Bunny83 · Jul 28, 2016 at 11:32 AM

Here i quickly wrote those two helper functions:

 public static Vector2 PointOnBounds(Bounds bounds, Vector2 aDirection)
 {
     aDirection.Normalize();
     var e = bounds.extents;
     var v = aDirection;
     float y = e.x * v.y / v.x;
     if (Mathf.Abs(y) < e.y)
         return new Vector2(e.x, y);
     return new Vector2(e.y * v.x / v.y, e.y);
 }
 public static Vector2 PointOnBounds(Bounds bounds, float aAngle)
 {
     float a = aAngle * Mathf.Deg2Rad;
     return PointOnBounds(bounds, new Vector2(Mathf.Cos(a), Mathf.Sin(a)));
 }


Note: The used Bounds has to be in the same coordinate system as the direction vector / angle. The angle is as always counter clockwise, specified in degree and "0°" points to the right.

Collider.bounds and Renderer.bounds returns the bounds in worldspace. So the box is axis aligned to the world axes. However Mesh.bounds is defined in localspace so the direction vector has to be in localspace as well.

I haven't tested the method. The idea is to project the center along the direction first onto the "x" size of the bounds. If the resulting point is outside the bounds we do the same for the y size. So in your example image the first case would be outside the bounds. If you extend the purple line until it crosses the "x size", the y position would be above the bounds rect (somewhere below your word "step" in the text above). So the second case would apply here.

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 satchel82 · Jul 28, 2016 at 02:31 PM 0
Share

Thanks for taking the time to respond to me. I will attack this post tonight when I go home. It is 90% making sense! :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Subtract square from polygon 0 Answers

Inverse projection vertex shader 0 Answers

How to find centers of inner child of a quad (geometry guy needed)? 1 Answer

How do I calculate the arc of a throw, given two positions and an angle of attack? 0 Answers

Issues with Sine Wave Generation and Rotation 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