I have been working on a project which will accurately simulate the gravitational interactions between bodies using Newton’s law of universal gravitation. To make sure it is working, I have created an approximation of the Earth-Moon system to see if the orbital period of the moon is the same as it is in real life. The problem is that the moon goes way too fast but in more or less the correct orbit. It’s as if the time scale was a lot faster. Instead of having an orbital period of 27 days (2332800 s) it goes over 800 times faster. The actual timescale is set to 1, so if all my math is right, it should be moving almost exactly the same as the moon.
To make the scale and distances easier to handle I have made all the distances 100000 times smaller (the distance between the Earth and Moon is 3844 m instead of 384400000 m), and to make up for this change I have made the gravitational constant ten times smaller because the distance is squared, so 100000^2 = 10^10. You can see all of this in the code below. And as for the masses of each object, I have made a separate variable inside the script attached to each object which can handle the enormous masses of the earth and moon, and have left the rigidbody mass of each attractor as 1 and have left the body to attract’s mass out of the equation. I’m sorry if this is really confusing.
I don’t know if the problem is with my math or with the way I’m applying forces or something else. Thank you for any help
public class Attractor : MonoBehaviour {
public Rigidbody rb;
public Vector3 startVelocity;
const double G = 0.000000000000000000006674; // Gravitational constant adapted proportionally to distances and scale (normal value = 6.674*10^-11 = 0.0000000000674)
public static List<Attractor> Attractors;
public double mass;
void FixedUpdate () {
foreach (Attractor attractor in Attractors) {
if (attractor != this) {
Attract (attractor);
}
}
}
void OnEnable () {
rb = GetComponent<Rigidbody>();
rb.velocity = startVelocity;
if (Attractors == null) {
Attractors = new List<Attractor> ();
}
Attractors.Add (this);
}
void OnDisable () {
Attractors.Remove (this);
}
void Attract(Attractor bodyToAttract) {
Rigidbody rbToAttract = bodyToAttract.rb;
Vector3 direction = rb.position - rbToAttract.position;
float distance = direction.magnitude;
Debug.Log(distance);
if (distance == 0)
return;
double forceMagnitude = G * mass / Mathf.Pow (distance, 2); // I don't need to multiply by mass on top because when you use rb.AddForce it uses the mass of the rb which is 1, If I did add the actual mass variable of the other body it would add a force way too large
Vector3 force = direction.normalized * (float)forceMagnitude;
rbToAttract.AddForce (force, ForceMode.Force);
}
}
I think you have rounding point issue.
What is you distance? I assume you keep it far below 10k units.
Further you go, bigger floating point error you may have.
I suggest try something with more resealable values at first.
Check if anything with gravity of 9.81 behaves accurately.
Then move it further away (or scale), test again and so forth.
Until you reach you current G. If you start seeing issues along the way, you probably will a bottle neck.
Then different approach you will need to find, to “trick” real physics.
I’m talking about the forceMagnitude calculation, use the mass your storing for the wanted force, but try using it in the calculation and using the impulse force mode
forceMagnitude = G * (myMass * otherMass) / Mathf.Pow(distance, 2);
on second thought this will ignor inertia i think…
maybe use the rigid body mass instead of you own variable and scaling the whole thing down by some factor?
I am making some physics test in Unity 2003 and is a surprise the inaccurate that gravity works, in a fall of 9.81m the time is 1.39s, but the system looks like not use air resistance for the delta error.
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class gravity_calibrator : MonoBehaviour
{
bool collided;
float timer;
float scale = 1.0f;
Please don’t necropost (5 years ago with custom gravity simulation). Also, please use code-tags when posting code; the above is barely readable.
Note that your post isn’t really to do with the OP subject apart from having the word “gravity” in it.
Please create your own thread describing your problem; in this case what you mean by “air resistance”. The above is the most basic part of PhysX. Gravity is added as an impulse and linear drag tries to reduce gravity; all of which is done using numerical integration using the fixed time-step.
Nope, the OP isn’t saying Unity is at fault nor are they discussing the gravity you’re referring to which isn’t the same thing; it’s discussing their custom mututal gravity attraction code. Read again.