Created
May 16, 2019 13:02
-
-
Save anuraghazra/6afa8ed6ba68716a8ddeab0031c0602c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
update() { | |
// calculate the distance between two dots | |
let dx = this.endPoint.pos.x - this.startPoint.pos.x; | |
let dy = this.endPoint.pos.y - this.startPoint.pos.y; | |
// pythagoras theorem | |
let dist = Math.sqrt(dx * dx + dy * dy); | |
// calculate the resting distance betwen the dots | |
let diff = (this.length - dist) / dist * this.stiffness; | |
// getting the offset of the points | |
let offsetx = dx * diff * 0.5; | |
let offsety = dy * diff * 0.5; | |
// calculate mass | |
let m1 = this.startPoint.mass + this.endPoint.mass; | |
let m2 = this.startPoint.mass / m1; | |
m1 = this.endPoint.mass / m1; | |
// and finally apply the offset with calculated mass | |
if (!this.startPoint.pinned) { | |
this.startPoint.pos.x -= offsetx * m1; | |
this.startPoint.pos.y -= offsety * m1; | |
} | |
if (!this.endPoint.pinned) { | |
this.endPoint.pos.x += offsetx * m2; | |
this.endPoint.pos.y += offsety * m2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment