Add support for multiple planets
This commit is contained in:
parent
9eb34b431a
commit
6d0da014df
57
PlanetMaterial.tres
Normal file
57
PlanetMaterial.tres
Normal file
@ -0,0 +1,57 @@
|
||||
[gd_resource type="ShaderMaterial" load_steps=2 format=2]
|
||||
|
||||
[sub_resource type="Shader" id=2]
|
||||
code = "shader_type spatial;
|
||||
|
||||
|
||||
uniform vec2 scale = vec2(50.0);
|
||||
uniform bool seamless = false;
|
||||
|
||||
uniform vec3 color_scale;
|
||||
|
||||
float rand(vec2 coord) {
|
||||
return fract(sin(dot(coord, vec2(12.9898, 78.233))) * 43758.5453);
|
||||
}
|
||||
|
||||
float perlin_noise(vec2 coord) {
|
||||
vec2 i = floor(coord);
|
||||
vec2 f = fract(coord);
|
||||
|
||||
float t_l = rand(i) * 6.283;
|
||||
float t_r = rand(i + vec2(1, 0)) * 6.283;
|
||||
float b_l = rand(i + vec2(0, 1)) * 6.283;
|
||||
float b_r = rand(i + vec2(1)) * 6.283;
|
||||
|
||||
vec2 t_l_vec = vec2(-sin(t_l), cos(t_l));
|
||||
vec2 t_r_vec = vec2(-sin(t_r), cos(t_r));
|
||||
vec2 b_l_vec = vec2(-sin(b_l), cos(b_l));
|
||||
vec2 b_r_vec = vec2(-sin(b_r), cos(b_r));
|
||||
|
||||
float t_l_dot = dot(t_l_vec, f);
|
||||
float t_r_dot = dot(t_r_vec, f - vec2(1, 0));
|
||||
float b_l_dot = dot(b_l_vec, f - vec2(0, 1));
|
||||
float b_r_dot = dot(b_r_vec, f - vec2(1));
|
||||
|
||||
vec2 cubic = f * f * (3.0 - 2.0 * f);
|
||||
|
||||
float top_mix = mix(t_l_dot, t_r_dot, cubic.x);
|
||||
float bot_mix = mix(b_l_dot, b_r_dot, cubic.x);
|
||||
float whole_mix = mix(top_mix, bot_mix, cubic.y);
|
||||
|
||||
return whole_mix + 0.5;
|
||||
}
|
||||
|
||||
void fragment() {
|
||||
vec2 coord = UV * scale;
|
||||
|
||||
float value = perlin_noise(coord);
|
||||
|
||||
ALBEDO = vec3(value * color_scale);
|
||||
}"
|
||||
|
||||
[resource]
|
||||
resource_local_to_scene = true
|
||||
shader = SubResource( 2 )
|
||||
shader_param/scale = Vector2( 50, 50 )
|
||||
shader_param/seamless = false
|
||||
shader_param/color_scale = Vector3( 0.8, 0.2, 0.4 )
|
14
Planets.gd
14
Planets.gd
@ -10,18 +10,22 @@ const distance_multiplier = 318550 # thus, a radius of 20 results in an earth-
|
||||
|
||||
# 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
|
||||
const gravity_multiplier = 5.0
|
||||
|
||||
|
||||
func get_gravitation_acceleration(position: Vector3) -> Vector3:
|
||||
# TODO: Take all planets and their mass into account
|
||||
var pos_to_center = ($Earth.transform.origin - position)
|
||||
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($Earth.mass * mass_multiplier, distance * distance_multiplier)
|
||||
var force = _gravity(planet.mass * mass_multiplier, distance * distance_multiplier)
|
||||
force *= gravity_multiplier
|
||||
|
||||
return (pos_to_center / distance) * force
|
||||
total_force += (pos_to_center / distance) * force
|
||||
|
||||
return total_force
|
||||
|
||||
|
||||
# Formula for gravity
|
||||
|
71
Player.gd
71
Player.gd
@ -1,19 +1,39 @@
|
||||
extends KinematicBody
|
||||
|
||||
|
||||
const MAX_VEL = 500.0
|
||||
|
||||
var acceleration := Vector3(0.0, -9.81, 0.0)
|
||||
var velocity := Vector3(0.0, 0.0, 0.0)
|
||||
|
||||
var move_speed = 15.0
|
||||
var jump_accel = 600.0
|
||||
var move_accel = 60.0
|
||||
var rotate_speed = 2.0
|
||||
var drag = 0.05
|
||||
|
||||
# Jumping
|
||||
var jumping := false
|
||||
var on_ground := false
|
||||
var time_since_jump_start := 0.0
|
||||
|
||||
var initial_jump_burst = 10.0
|
||||
var jump_exponent = 0.05
|
||||
|
||||
export(NodePath) var solar_system
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
pass # Replace with function body.
|
||||
func _input(event):
|
||||
if event.is_action_pressed("jump") and on_ground:
|
||||
on_ground = false
|
||||
jumping = true
|
||||
time_since_jump_start = 0.0
|
||||
elif event.is_action_released("jump"):
|
||||
jumping = false
|
||||
|
||||
|
||||
func apply_acceleration(acceleration):
|
||||
# First drag, then add the new acceleration
|
||||
velocity *= 1 - drag
|
||||
velocity += acceleration
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
@ -22,34 +42,41 @@ func _physics_process(delta):
|
||||
var move_acceleration := Vector3.ZERO
|
||||
|
||||
# Movement and rotation
|
||||
if Input.is_action_pressed("ui_up"):
|
||||
move_velocity.z -= move_speed
|
||||
if Input.is_action_pressed("ui_down"):
|
||||
move_velocity.z += move_speed
|
||||
if Input.is_action_pressed("ui_left"):
|
||||
if Input.is_action_pressed("move_up"):
|
||||
move_acceleration.z -= move_accel
|
||||
if Input.is_action_pressed("move_down"):
|
||||
move_acceleration.z += move_accel
|
||||
if Input.is_action_pressed("move_left"):
|
||||
rotate(transform.basis.y, delta * rotate_speed)
|
||||
if Input.is_action_pressed("ui_right"):
|
||||
if Input.is_action_pressed("move_right"):
|
||||
rotate(transform.basis.y, -delta * rotate_speed)
|
||||
|
||||
# Make movement local
|
||||
move_velocity = transform.basis * move_velocity
|
||||
move_acceleration = transform.basis * move_acceleration
|
||||
|
||||
# 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 += gravity_acceleration.normalized() * jump_accel
|
||||
apply_acceleration((move_acceleration + gravity_acceleration) * delta)
|
||||
|
||||
# Gravity
|
||||
velocity += (gravity_acceleration - move_acceleration) * delta
|
||||
var movement = (move_velocity + velocity) * delta
|
||||
# Handle jumping
|
||||
if jumping:
|
||||
var e_section = max(exp(log(initial_jump_burst - 1 / jump_exponent * time_since_jump_start)), 0.0000001)
|
||||
velocity += -gravity_acceleration.normalized() * e_section
|
||||
time_since_jump_start += delta
|
||||
|
||||
# Apply movement to position
|
||||
var collision = move_and_collide(movement)
|
||||
# FIXME: move_and_slide might make more sense, but couldn't quite get that to work...
|
||||
#move_and_slide(velocity, -gravity_acceleration.normalized(), true)
|
||||
var collision = move_and_collide(velocity * delta)
|
||||
|
||||
# Reset the velocity if the body is colliding
|
||||
if collision:
|
||||
velocity = Vector3.ZERO
|
||||
if collision and not jumping:
|
||||
on_ground = true
|
||||
|
||||
# Clamp the velocity just to be save
|
||||
velocity.x = clamp(velocity.x, -MAX_VEL, MAX_VEL)
|
||||
velocity.y = clamp(velocity.y, -MAX_VEL, MAX_VEL)
|
||||
velocity.z = clamp(velocity.z, -MAX_VEL, MAX_VEL)
|
||||
|
||||
# Rotate down vector to face center of gravity
|
||||
var down = gravity_acceleration
|
||||
@ -57,5 +84,7 @@ func _physics_process(delta):
|
||||
var angle = local_down.angle_to(down)
|
||||
var axis = local_down.cross(down).normalized()
|
||||
|
||||
print(velocity.y)
|
||||
|
||||
if axis != Vector3.ZERO: # Happens if we're perfectly aligned already (local_down and down are equal)
|
||||
rotate(axis, angle)
|
||||
|
67
World.tscn
67
World.tscn
File diff suppressed because one or more lines are too long
@ -50,6 +50,31 @@ ui_down={
|
||||
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null)
|
||||
]
|
||||
}
|
||||
move_up={
|
||||
"deadzone": 0.5,
|
||||
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
move_down={
|
||||
"deadzone": 0.5,
|
||||
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"unicode":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
move_left={
|
||||
"deadzone": 0.5,
|
||||
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
move_right={
|
||||
"deadzone": 0.5,
|
||||
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
jump={
|
||||
"deadzone": 0.5,
|
||||
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
|
||||
[rendering]
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user