diff --git a/images/error_type1.png b/images/error_type1.png new file mode 100644 index 0000000..bdcde3b Binary files /dev/null and b/images/error_type1.png differ diff --git a/js/verlet-js/verlet.js b/js/verlet-js/verlet.js index 44a8710..8cc817d 100644 --- a/js/verlet-js/verlet.js +++ b/js/verlet-js/verlet.js @@ -44,6 +44,34 @@ Particle.prototype.draw = function(ctx) { ctx.fill(); } +/** +* +* Computes time of intersection of a particle with a wall +* +* @param {Vec2} line wall's root position +* @param {Vec2} p particle's position +* @param {Vec2} dir walls's direction +* @param {Vec2} v particle's velocity +*/ +var intersectionTime = function(wall, p, dir, v) { + if (dir.x != 0) { + var denominator = v.y - dir.y * v.x / dir.x ; + if (denominator == 0) return undefined; // Movement is parallel to wall + var numerator = wall.y + dir.y * (p.x - wall.x) / dir.x - p.y; + return numerator / denominator; + } else { + if (v.x == 0) return undefined; // parallel again + var denominator = v.x; + var numerator = wall.x - p.x; + return numerator / denominator; + } +} + +var intersectionPoint = function(wall, p, dir, v) { + var t = intersectionTime(wall, p, dir, v); + return new Vec2(p.x + v.x * t, p.y + v.y * t); +} + var VerletJS = function(width, height, canvas) { this.width = width; this.height = height; @@ -54,16 +82,24 @@ var VerletJS = function(width, height, canvas) { this.draggedEntity = null; this.selectionRadius = 20; this.highlightColor = "#4f545c"; - + var v = new Vec2(); this.bounds = function (particle) { - if (particle.pos.y > this.height-1) - particle.pos.y = this.height-1; + v.mutableSet(particle.pos); + v.mutableSub(particle.lastPos); + if (particle.pos.y > this.height-1) { + particle.pos.mutableSet( + intersectionPoint(new Vec2(0,this.height-1), particle.lastPos, new Vec2(1,0), v)); + } - if (particle.pos.x < 0) - particle.pos.x = 0; + if (particle.pos.x < 0) { + particle.pos.mutableSet( + intersectionPoint(new Vec2(0,0), particle.pos, new Vec2(0,1), v)); + } - if (particle.pos.x > this.width-1) - particle.pos.x = this.width-1; + if (particle.pos.x > this.width-1) { + particle.pos.mutableSet( + intersectionPoint(new Vec2(this.width-1,0), particle.pos, new Vec2(0,1), v)); + } } var _this = this; @@ -227,4 +263,3 @@ VerletJS.prototype.nearestEntity = function() { return entity; } -