2022-08-17 00:50:30 +02:00
|
|
|
extends Node2D
|
|
|
|
|
2022-08-25 14:10:02 +02:00
|
|
|
var SpringDamper = preload("res://utils/SpringDamper.gd")
|
2022-08-17 00:50:30 +02:00
|
|
|
|
2022-09-21 14:09:31 +02:00
|
|
|
export var target = Vector3 (0, 0, 0)
|
|
|
|
export var pos = Vector3 (0, 0, 0)
|
|
|
|
export var vel = Vector3 (0, 0, 0)
|
2022-08-19 19:13:39 +02:00
|
|
|
export var max_speed = 100000
|
2022-08-17 00:50:30 +02:00
|
|
|
|
2022-08-19 18:48:54 +02:00
|
|
|
onready var spring_damper = SpringDamper.new(4, .99, 0.5)
|
|
|
|
|
2022-08-17 00:50:30 +02:00
|
|
|
var velocity = Vector2(0, 0)
|
|
|
|
|
|
|
|
const pos_error_eps = 0.01
|
|
|
|
|
|
|
|
signal position_updated
|
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
|
|
|
pass # Replace with function body.
|
|
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
|
|
func _process(delta):
|
|
|
|
if (target - pos).length_squared() > pos_error_eps * pos_error_eps:
|
2022-08-19 19:13:39 +02:00
|
|
|
var spring_res = spring_damper.calc_clamped_speed(pos, vel, target, delta, max_speed)
|
2022-08-19 18:48:54 +02:00
|
|
|
|
|
|
|
pos = spring_res[0]
|
|
|
|
vel = spring_res[1]
|
2022-08-17 00:50:30 +02:00
|
|
|
|
2022-08-19 19:13:39 +02:00
|
|
|
var speed = vel.length()
|
|
|
|
if speed > max_speed:
|
|
|
|
vel = vel * max_speed / speed
|
|
|
|
|
2022-08-17 00:50:30 +02:00
|
|
|
emit_signal("position_updated", pos)
|
2022-08-20 00:18:16 +02:00
|
|
|
|
2022-08-20 14:36:17 +02:00
|
|
|
|
2022-08-20 00:18:16 +02:00
|
|
|
func _draw():
|
2022-09-21 14:09:31 +02:00
|
|
|
#draw_circle (target, 10, Color(0.9, 0.2, 0.2))
|
|
|
|
pass
|