The Lounge

Made Gravity simulator in Unity

Submitted by Astrophotography, , Thread ID: 83256

Thread Closed
06-04-2018, 01:48 AM
This post was last modified: 06-04-2018, 02:06 AM by Astrophotography
#1
I'm pretty proud of myself, sadly at such small distances the orbital speed at different locations (getting so close and so far) leads to integration errors which is bad news.
Basically, whenit gets too close to main gravitational point and due to higher gravity the closer you are, it adds extra momentum when it realistically shouldn't.
But most if not all Gravity simulation programs have some form of that and I can't fix it from what I know.
Also I know it's nothing amazing like Universe Sandbox but I think it's nice for what I learned from the experience!
It only works at such small scales as that without falling straight towards the planet, hoping to fix it soon though!
Also I know this is a really niche liking and that barely anyone thinks it matters but just wanted to post it for the people who like the same things!

Here's the code, can't add any info on how to get it working yourself but anyone who can at-least read C# should be able to pull it through.
[*]usingSystem.Collections;
[*]usingSystem.Collections.Generic;
[*]usingUnityEngine;
[*]
[*]publicclassGrav:MonoBehaviour{
[*]
[*] doubleG=6.67e-11;// Gravitiaon l constant
[*] voidUpdate(){
[*] doubleSphere1Mass=5.972e+24;// Whew big number but it's the Sphere1 Mass (Main gravitation)
[*] doubleSphere2Mass=7.34767309e+22;// Seondary object
[*]
[*] varSphere=GameObject.FindGameObjectWithTag("Sphere");// Let's fine first sphere shall we?
[*] varSphereT=GameObject.FindGameObjectWithTag("Sphereo");// Let's do same for 2nd
[*] //var com = GameObject.FindGameObjectWithTag("Com");
[*]
[*] varSphere1TransformPosition=GameObject.FindGameObjectWithTag("Sphere").transform.position;// Position for first sphere
[*] varSphere2TransformPosition=GameObject.FindGameObjectWithTag("Sphereo").transform.position;// same
[*]
[*] varRigidComp=SphereT.GetComponent<Rigidbody>();// Get rigid body component
[*] varRigidOne=Sphere.GetComponent<Rigidbody>();// same
[*] doubleMassPMass=(double)Sphere1Mass+Sphere2Mass;// Mass Plus Mass
[*]
[*] floatDistance=Vector3.Distance(Sphere1TransformPosition, Sphere2TransformPosition)/1000;// Gets distance between the 2 objects
[*] floatSquareDist=(Distance*2);// Distance of 2 objects squared
[*]
[*] varcm=(Sphere1TransformPosition-Sphere2TransformPosition).normalized;// Center of mass
[*] varCOM=(Sphere2Mass*Distance)/(Sphere1Mass+Sphere2Mass);// Center of mass
[*] //Vector3 CenterofMass = ((Sphere1Mass * Sphere1TransformPosition) + (Sphere2Mass * Sphere2TransformPosition)) / (Sphere1Mass + Sphere2Mass);
[*] //var NewPos = com.transform.position = new Vector3(CenterofMass.x, CenterofMass.y, CenterofMass.z); // Center of Mass X Y Z Vector
[*] doubleGravT=(G)*(Sphere2Mass)/(SquareDist);// Implements gravity for 2nd object
[*] doubleGrav=(G)*(Sphere1Mass)/(SquareDist);// Implements gravity
[*] varheading=(Sphere2TransformPosition-Sphere1TransformPosition).normalized;// Get the heading
[*] varheadingt=(Sphere1TransformPosition-Sphere2TransformPosition).normalized;// Get the heading
[*] RigidOne.AddForce(heading*(float)Grav, ForceMode.Acceleration);// Add the force
[*] RigidComp.AddForce(headingt*(float)GravT, ForceMode.Acceleration);
[*] //Debug.Log(CenterofMass);
[*] }
[*] voidStart()
[*] {
[*] varSphereT=GameObject.FindGameObjectWithTag("Sphere");
[*] varRigidComp=SphereT.GetComponent<Rigidbody>();
[*] RigidComp.AddForce(transform.forward, ForceMode.VelocityChange);// Let's add the force
[*] }
[*]}

Link to pastebin of code for those who don't want to open the Spoilder
https://pastebin.com/XuqB7YAX

Any comments? Any opinions? Any suggestions on what to do? Post them below!

Cheers.Thanks for reading, have a nice day!
1

RE: Made Gravity simulator in Unity

OP
#2
Added Roche limit calculation! Accurate to the ~4 km (Only tested on Mars and it's Moon,Phobos).It's basically the point where body's fragment (dependingonMass) due to tidal forces. Sadly, I have encountered an issue with it going straight towards Mars when it should go into orbit when adding horizontal force (Which I do but still happens). If anyone knows C# here's the code https://hastebin.com/ohagotenel.cs(Unity). Hoping to fix it sometime!
Thank's for reading! ~ Astro
Cheers. Have a good day!

RE: Made Gravity simulator in Unity

#3
damn bro I wish I could code like that! Nice job. Is this your first project?
google translate this

RE: Made Gravity simulator in Unity

OP
#4
08-04-2018, 07:31 PM
dooby Wrote:
damn bro I wish I could code like that! Nice job. Is this your first project?

Yep! I've actually learned C# from this. If you know how to use equations and how to implement them in C# then this is way easier than you'd think!
~Astro
Cheers. Have a good day!

RE: Made Gravity simulator in Unity

#5
That's pretty sick dude!
Looks similar to something like Universe Sandbox, or the second one. Did you pull any inspiration from those games at all? or are you just passionate about space and stuff? or just testing physics? lmao
"It never hurts to keep looking for sunshine" - A. A. Milne

RE: Made Gravity simulator in Unity

OP
#6
08-04-2018, 07:48 PM
MrFortnite Wrote:
That's pretty sick dude!
Looks similar to something like Universe Sandbox, or the second one. Did you pull any inspiration from those games at all? or are you just passionate about space and stuff? or just testing physics? lmao

All 3 of those haha
Cheers. Have a good day!

RE: Made Gravity simulator in Unity

#7
Very nice, look foward to seeing what else you can do ;o

RE: Made Gravity simulator in Unity

#8
08-04-2018, 08:02 PM
Astrophotography Wrote:
All 3 of those haha

Ahahahah, that's great dude!
Really proud of you!
I wanted to start game development, but I never really got around to getting good at it... I like just simple flat UI things and webpages.
"It never hurts to keep looking for sunshine" - A. A. Milne

RE: Made Gravity simulator in Unity

#9
Wow, looks very good. But I suggest you to add fast forward option if you are going to work more on it.
[Image: Xs3ZTCS.gif]

RE: Made Gravity simulator in Unity

OP
#10
08-04-2018, 08:29 PM
NikolaTesla Wrote:
Wow, looks very good. But I suggest you to add fast forward option if you are going to work more on it.

Yeah I will once I get this to actually *work* on large scales
Cheers. Have a good day!

Users browsing this thread: 2 Guest(s)