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 /
This question was closed Apr 19, 2020 at 10:01 PM by martygrof3708 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by martygrof3708 · Apr 18, 2020 at 05:22 PM · 2dmathlogic

Rotate a point around another point?

I have groups of soldiers which move together in formation. Right now in rows and columns, and they form square-like formations. However, whenever I set the angle to anything other than 0 in the following algorithm, they clump and don't form up properly. What is wrong with my logic?

                 int dimx = chunkFormationDimensions[ i ].Value.x;
                 int dimy = chunkFormationDimensions[ i ].Value.y;
                 float centerx = chunkFormationPosition[ i ].Value.x;
                 float centery = chunkFormationPosition[ i ].Value.y;
                 float spacex = chunkFormationSpacing[ i ].Value.x;
                 float spacey = chunkFormationSpacing[ i ].Value.y;
                 float angle = math.radians( chunkFormationRotation[ i ].Value ); // chunkFormationRotation[ i ].Value; //
 
                 for ( int j = 0; j < unitBuffer.Length; j++ )
                 {       
                     Entity entity = unitBuffer[ j ].Entity;
 
                     int col = j % dimx;
                     int row = j / dimx;
 
                     float unitx = centerx - ( dimx * spacex ) / 2 + ( col * spacex );
                     float unity = centery - ( dimy * spacey ) / 2 + ( row * spacey );
 
                     unitx = math.cos( angle ) * ( unitx - centerx ) - math.sin( angle ) * ( unity - centery ) + centerx;
                     unity = math.sin( angle ) * ( unitx - centerx ) + math.cos( angle ) * ( unity - centery ) + centery;
 
                     unitFormationPositionFromEntity[ entity ] = new UnitFormationPosition { Value = new float2( unitx , unity ) };
                 }
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

  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Apr 19, 2020 at 03:55 AM

I haven't checked all of your code but I do see a major mistake. You're modifying "unitx" before you calculate "unity". Therefore your "unity" calculation uses the wrong unitx value.


What you can also see immediately that you can simplify your unitx and y calculation before your rotation. You subtract the row / col indices from centerx / y and before applying the rotation you subtract centerx / y. Therefore centerx and y cancel:

 float s = math.sin( angle );
 float c = math.cos( angle );
 // [ ... ]
 float unitx = spacex * (col - dimx * 0.5f);
 float unity = spacey * (row - dimy * 0.5f);
 float newUnitx = c * unitx - s * unity + centerx;
 float newUnity = s * unitx + c * unity + centery;

As you can see it becomes much simpler when you remove centerx and y. I also stored the sine and cosine into seperate variables since they are always the same for the same angle. Therefore they can be calculated once before the loops.

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 martygrof3708 · Apr 19, 2020 at 10:01 PM 0
Share

Thank you! I completely missed that.. I gotta slow down lol

avatar image
0

Answer by jmailloux11 · Apr 18, 2020 at 05:46 PM

transform.RotateAround(other gameobject's transform)

is a much simpler way to do it.

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 martygrof3708 · Apr 18, 2020 at 05:49 PM 0
Share

I'm using ECS no GameObjects.

Follow this Question

Answers Answers and Comments

267 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 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 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 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

3D Video within Scene 1 Answer

New Input system mouse returns wrong values 0 Answers

Looking at a target in 2D 0 Answers

What's a more efficient way to sort an array into multiple groups? 1 Answer

2D Animation does not start 1 Answer


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