phs-galaxy/Planets.gd

57 lines
1.9 KiB
GDScript

extends Spatial
class_name SolarSystem
const G = 6.674 * pow(10, -11)
# Because we're dealing with a miniature, we want small masses and distances to have noticeable
# gravitational effects. These multipliers adapt the scale of the game to an earth-like scale.
const mass_multiplier = pow(10, 24) # thus, the earth would get a mass of 6
const distance_multiplier = 318550 # thus, a radius of 20 results in an earth-like radius
# If the gravity doesn't feel like it should, this scales it. A higher value means that the pull is
# stronger.
const gravity_multiplier = 10.0
# Return the gravity acceleration vector to the closest planet, not taking all other planets into
# account.
# This is useful for keeping something firmly on the planet while it is touching it, or for creating
# a predictable orbit.
func get_closest_gravity_acceleration(position: Vector3) -> Vector3:
var closest_planet_distance = INF
var closest_force = 0.0
for planet in get_children():
var pos_to_center = (planet.transform.origin - position)
var distance = pos_to_center.length()
if distance < closest_planet_distance:
var force = _gravity(planet.mass * mass_multiplier, distance * distance_multiplier)
force *= gravity_multiplier
closest_force = (pos_to_center / distance) * force
closest_planet_distance = distance
return closest_force
# Return the total gravity acceleration vector experienced at that position.
func get_gravity_acceleration(position: Vector3) -> Vector3:
var total_force = Vector3.ZERO
for planet in get_children():
var pos_to_center = (planet.transform.origin - position)
var distance = pos_to_center.length()
var force = _gravity(planet.mass * mass_multiplier, distance * distance_multiplier)
force *= gravity_multiplier
total_force += (pos_to_center / distance) * force
return total_force
# Formula for gravity
static func _gravity(mass, distance):
return (G * mass) / (distance * distance)