The Lounge
Made Gravity simulator in Unity
Submitted by Astrophotography, 06-04-2018, 01:48 AM, Thread ID: 83256
Thread Closed
06-04-2018, 01:48 AM
#1 This post was last modified: 06-04-2018, 02:06 AM by Astrophotography
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.
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!
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
[*] }
[*]}
[*]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!
Users browsing this thread: