-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpring.js
More file actions
27 lines (21 loc) · 703 Bytes
/
Spring.js
File metadata and controls
27 lines (21 loc) · 703 Bytes
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
var Spring = function (p1, p2, restDistance) {
this.p1 = p1
this.p2 = p2
this.restDistance = restDistance
}
Spring.prototype.update = function () {
var dx = this.p2.pos.x - this.p1.pos.x
var dy = this.p2.pos.y - this.p1.pos.y
// get distance between the two particles
var distance = new Vector(dx, dy)
// subtract rest distance
distance.setLength(distance.getLength() - this.restDistance)
// F = k . x
var force = new Vector(distance.x * STIFFNESS, distance.y * STIFFNESS)
// add the force to the forst particles
this.p1.vel.x += force.x
this.p1.vel.y += force.y
// add the opposite force to the second particle
this.p2.vel.x -= force.x
this.p2.vel.y -= force.y
}