Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added images/error_type1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 43 additions & 8 deletions js/verlet-js/verlet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -227,4 +263,3 @@ VerletJS.prototype.nearestEntity = function() {

return entity;
}