Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SpectralEdge · Oct 21, 2012 at 11:31 PM · c#rotationmath

Spiral galaxy math

I am creating a spinning galaxy made of blocks(for now, just so I can see where the 'systems' would be.)

I have been fiddling with this for a few days. Honestly don't know what I will do with it, just thought it would be fun to do. Learning a bit about rotations and such.

I have this so far:

 public int numberArms = 6;
     public int numberStars = 1000;
     public float galaxyRadius = 500f;
     public int spread = 100;
         
     float fHatRandom (float fRange)
     {
         float fArea = 4 * Mathf.Atan (6.0f);
         float fP = fArea * Random.value;
         return Mathf.Tan (fP / 4) * fRange / 6.0f;
     }
     
     float fLineRandom (float fRange)
     {
         float fArea = fRange * fRange / 2;
         float fP = fArea * Random.value;
         return fRange - Mathf.Sqrt (fRange * fRange - 2 * fP);
     }
     // Use this for initialization
     void Start ()
     {
         Random.seed = 100;
         
         int starsPerArm = numberStars / numberArms;
         float fAngularSpread = spread / numberArms;
         float fArmAngle = (360 / numberArms);
         for (int arm = 0; arm < numberArms; arm++)
         {
             for (int i = 0; i < starsPerArm; i++)
             {
                 float fR = fHatRandom (galaxyRadius);            
                 float fQ = fLineRandom (fAngularSpread);
                 float fK = 1;
 
                 float fA = numberArms * (fArmAngle);
 
                 float fX = fR * Mathf.Cos (Mathf.Deg2Rad * (fA + fR * fK + fQ));
                 float fY = fR * Mathf.Sin (Mathf.Deg2Rad * (fA + fR * fK + fQ));
             
                 Vector3 starPos = new Vector3 (fX, fY, arm*4);
                 Collider[] colliders = Physics.OverlapSphere (starPos, 1);
             
                 if (colliders.Length == 0)
                 {
                     GameObject star = GameObject.CreatePrimitive (PrimitiveType.Cube);
                     star.transform.position = starPos;
                     star.transform.parent = transform;
                 } else
                 { 
                     i--;//because they overlapped, we try again.
                 }
             }    
         }
     }
     // Update is called once per frame
     void Update ()
     {
         transform.Rotate(0,0,-0.1f);
     }

I have a picture of what it produces but unity is being odd about letting me post it.

As it works right now, it creates the spiral arm of the galaxy just fine. But as you can see, I just set the position of the arm to be stacked on the other arms because I cannot for the life of me figure out how to get them to rotate around the center, for that matter my center seems to be off.

I admittedly have the math skills of a gnat and have been fumbling my way through this, can someone help correct the math and get the arms/center where they belong?

If you want to test the code, just make a new class and drop it on an object, nothing else is required.

Comment
Add comment · Show 1
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 SpectralEdge · Oct 22, 2012 at 08:27 PM 0
Share
  1. I tried, it gives me an un-usable square with 1/10th of my picture visible in it. It just floats there...waiting for something.

3 Replies

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

Answer by DaveA · Oct 22, 2012 at 09:46 PM

You might find this helpful: http://wiki.unity3d.com/index.php/Particle_Spiral_Effect

You might want to create arm under one game object, then instantiate that arm (will copy the child stars), but use a different angle for each arm.

But to alter this code, try something like this:

float fA = numberArms (fArmAngle arm);

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 SpectralEdge · Oct 22, 2012 at 11:24 PM 0
Share

Saw that one and looked at it, just didn't feel like redoing all my code when I was only needing a modification to my math.

avatar image SpectralEdge · Oct 22, 2012 at 11:25 PM 0
Share

Also, that is actually the answer, kind of. What I needed was float fA = arm*fArmAngle;

Will mark your answer as correct since you got the closest and were kind enough to try.

avatar image
2

Answer by Kiloblargh · Oct 22, 2012 at 10:36 PM

All the math you can eat:

http://www.kof.zcu.cz/st/dis/schwarzmeier/galaxy_models.html

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 SpectralEdge · Oct 22, 2012 at 11:22 PM 0
Share

That actually gave me a headache just reading the first paragraph. But thanks, will keep if for someday when I am gifted with the ability to read it.

avatar image
0

Answer by pvargas · Dec 22, 2013 at 01:44 AM

maybe that is what you want....anyway, tnks for the code.

public int numberArms = 6;

public int numberStars = 1000;

public float galaxyRadius = 500f;

public int spread = 100;

 float fHatRandom(float fRange)
 {
     float fArea = 4 * Mathf.Atan(6.0f);
     float fP = fArea * Random.value;
     return Mathf.Tan(fP / 4) * fRange / 6.0f;
 }

 float fLineRandom(float fRange)
 {
     float fArea = fRange * fRange / 2;
     float fP = fArea * Random.value;
     return fRange - Mathf.Sqrt(fRange * fRange - 2 * fP);
 }
 // Use this for initialization
 void Start()
 {
     Random.seed = 100;

     int starsPerArm = numberStars / numberArms;
     float fAngularSpread = spread / numberArms;
     float fArmAngle = (360 / numberArms);
     for (int arm = 0; arm < numberArms; arm++)
     {
         for (int i = 0; i < starsPerArm; i++)
         {
             float fR = fHatRandom(galaxyRadius);
             float fQ = fLineRandom(fAngularSpread);
             float fK = 1;

             //float fA = numberArms * (fArmAngle);
             float fA = arm * fArmAngle;

             float fX = fR * Mathf.Cos(Mathf.Deg2Rad * (fA + fR * fK + fQ));
             float fY = fR * Mathf.Sin(Mathf.Deg2Rad * (fA + fR * fK + fQ));

             Vector3 starPos = new Vector3(fX, fY, arm * 4);
             Collider[] colliders = Physics.OverlapSphere(starPos, 15);

             if (colliders.Length == 0)
             {
                 GameObject star = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 star.transform.position = starPos;
                 star.transform.parent = transform;
                 Debug.Log(starPos);
             }
             else
             {
                 i--;//because they overlapped, we try again.
             }
         }
     }
 }
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

13 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Make 3D Player look exactly at mouse with ScreenToWorldPoint? (maths Question) 2 Answers

Help with rotating Camera around Pivotpoint(from A to B) 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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