- Home /
Newtonian Gravity
I am attempting to make a realistic gravity simulation that can use the mass of an object to then calculate how much gravity it has using Newton's law of universal gravity. Here is what I have so far:
function gravity(float m1, m2, r)
{
f = 6.674 e -11 * m1 * m2 / (r * r);
return f;
}
Of course this is only a start, and I have no idea what to do from here. Any help is welcome!
The one suggestion I will make is that you avoid using rigidbody mass, as this variable does not function like normal mass, and becomes unstable with values exceeding 10. You can find more about how rigidbody handles physics here.
I want to try to use a variable in the script that states how much the object weighs, however I have no idea how.
@Range (0.1, 10000.0)
var mass : float;
function gravity(float m1, m2, r)
{
f = 6.674 e -11 * m1 * m2 / (r * r);
return f;
}
This should give you a proper start :) The @Range (0.1, 10000.0)
bit will more or less just limit the field between 0.1 and 10000.0 units, in the inspector. You can set the upper limit higher, if you want, all the way up to javascript's max value for floating point numbers (the exact value of which I'm not certain, but it's pretty big :P).
Right but how would I find m1, m2, r, and then I would need to intergrate the mass? (Sorry this is way outside my realm)
Hi, I have a video addressing your problem. Have a look here
Answer by nenorse · Jun 26, 2014 at 01:20 PM
Cool - for those of you who need the full class listing - here it is (in a class called GameManager)
-(this also gives a orbit between the two objects)
using UnityEngine; using System.Collections;
public class GameManager : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject[] Objects = GameObject.FindGameObjectsWithTag ("Planet");
//the gravity between each couple of object is calculated
foreach (GameObject ObjectA in Objects)
{
ObjectA.rigidbody.AddForce(new Vector3(100,0,0));
}
}
void ApplyGravity(Rigidbody A, Rigidbody B)
{
//This is how to get the distance vector between two objects.
Vector3 dist = B.transform.position - A.transform.position;
float r = dist.magnitude;
dist /= r;
//This is the Newton's equation
//G = 6.67 * 10^-11 N.m².kg^-2
double G = 6.674f * (10 ^ 11);
float force = ((float)G * A.mass * B.mass) / (r * r);
//Then, just apply the forces
A.AddForce (dist * force);
B.AddForce (-dist * force);
}
void FixedUpdate ()
{
//Get every object
GameObject[] Objects = GameObject.FindGameObjectsWithTag ("Planet");
//the gravity between each couple of object is calculated
foreach (GameObject ObjectA in Objects)
{
foreach (GameObject ObjectB in Objects)
{
//Objects must not self interact
if(ObjectA == ObjectB)
continue;
ApplyGravity(ObjectA.rigidbody, ObjectB.rigidbody);
}
}
}
}
Answer by Megasyl20 · Jan 19, 2014 at 01:22 AM
Hello ! I had the same issue, and even if the topic is old, it could help someone.. So here's a code. It's pretty simple, but it works.
void ApplyGravity(Rigidbody A, Rigidbody B)
{
//This is how to get the distance vector between two objects.
Vector3 dist = B.transform.position - A.transform.position;
float r = dist.magnitude;
dist /= r;
//This is the Newton's equation
//G = 6.67 * 10^-11 N.m².kg^-2
float force = ((float)G * A.mass * B.mass) / (r * r);
//Then, just apply the forces
A.AddForce (dist * force);
B.AddForce (-dist * force);
}
Every "planet" or whatever must have the tag "Object"
void FixedUpdate ()
{
//Get every object
Objects = GameObject.FindGameObjectsWithTag ("Object");
//the gravity between each couple of object is calculated
foreach (GameObject ObjectA in Objects)
{
foreach (GameObject ObjectB in Objects)
{
//Objects must not self interact
if(ObjectA == ObjectB)
continue;
ApplyGravity(ObjectA.rigidbody, ObjectB.rigidbody);
}
}
}
I'm sorry, i'm not into Javascript, this is C# Hope this will help ! Have a nice day.
I know this post is old, but you'll just saved me a but load of time, trying to figure out how to get the objects to interact with each other. The math part is easy (already have C# experience), but I'm new to unity. This saved me so much time.
Thank you,
It depends on what your particular project is. When looking at these posts for help, what you will get is an outline of something you want, but not exactly what you need. It is up to you do decide how to implement it into your own project.
Your answer
Follow this Question
Related Questions
Universe function on gravity script not working 1 Answer
Newtonian physics in a space game 1 Answer
How to make Object1 be upfront Object2 even though it is inside of it? 0 Answers
How can I implement Planet Gravity in my game? 2 Answers
Movement with character controller on different walls 0 Answers