Created
February 16, 2018 17:22
-
-
Save neiltron/d71b171f0c23ee316854aea9fa56ff0e to your computer and use it in GitHub Desktop.
3d verlet constraint
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
export default class Constraint { | |
constructor (opts) { | |
this.vertices = opts.vertices; | |
this.points = opts.points; | |
this.index = opts.index; | |
this.restingDistance = opts.restingDistance || this.vertices[0].distanceTo(this.vertices[1]); | |
this.stiffness = this.index == 0 ? .05 : .005; | |
this.masses = opts.masses || [.01, .01]; | |
} | |
update(dt = 0, time) { | |
let distance = this.vertices[0].distanceTo(this.vertices[1]); | |
let diffX = this.vertices[0].x - this.vertices[1].x; | |
let diffY = this.vertices[0].y - this.vertices[1].y; | |
let diffZ = this.vertices[0].z - this.vertices[1].z; | |
let difference = (this.restingDistance - distance) / distance; | |
let translateX = diffX * this.stiffness * difference; | |
let translateY = diffY * this.stiffness * difference; | |
let translateZ = diffZ * this.stiffness * difference; | |
this.vertices[0].x += translateX * (2 / this.masses[0]); | |
this.vertices[0].y += translateY * (2 / this.masses[0]); | |
this.vertices[0].z += translateZ * (2 / this.masses[0]); | |
this.vertices[1].x -= translateX * (2 / this.masses[1]); | |
this.vertices[1].y -= translateY * (2 / this.masses[1]); | |
this.vertices[1].z -= translateZ * (2 / this.masses[1]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment