Broken rope physics
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
extends SceneTree
|
||||
|
||||
# Drives the players apart at a set speed and measures actual droop: how much
|
||||
# longer the rope's real path is than the straight line between the anchors.
|
||||
# droop ~0 means visually taut. That is the thing the fix has to deliver.
|
||||
|
||||
var rope
|
||||
var p1
|
||||
var p2
|
||||
var t := 0.0
|
||||
var speed := 0.0
|
||||
var trial := 0
|
||||
var speeds := [1.0, 3.0, 6.0]
|
||||
var worst_droop_while_taut := 0.0
|
||||
var saw_taut := false
|
||||
|
||||
|
||||
func _initialize() -> void:
|
||||
var level = load("res://scenes/game_scene/levels/3dlevel.tscn").instantiate()
|
||||
root.add_child(level)
|
||||
rope = level.get_node("Path3DRope")
|
||||
p1 = level.get_node("Player1")
|
||||
p2 = level.get_node("Player2")
|
||||
rope.can_snap = false
|
||||
speed = speeds[0]
|
||||
print("max_length=%.1f reel_speed=%.1f taut_margin=%.2f" % [
|
||||
rope.max_length, rope.reel_speed, rope.taut_margin])
|
||||
|
||||
|
||||
# Length of the rope's actual polyline through every segment.
|
||||
func path_len() -> float:
|
||||
var total := 0.0
|
||||
var pts := []
|
||||
for s in rope.segments:
|
||||
var h: float = s.get_child(0).shape.height * 0.5
|
||||
pts.append(s.position + s.transform.basis.y * h)
|
||||
var last = rope.segments[-1]
|
||||
var lh: float = last.get_child(0).shape.height * 0.5
|
||||
pts.append(last.position - last.transform.basis.y * lh)
|
||||
for i in range(pts.size() - 1):
|
||||
total += pts[i].distance_to(pts[i + 1])
|
||||
return total
|
||||
|
||||
|
||||
func _physics_process(d: float) -> bool:
|
||||
t += d
|
||||
if t < 0.4:
|
||||
return false
|
||||
# Walk them apart at a controlled rate.
|
||||
p1.global_position.x -= speed * d * 0.5
|
||||
p2.global_position.x += speed * d * 0.5
|
||||
p1.velocity.x = 0.0
|
||||
p2.velocity.x = 0.0
|
||||
|
||||
var gap: float = p1.global_position.distance_to(p2.global_position)
|
||||
var droop: float = path_len() - gap
|
||||
if rope.taut:
|
||||
saw_taut = true
|
||||
worst_droop_while_taut = max(worst_droop_while_taut, droop)
|
||||
|
||||
if fmod(t, 0.6) < d:
|
||||
print(" sep=%.0fm/s gap=%5.2f rope=%5.2f path=%5.2f droop=%5.2f taut=%s" % [
|
||||
speed, gap, rope._rope_length, path_len(), droop, str(rope.taut)])
|
||||
|
||||
if gap > rope.max_length * 1.3:
|
||||
print("trial sep=%.0fm/s -> saw_taut=%s worst droop while taut=%.3f\n" % [
|
||||
speed, str(saw_taut), worst_droop_while_taut])
|
||||
trial += 1
|
||||
if trial >= speeds.size():
|
||||
quit(0)
|
||||
return true
|
||||
speed = speeds[trial]
|
||||
saw_taut = false
|
||||
worst_droop_while_taut = 0.0
|
||||
p1.global_position.x = -1.0
|
||||
p2.global_position.x = 1.0
|
||||
t = 0.0
|
||||
return false
|
||||
Reference in New Issue
Block a user