-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphysics.js
152 lines (138 loc) · 4.15 KB
/
physics.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict'
var mass = function(x, y, vx, vy, m) {
return {
pos: new Vec(x, y),
vel: new Vec(vx, vy),
forc: new Vec(0.0, 0.0),
m: m
}
}
var spring = function(ida, idb, l, k) {
return {
ida: ida, idb: idb, l: l, k: k
}
}
var muscle = function(r, a, p) {
return {
r: r, a: a, p: p
}
}
function Physics() {
this.DEFAULT_MASS = 1.0
this.masses = {}
this.springs = {}
this.muscles = {}
this.f = 0.0005
this.g = new Vec(0.0, -0.0)
this.slip = 0.2
this.bounce = 0.99
this.dt = 1
this.wavetime = 0.0
this.wavestep = 0.05
this.height = 520
this.width = 1024
this.runtime = 0.0
this.run = function(crabs) {
var me = this
time(me.runtime, function() {
crabs.ids.forEach(function(id) {
if (me.muscles[id]) {
var muscle = me.muscles[id]
var spring = me.springs[id]
if (spring) {
spring.l = muscle.r*(1.0+muscle.a*Math.sin(me.wavetime + muscle.p))
}
}
if (me.springs[id]) {
var spring = me.springs[id]
var a = me.masses[spring.ida]
var b = me.masses[spring.idb]
var r = Vec.sub(a.pos, b.pos)
var f = Vec.scale(spring.k * (Vec.mag(r) - spring.l), Vec.norm(r))
a.forc = Vec.sub(a.forc, f)
b.forc = Vec.add(b.forc, f)
}
if (me.masses[id]) {
var mass = me.masses[id]
mass.forc = Vec.add(mass.forc, Vec.scale(-me.f, mass.vel))
mass.forc = Vec.add(mass.forc, Vec.scale(mass.m, me.g))
}
})
crabs.ids.forEach(function(id) {
if (me.masses[id]) {
var mass = me.masses[id]
mass.vel = Vec.add(mass.vel, Vec.scale(1.0/mass.m, mass.forc))
mass.pos = Vec.add(mass.pos, Vec.scale(me.dt, mass.vel))
mass.forc = Vec.zero()
}
})
crabs.ids.forEach(function(id) {
// reduce the jitteryness near the surface by requiring a minimum speed
// to actually move
var stick = function(x) {
var threshold = 10.0*(1.0-me.bounce)
if (x >= -threshold && x <= threshold) {
return 0.0
} else {
return x - threshold*Math.sign(x)
}
}
if (me.masses[id]) {
var mass = me.masses[id]
var collisionTime = null
if (mass.pos.x < 0.0) {
collisionTime = -mass.pos.x / mass.vel.x
}
if (mass.pos.x > me.width) {
collisionTime = -(mass.pos.x - me.width) / mass.vel.x
}
if (collisionTime != null) {
var collisionPos = Vec.add(mass.pos, Vec.scale(collisionTime, mass.vel))
mass.vel = new Vec(-me.bounce*stick(mass.vel.x),
(1.0 - me.slip*Math.abs(mass.vel.x/Vec.mag(mass.vel)))*mass.vel.y)
mass.pos = Vec.add(collisionPos, Vec.scale(-collisionTime, mass.vel))
}
collisionTime = null
if (mass.pos.y < 0.0) {
collisionTime = -mass.pos.y / mass.vel.y
}
if (mass.pos.y > me.height) {
collisionTime = -(mass.pos.y - me.height) / mass.vel.y
}
if (collisionTime != null) {
var collisionPos = Vec.add(mass.pos, Vec.scale(collisionTime, mass.vel))
mass.vel = new Vec((1.0 - me.slip*Math.abs(mass.vel.y/Vec.mag(mass.vel)))*mass.vel.x,
-me.bounce*stick(mass.vel.y))
mass.pos = Vec.add(collisionPos, Vec.scale(-collisionTime, mass.vel))
}
}
})
})
me.wavetime += me.wavestep
if (me.wavetime > 2*Math.PI) {
me.wavetime -= 2*Math.PI
}
}
this.resize = function(crabs) {
// do nothing
return
}
this.addMass = function(id, pos, vel, m) {
this.masses[id] = mass(pos.x, pos.y, vel.x, vel.y, m)
return id
}
this.addSpring = function(id, m0, m1, r, k) {
if (!this.masses[m0] || !this.masses[m1]) {
throw 'invalid spring'
}
this.springs[id] = spring(m0, m1, r, k)
return id
}
this.addMuscle = function(id, r, a, p) {
if (!this.springs[id]) {
throw 'invalid muscle'
}
this.muscles[id] = muscle(r, a, p)
return id
}
}