40 lines
1.3 KiB
GDScript
40 lines
1.3 KiB
GDScript
extends KinematicBody
|
|
|
|
# Orbits the parent planet by velocity.
|
|
|
|
|
|
var time_passed := 0.0
|
|
var velocity := Vector3.ZERO
|
|
|
|
export(float) var move_speed_factor = 1.0
|
|
export(NodePath) var solar_system
|
|
|
|
|
|
func _ready():
|
|
var velocity_value = get_node(solar_system).get_orbit_velocity(global_transform.origin, get_parent())
|
|
|
|
# We need a vector perpendicular to the gravity acceleration to apply the velocity along.
|
|
# So we get the cross product of that and some arbitrary other vector, in this case Vector3.RIGHT
|
|
var gravity_acceleration = get_node(solar_system).get_closest_gravity_acceleration(global_transform.origin)
|
|
var other = Vector3.RIGHT
|
|
|
|
# Apply the velocity
|
|
velocity = gravity_acceleration.cross(other).normalized() * velocity_value
|
|
|
|
|
|
func _process(delta):
|
|
# Apply gravity as acceleration to velocity, then velocity to position
|
|
var gravity_acceleration = get_node(solar_system).get_closest_gravity_acceleration(global_transform.origin)
|
|
velocity += gravity_acceleration * delta
|
|
|
|
move_and_slide(velocity)
|
|
|
|
# Rotate down vector to face center of gravity -> tidally locked
|
|
var down = gravity_acceleration
|
|
var local_down = transform.basis * Vector3.DOWN
|
|
var angle = local_down.angle_to(down)
|
|
var axis = local_down.cross(down).normalized()
|
|
|
|
if axis != Vector3.ZERO: # Happens if we're perfectly aligned already (local_down and down are equal)
|
|
rotate(axis, angle)
|