-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tension.gd
55 lines (37 loc) · 1.17 KB
/
test_tension.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
49
50
51
52
53
54
55
extends Node3D
@onready var ball_1 = $Sphere1
@onready var ball_2 = $Sphere2
var velocity = Vector3()
var mass = 1.0
var gravity = 9.81
var friction = 1.0
var rest_point = Vector3()
var velocity_2 = Vector3()
func _physics_process(delta):
simulate_ball_1(delta)
simulate_ball_2(delta)
func simulate_ball_1(delta):
var net_force = Vector3()
if Input.is_action_pressed("ui_right"):
net_force.x += 10.0
if Input.is_action_pressed("ui_left"):
net_force.x -= 10.0
var f_friction = velocity_2 * friction
net_force -= f_friction
var acceleration = net_force / mass
velocity_2 += acceleration * delta
ball_1.position += velocity_2 * delta
rest_point = ball_1.position + Vector3(0, -1.5, 0)
func simulate_ball_2(delta):
var net_force = Vector3()
net_force.y -= mass * gravity
var displacement = ball_2.position - rest_point
var spring_constant = 50.0
var f_spring = displacement.normalized() * spring_constant * displacement.length()
net_force -= f_spring
var damping_rate = 1.0
var f_damping = velocity * damping_rate
net_force -= f_damping
var acceleration = net_force / mass
velocity += acceleration * delta
ball_2.position += velocity * delta