Make gravity variable, add procedural planet texture

This commit is contained in:
karl 2021-03-31 17:05:08 +02:00
parent 926684ca7a
commit 9eb34b431a
4 changed files with 118 additions and 19 deletions

4
Planet.gd Normal file
View File

@ -0,0 +1,4 @@
extends StaticBody
export(float) var mass

View File

@ -1,7 +1,29 @@
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
return ($Earth.transform.origin - position).normalized()
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)

View File

@ -4,8 +4,9 @@ extends KinematicBody
var acceleration := Vector3(0.0, -9.81, 0.0)
var velocity := Vector3(0.0, 0.0, 0.0)
var move_speed = 5.0
var jump_accel = 300.0
var move_speed = 15.0
var jump_accel = 600.0
var rotate_speed = 2.0
export(NodePath) var solar_system
@ -26,20 +27,21 @@ func _physics_process(delta):
if Input.is_action_pressed("ui_down"):
move_velocity.z += move_speed
if Input.is_action_pressed("ui_left"):
rotate(transform.basis.y, delta)
rotate(transform.basis.y, delta * rotate_speed)
if Input.is_action_pressed("ui_right"):
rotate(transform.basis.y, -delta)
rotate(transform.basis.y, -delta * rotate_speed)
# Make movement local
move_velocity = transform.basis * move_velocity
# Jumping
# Jumping and Gravity
var gravity_acceleration = get_node(solar_system).get_gravitation_acceleration(transform.origin)
if Input.is_action_just_pressed("ui_select"):
move_acceleration.y += jump_accel
move_acceleration += gravity_acceleration.normalized() * jump_accel
# Gravity
var gravity_acceleration = get_node(solar_system).get_gravitation_acceleration(transform.origin)
velocity += (gravity_acceleration * 9.81 - gravity_acceleration * move_acceleration.y) * delta
velocity += (gravity_acceleration - move_acceleration) * delta
var movement = (move_velocity + velocity) * delta
# Apply movement to position

File diff suppressed because one or more lines are too long