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 ugriffin · Oct 13, 2016 at 03:41 PM · c#2dspritetrigonometry

Find point of top of rotated sprite

So, I have this object that 'orbits' around a planet object. I need to determine if the top of the object is closer to the epicentre of the planet or if the bottom of the object is closer. I have the following code, however it is simply not working:

 Bounds theBounds = spriteRenderer.bounds;
 
 //float angle = transform.rotation.eulerAngles.z;
 
 float angle = Mathf.Deg2Rad*transform.rotation.eulerAngles.z;
 
 Vector2 topBus = new Vector2(transform.position.x * Mathf.Cos(angle), transform.position.y+theBounds.extents.y * Mathf.Sin(angle));
 Vector2 bottomBus = new Vector2(transform.position.x * Mathf.Cos(angle), transform.position.y-theBounds.extents.y * Mathf.Sin(angle));
 
 if(Vector2.Distance(topBus,planet.transform.position) < Vector2.Distance(bottomBus,planet.transform.position))
 {
     rotationDirection = -90;
 }
 else
 {
     rotationDirection = 90;
 }

I am completely stumped over why this is not working. I was wondering if I could get a pointer over what's wrong or alternative code that would work.

Thanks a lot!

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
0
Best Answer

Answer by ugriffin · Oct 14, 2016 at 11:17 AM

I have found a solution to my problem after a great deal of Google searching. There was a mistake in the formula used to calculate the actual points.

The correct code is:

 //Class to hold the function in for simplicity´s sake. 
 public class SpriteTools 
 {
 
     //float angle: the angle of rotation (if any) for our sprite. 
     //Vector 2 pivot: the pivot from which the sprite is rotating (usually the transform)
     //Vector2 point: the point (using relative sprite x and y) that we wish to calculate. 
     public static Vector2 returnWorldSpacePointFromSprite(Vector2 pivot,Vector2 point,float angle = 0.0f)
     {
 
         return new Vector2((point.x - pivot.x) * Mathf.Cos(angle) - (point.y - pivot.y) * Mathf.Sin(angle) + pivot.x,(point.y - pivot.y) * Mathf.Cos(angle) + (point.x - pivot.x) * Mathf.Sin(angle) + pivot.y);
     }
 
     
 }

Example usage:

 Bounds theBounds = spriteRenderer.bounds;
     
     float angle = Mathf.Deg2Rad*transform.rotation.eulerAngles.z;
     
     Vector2 topBus = SpriteTools.returnWorldSpacePointFromSprite((Vector2)transform.position,new Vector2(transform.position.x,transform.position.y+theBounds.extents.y),angle);
     Vector2 bottomBus = SpriteTools.returnWorldSpacePointFromSprite((Vector2)transform.position,new Vector2(transform.position.x,transform.position.y-theBounds.extents.y),angle);
     
     
     if(Vector2.Distance(topBus,planet.transform.position) > Vector2.Distance(bottomBus,planet.transform.position))
     {
     rotationDirection = -90;
     }
     else
     {
     rotationDirection = 90;
     }
 

Do note that this code won't account for sprites that are scaled up or down but it can be easily modified to do so.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Question on Sprite and Movement 0 Answers

Texturing custom 2d sprite 0 Answers

Handling Multiple Resolutions in 2D? 0 Answers

In-Game 2D Sprite Building System 0 Answers

Top Down Sprite Facing Mouse 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