phs-galaxy/Planets.gd

34 lines
1.1 KiB
GDScript3
Raw Normal View History

2021-03-25 21:36:04 +01:00
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.
2021-04-22 23:40:01 +02:00
const gravity_multiplier = 5.0
2021-03-25 21:36:04 +01:00
func get_gravitation_acceleration(position: Vector3) -> Vector3:
2021-04-22 23:40:01 +02:00
var total_force = Vector3.ZERO
2021-04-22 23:40:01 +02:00
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
2021-04-22 23:40:01 +02:00
return total_force
# Formula for gravity
static func _gravity(mass, distance):
return (G * mass) / (distance * distance)