Add a moving platform

No player velocity handling etc yet, just a moving platform
This commit is contained in:
karl 2021-05-05 19:59:58 +02:00
parent 6d0da014df
commit 36ba889eeb
5 changed files with 90 additions and 26 deletions

38
MovingPlatform.gd Normal file
View File

@ -0,0 +1,38 @@
extends KinematicBody
class_name MovingPlatform
var time_passed := 0.0
var distance_to_pivot := 0.0
export(float) var move_speed = 2.0
export(NodePath) var solar_system
func _process(delta):
# Move the platform using the pivot and sin/cos.
# Theoretically we could just use the gravity from the solar system and actually orbit, but
# that seems like it'd get unstable very easily.
if distance_to_pivot == 0.0:
distance_to_pivot = get_parent().global_transform.origin.distance_to(global_transform.origin)
time_passed += delta
var speed_multiplier = (1.0 / distance_to_pivot) * move_speed
translation = Vector3(0.0,
cos(time_passed * speed_multiplier) * distance_to_pivot,
sin(time_passed * speed_multiplier) * distance_to_pivot
)
# Rotate according to gravity
var gravity_acceleration = get_node(solar_system).get_gravitation_acceleration(global_transform.origin)
# Rotate down vector to face center of gravity
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)

View File

@ -1,6 +1,6 @@
[gd_resource type="ShaderMaterial" load_steps=2 format=2]
[sub_resource type="Shader" id=2]
[sub_resource type="Shader" id=1]
code = "shader_type spatial;
@ -51,7 +51,7 @@ void fragment() {
[resource]
resource_local_to_scene = true
shader = SubResource( 2 )
shader = SubResource( 1 )
shader_param/scale = Vector2( 50, 50 )
shader_param/seamless = false
shader_param/color_scale = Vector3( 0.8, 0.2, 0.4 )

View File

@ -84,7 +84,5 @@ 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)

File diff suppressed because one or more lines are too long

View File

@ -9,12 +9,18 @@
config_version=4
_global_script_classes=[ {
"base": "KinematicBody",
"class": "MovingPlatform",
"language": "GDScript",
"path": "res://MovingPlatform.gd"
}, {
"base": "Spatial",
"class": "SolarSystem",
"language": "GDScript",
"path": "res://Planets.gd"
} ]
_global_script_class_icons={
"MovingPlatform": "",
"SolarSystem": ""
}