forked from JohnMeadow1/GodotSimulations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.gd
48 lines (37 loc) · 1.2 KB
/
node.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
extends Node2D
var velocity = Vector2( 0.0, 0.0 )
var previous_position = Vector2( 0.0, 0.0 )
var F = Vector2( 0.0, 0.0 )
var mass = 1.0
var mu = 1.0 # 1./mass
var neighbors = []
var ks = []
var ls = []
export var is_static = false
func _ready():
previous_position = position - velocity * get_physics_process_delta_time()
if is_static:
get_node("sprite").modulate = Color( 1.0, 0.0, 0.0 )
func _physics_process(delta):
if !is_static:
verlet(delta)
func set_velocity(v):
velocity = v
previous_position = position - velocity * get_physics_process_delta_time()
func set_mass(m):
mass = m
mu = pow( m, -1.0 )
func euler(delta):
velocity += force() * mu * delta
position += velocity * delta
func verlet(delta):
var new_position = 2 * position - previous_position + force() * mu * pow( delta , 2.0 )
previous_position = position
position = new_position
velocity = ( position - previous_position ) / delta
func force():
F = Vector2( 0.0, 0.0 )
for i in range( neighbors.size() ):
var direction = neighbors[i].position - position
F += -ks[i] * ( ls[i] - direction.length() ) * direction.normalized()
return F - 1 * velocity