40 lines
975 B
GDScript
40 lines
975 B
GDScript
extends Node2D
|
|
|
|
var SpringDamper = preload("res://utils/SpringDamper.gd")
|
|
|
|
export var target = Vector3 (0, 0, 0)
|
|
export var pos = Vector3 (0, 0, 0)
|
|
export var vel = Vector3 (0, 0, 0)
|
|
export var max_speed = 100000
|
|
|
|
onready var spring_damper = SpringDamper.new(4, .99, 0.5)
|
|
|
|
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:
|
|
var spring_res = spring_damper.calc_clamped_speed(pos, vel, target, delta, max_speed)
|
|
|
|
pos = spring_res[0]
|
|
vel = spring_res[1]
|
|
|
|
var speed = vel.length()
|
|
if speed > max_speed:
|
|
vel = vel * max_speed / speed
|
|
|
|
emit_signal("position_updated", pos)
|
|
|
|
|
|
func _draw():
|
|
#draw_circle (target, 10, Color(0.9, 0.2, 0.2))
|
|
pass
|