Make gravity variable, add procedural planet texture
This commit is contained in:
parent
926684ca7a
commit
9eb34b431a
24
Planets.gd
24
Planets.gd
@ -1,7 +1,29 @@
|
|||||||
extends Spatial
|
extends Spatial
|
||||||
class_name SolarSystem
|
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:
|
func get_gravitation_acceleration(position: Vector3) -> Vector3:
|
||||||
# TODO: Take all planets and their mass into account
|
# 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)
|
||||||
|
18
Player.gd
18
Player.gd
@ -4,8 +4,9 @@ extends KinematicBody
|
|||||||
var acceleration := Vector3(0.0, -9.81, 0.0)
|
var acceleration := Vector3(0.0, -9.81, 0.0)
|
||||||
var velocity := Vector3(0.0, 0.0, 0.0)
|
var velocity := Vector3(0.0, 0.0, 0.0)
|
||||||
|
|
||||||
var move_speed = 5.0
|
var move_speed = 15.0
|
||||||
var jump_accel = 300.0
|
var jump_accel = 600.0
|
||||||
|
var rotate_speed = 2.0
|
||||||
|
|
||||||
export(NodePath) var solar_system
|
export(NodePath) var solar_system
|
||||||
|
|
||||||
@ -26,20 +27,21 @@ func _physics_process(delta):
|
|||||||
if Input.is_action_pressed("ui_down"):
|
if Input.is_action_pressed("ui_down"):
|
||||||
move_velocity.z += move_speed
|
move_velocity.z += move_speed
|
||||||
if Input.is_action_pressed("ui_left"):
|
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"):
|
if Input.is_action_pressed("ui_right"):
|
||||||
rotate(transform.basis.y, -delta)
|
rotate(transform.basis.y, -delta * rotate_speed)
|
||||||
|
|
||||||
# Make movement local
|
# Make movement local
|
||||||
move_velocity = transform.basis * move_velocity
|
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"):
|
if Input.is_action_just_pressed("ui_select"):
|
||||||
move_acceleration.y += jump_accel
|
move_acceleration += gravity_acceleration.normalized() * jump_accel
|
||||||
|
|
||||||
# Gravity
|
# Gravity
|
||||||
var gravity_acceleration = get_node(solar_system).get_gravitation_acceleration(transform.origin)
|
velocity += (gravity_acceleration - move_acceleration) * delta
|
||||||
velocity += (gravity_acceleration * 9.81 - gravity_acceleration * move_acceleration.y) * delta
|
|
||||||
var movement = (move_velocity + velocity) * delta
|
var movement = (move_velocity + velocity) * delta
|
||||||
|
|
||||||
# Apply movement to position
|
# Apply movement to position
|
||||||
|
91
World.tscn
91
World.tscn
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user