30 lines
1.0 KiB
GDScript
30 lines
1.0 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 = 2.5
|
|
|
|
|
|
func get_gravitation_acceleration(position: Vector3) -> Vector3:
|
|
# TODO: Take all planets and their mass into account
|
|
var pos_to_center = ($Earth.transform.origin - position)
|
|
var distance = pos_to_center.length()
|
|
|
|
var force = _gravity($Earth.mass * mass_multiplier, distance * distance_multiplier)
|
|
force *= gravity_multiplier
|
|
|
|
return (pos_to_center / distance) * force
|
|
|
|
|
|
# Formula for gravity
|
|
static func _gravity(mass, distance):
|
|
return (G * mass) / (distance * distance)
|