Implement standing on moving platform
This worked immediately, wtf
This commit is contained in:
parent
36ba889eeb
commit
9401fc9c97
@ -4,6 +4,7 @@ class_name MovingPlatform
|
|||||||
|
|
||||||
var time_passed := 0.0
|
var time_passed := 0.0
|
||||||
var distance_to_pivot := 0.0
|
var distance_to_pivot := 0.0
|
||||||
|
var velocity := Vector3.ZERO
|
||||||
|
|
||||||
export(float) var move_speed = 2.0
|
export(float) var move_speed = 2.0
|
||||||
export(NodePath) var solar_system
|
export(NodePath) var solar_system
|
||||||
@ -20,11 +21,17 @@ func _process(delta):
|
|||||||
time_passed += delta
|
time_passed += delta
|
||||||
var speed_multiplier = (1.0 / distance_to_pivot) * move_speed
|
var speed_multiplier = (1.0 / distance_to_pivot) * move_speed
|
||||||
|
|
||||||
|
var translation_before = translation
|
||||||
|
|
||||||
translation = Vector3(0.0,
|
translation = Vector3(0.0,
|
||||||
cos(time_passed * speed_multiplier) * distance_to_pivot,
|
cos(time_passed * speed_multiplier) * distance_to_pivot,
|
||||||
sin(time_passed * speed_multiplier) * distance_to_pivot
|
sin(time_passed * speed_multiplier) * distance_to_pivot
|
||||||
)
|
)
|
||||||
|
|
||||||
|
velocity = (translation - translation_before) / delta
|
||||||
|
|
||||||
|
print(velocity)
|
||||||
|
|
||||||
# Rotate according to gravity
|
# Rotate according to gravity
|
||||||
var gravity_acceleration = get_node(solar_system).get_gravitation_acceleration(global_transform.origin)
|
var gravity_acceleration = get_node(solar_system).get_gravitation_acceleration(global_transform.origin)
|
||||||
|
|
||||||
|
@ -1,4 +1,11 @@
|
|||||||
extends StaticBody
|
extends StaticBody
|
||||||
|
class_name Planet
|
||||||
|
|
||||||
|
|
||||||
export(float) var mass
|
export(float) var mass
|
||||||
|
export(float) var rotate_speed := 0.0
|
||||||
|
|
||||||
|
|
||||||
|
func _process(delta):
|
||||||
|
rotate_y(rotate_speed * delta)
|
||||||
|
|
||||||
|
28
Player.gd
28
Player.gd
@ -18,6 +18,8 @@ var time_since_jump_start := 0.0
|
|||||||
var initial_jump_burst = 10.0
|
var initial_jump_burst = 10.0
|
||||||
var jump_exponent = 0.05
|
var jump_exponent = 0.05
|
||||||
|
|
||||||
|
var current_target_velocity := Vector3.ZERO
|
||||||
|
|
||||||
export(NodePath) var solar_system
|
export(NodePath) var solar_system
|
||||||
|
|
||||||
|
|
||||||
@ -32,7 +34,10 @@ func _input(event):
|
|||||||
|
|
||||||
func apply_acceleration(acceleration):
|
func apply_acceleration(acceleration):
|
||||||
# First drag, then add the new acceleration
|
# First drag, then add the new acceleration
|
||||||
velocity *= 1 - drag
|
# 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
|
||||||
|
velocity = lerp(velocity, current_target_velocity, drag)
|
||||||
velocity += acceleration
|
velocity += acceleration
|
||||||
|
|
||||||
|
|
||||||
@ -61,7 +66,12 @@ func _physics_process(delta):
|
|||||||
|
|
||||||
# Handle jumping
|
# Handle jumping
|
||||||
if jumping:
|
if jumping:
|
||||||
var e_section = max(exp(log(initial_jump_burst - 1 / jump_exponent * time_since_jump_start)), 0.0000001)
|
# Continuously apply an impulse by adding velocity: a lot at first, then less until it's 0
|
||||||
|
# Use max() to avoid NaN from being applied once no more impulse should be added
|
||||||
|
var e_section = max(
|
||||||
|
exp(log(initial_jump_burst - 1 / jump_exponent * time_since_jump_start)),
|
||||||
|
0.0
|
||||||
|
)
|
||||||
velocity += -gravity_acceleration.normalized() * e_section
|
velocity += -gravity_acceleration.normalized() * e_section
|
||||||
time_since_jump_start += delta
|
time_since_jump_start += delta
|
||||||
|
|
||||||
@ -70,8 +80,17 @@ func _physics_process(delta):
|
|||||||
#move_and_slide(velocity, -gravity_acceleration.normalized(), true)
|
#move_and_slide(velocity, -gravity_acceleration.normalized(), true)
|
||||||
var collision = move_and_collide(velocity * delta)
|
var collision = move_and_collide(velocity * delta)
|
||||||
|
|
||||||
if collision and not jumping:
|
if collision:
|
||||||
|
if collision.collider.is_in_group("MovingPlatform"):
|
||||||
|
# Inherit the moving platform's velocity
|
||||||
|
current_target_velocity = collision.collider.velocity
|
||||||
|
velocity = current_target_velocity
|
||||||
|
if not jumping:
|
||||||
on_ground = true
|
on_ground = true
|
||||||
|
else:
|
||||||
|
# We're not colliding with anything, so drag takes us towards 0
|
||||||
|
current_target_velocity = Vector3.ZERO
|
||||||
|
|
||||||
|
|
||||||
# Clamp the velocity just to be save
|
# Clamp the velocity just to be save
|
||||||
velocity.x = clamp(velocity.x, -MAX_VEL, MAX_VEL)
|
velocity.x = clamp(velocity.x, -MAX_VEL, MAX_VEL)
|
||||||
@ -84,5 +103,6 @@ func _physics_process(delta):
|
|||||||
var angle = local_down.angle_to(down)
|
var angle = local_down.angle_to(down)
|
||||||
var axis = local_down.cross(down).normalized()
|
var axis = local_down.cross(down).normalized()
|
||||||
|
|
||||||
if axis != Vector3.ZERO: # Happens if we're perfectly aligned already (local_down and down are equal)
|
# An axis of 0 happens if we're perfectly aligned already (local_down and down are equal)
|
||||||
|
if axis != Vector3.ZERO:
|
||||||
rotate(axis, angle)
|
rotate(axis, angle)
|
||||||
|
@ -108,7 +108,9 @@ glow_bloom = 0.1
|
|||||||
[node name="MovingPlatformPivot" type="Position3D" parent="."]
|
[node name="MovingPlatformPivot" type="Position3D" parent="."]
|
||||||
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -20, 0 )
|
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -20, 0 )
|
||||||
|
|
||||||
[node name="MovingPlatform" type="KinematicBody" parent="MovingPlatformPivot"]
|
[node name="MovingPlatform" type="KinematicBody" parent="MovingPlatformPivot" groups=[
|
||||||
|
"MovingPlatform",
|
||||||
|
]]
|
||||||
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 30, 0 )
|
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 30, 0 )
|
||||||
script = ExtResource( 5 )
|
script = ExtResource( 5 )
|
||||||
solar_system = NodePath("../../Planets")
|
solar_system = NodePath("../../Planets")
|
||||||
|
@ -14,6 +14,11 @@ _global_script_classes=[ {
|
|||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://MovingPlatform.gd"
|
"path": "res://MovingPlatform.gd"
|
||||||
}, {
|
}, {
|
||||||
|
"base": "StaticBody",
|
||||||
|
"class": "Planet",
|
||||||
|
"language": "GDScript",
|
||||||
|
"path": "res://Planet.gd"
|
||||||
|
}, {
|
||||||
"base": "Spatial",
|
"base": "Spatial",
|
||||||
"class": "SolarSystem",
|
"class": "SolarSystem",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
@ -21,6 +26,7 @@ _global_script_classes=[ {
|
|||||||
} ]
|
} ]
|
||||||
_global_script_class_icons={
|
_global_script_class_icons={
|
||||||
"MovingPlatform": "",
|
"MovingPlatform": "",
|
||||||
|
"Planet": "",
|
||||||
"SolarSystem": ""
|
"SolarSystem": ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user