Code cleanup
This commit is contained in:
parent
d8c60fbf41
commit
22b6023849
39
Orbiter.gd
Normal file
39
Orbiter.gd
Normal file
@ -0,0 +1,39 @@
|
||||
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)
|
16
Planets.gd
16
Planets.gd
@ -21,14 +21,17 @@ func get_closest_gravity_acceleration(position: Vector3) -> Vector3:
|
||||
var closest_planet_distance = INF
|
||||
var closest_force = 0.0
|
||||
|
||||
# Iterate through all planets to find the closest one
|
||||
for planet in get_children():
|
||||
var pos_to_center = (planet.transform.origin - position)
|
||||
var distance = pos_to_center.length()
|
||||
|
||||
if distance < closest_planet_distance:
|
||||
# This planet is closer than the previous ones -> calculate and save the values
|
||||
var force = _gravity(planet.mass * mass_multiplier, distance * distance_multiplier)
|
||||
force *= gravity_multiplier
|
||||
|
||||
# Multiply by the normalized vector towards the center to give the force a direction
|
||||
closest_force = (pos_to_center / distance) * force
|
||||
closest_planet_distance = distance
|
||||
|
||||
@ -39,6 +42,7 @@ func get_closest_gravity_acceleration(position: Vector3) -> Vector3:
|
||||
func get_gravity_acceleration(position: Vector3) -> Vector3:
|
||||
var total_force = Vector3.ZERO
|
||||
|
||||
# Add each planet's gravity force to the total force
|
||||
for planet in get_children():
|
||||
var pos_to_center = (planet.transform.origin - position)
|
||||
var distance = pos_to_center.length()
|
||||
@ -51,6 +55,18 @@ func get_gravity_acceleration(position: Vector3) -> Vector3:
|
||||
return total_force
|
||||
|
||||
|
||||
# Return the velocity needed to orbit the given planet
|
||||
func get_orbit_velocity(position: Vector3, planet):
|
||||
var pos_to_center = (planet.global_transform.origin - position)
|
||||
var distance = pos_to_center.length()
|
||||
|
||||
# raw_velocity is the velocity according to the formula, but we also need to account for the gravity_multiplier.
|
||||
var raw_velocity = -sqrt((G * planet.mass * mass_multiplier) / (distance * distance_multiplier))
|
||||
var gravity_scale_factor = sqrt(gravity_multiplier / distance_multiplier)
|
||||
|
||||
return raw_velocity * gravity_scale_factor
|
||||
|
||||
|
||||
# Formula for gravity
|
||||
static func _gravity(mass, distance):
|
||||
return (G * mass) / (distance * distance)
|
||||
|
@ -44,8 +44,7 @@ func get_center():
|
||||
func apply_acceleration(acceleration):
|
||||
# First drag, then add the new acceleration
|
||||
# For drag: Lerp towards the target velocity
|
||||
# This is usually 0, unless we're on something that's moving, in which case it is that object's
|
||||
# velocity
|
||||
# This is usually 0, unless we're on something that's moving, in which case it is that object's velocity
|
||||
velocity = lerp(velocity, current_target_velocity, drag)
|
||||
velocity += acceleration
|
||||
|
||||
@ -61,8 +60,7 @@ func get_gravity_acceleration():
|
||||
var distance_to_collision = ($GroundCheckRay.get_collision_point()
|
||||
- $GroundCheckRay.global_transform.origin).length()
|
||||
|
||||
# This factor is 0.0 if the player is exactly on the ground, and 1.0 if they're just barely
|
||||
# almost grounded
|
||||
# This factor is 0.0 if the player is exactly on the ground, and 1.0 if they're just barely almost grounded
|
||||
var factor = inverse_lerp(0.0, almost_on_ground_length, distance_to_collision)
|
||||
|
||||
return lerp(planet_gravity, total_gravity, factor)
|
||||
@ -149,8 +147,7 @@ func _physics_process(delta):
|
||||
reset_moving_platform_velocity()
|
||||
|
||||
# Apply movement to position
|
||||
# Add and subtract the move_velocity because we do want to apply it, but not remember it for
|
||||
# next frame
|
||||
# Add and subtract the move_velocity (used for velocity-based movement) because we do want to apply it, but not remember it for next frame
|
||||
velocity = move_and_slide(velocity + move_velocity) - move_velocity
|
||||
|
||||
# Clamp the velocity just to be save
|
||||
|
6
World.gd
6
World.gd
@ -18,3 +18,9 @@ func _physics_process(delta):
|
||||
if not result.empty():
|
||||
# If there was a collision, set the depth of the water at that position
|
||||
$Water.set_depth_at_position(result.position, 0.0)
|
||||
|
||||
|
||||
func _ready():
|
||||
# Create some initial waves
|
||||
for i in range(0, 11):
|
||||
$Water.set_depth_at_position(Vector3(-50 + i * 10, -66, -50), 0.0)
|
||||
|
43
World.tscn
43
World.tscn
File diff suppressed because one or more lines are too long
@ -46,16 +46,17 @@ func _physics_process(delta):
|
||||
# Calculate a new frame. First, get the data of the last frame and modify it accordingly
|
||||
var image_data = result.get_data()
|
||||
|
||||
# Set outstanding pixels
|
||||
for position_and_depth in _positions_to_set:
|
||||
var pos = position_and_depth[0]
|
||||
var depth = position_and_depth[1]
|
||||
|
||||
image_data.lock()
|
||||
image_data.set_pixel(floor(pos.x * 512), floor(pos.y * 512), Color(depth, 0.0, 0.0, 0.0))
|
||||
image_data.unlock()
|
||||
# Set outstanding pixels (after some frames, since the texture isn't initialized until then)
|
||||
if _frame_number >= 60:
|
||||
for position_and_depth in _positions_to_set:
|
||||
var pos = position_and_depth[0]
|
||||
var depth = position_and_depth[1]
|
||||
|
||||
image_data.lock()
|
||||
image_data.set_pixel(floor(pos.x * 512), floor(pos.y * 512), Color(depth, 0.0, 0.0, 0.0))
|
||||
image_data.unlock()
|
||||
|
||||
_positions_to_set.clear()
|
||||
_positions_to_set.clear()
|
||||
|
||||
# Create an ImageTexture for this new frame
|
||||
var previous_frame = ImageTexture.new()
|
||||
|
Loading…
x
Reference in New Issue
Block a user