Works fairly well, although it doesn't account for changing acceleration which happens due to the changing gravity. I guess this is good enough, since it does hit if the player isn't careful
34 lines
1.1 KiB
GDScript
34 lines
1.1 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
|
|
|
|
|
|
func get_gravitation_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)
|