Permalink
Browse files

Rect added, in build, VerletPhysics2D started

  • Loading branch information...
hapticdata committed Apr 22, 2011
1 parent a10e39d commit 1c6bec2bb3840b6bc1449cfdfce457a57abefd42
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,212 @@
+toxi.Rect = function(a,b,width,height){
+ if(arguments.length === 2){ //then it should've been 2 Vec2D's
+ if(!(a instanceof toxi.Vec2D)){
+ throw new Error("toxi.Rect received incorrect parameters");
+ } else {
+ this.x = a.x;
+ this.y = a.y;
+ this.width = b.x - this.x;
+ this.height = b.y - this.y;
+ }
+ } else if(arguments.length === 4){
+ this.x = a;
+ this.y = b;
+ this.width = width;
+ this.height = height;
+ } else if(arguments.length > 0){
+ throw new Error("toxi.Rect received incorrect parameters");
+ }
+};
+
+toxi.Rect.fromCenterExtent = function(center,extent){
+ return new toxi.Rect(center.sub(extent),center.add(extent));
+};
+
+toxi.Rect.prototype = {
+ containsPoint: function(p){
+ var px = p.x;
+ var py = p.y;
+ if(px < this.x || px >= this.x + this.width){
+ return false;
+ }
+ if(py < this.y || py >= this.y + this.height){
+ return false;
+ }
+ return true;
+ },
+
+ copy: function(){
+ return new toxi.Rect(this.x,this.y,this.width,this.height);
+ },
+
+ getArea: function(){
+ return this.width * this.height;
+ },
+
+ getAspect: function(){
+ return this.width / this.height;
+ },
+
+ getBottom: function(){
+ return this.y + this.height;
+ },
+
+ getBottomRight: function(){
+ return new toxi.Vec2D(this.x + this.width, this.y + this.height);
+ },
+
+ getCentroid: function(){
+ return new toxi.Vec2D(this.x + this.width * 0.5, this.y + this.height * 0.5);
+ },
+
+ getDimensions: function(){
+ return new toxi.Vec2D(this.width,this.height);
+ },
+
+ getEdge: function(id){
+ var edge = undefined;
+ switch(id){
+ case 0:
+ edge = new toxi.Line2D(
+ new toxi.Vec2D(this.x,this.y),
+ new toxi.Vec2D(this.x + this.width, this.y)
+ );
+ break;
+ case 1:
+ edge = new toxi.Line2D(
+ new toxi.Vec2D(this.x + this.width, this.y),
+ new toxi.Vec2D(this.x + this.width, this.y + this.height)
+ );
+ break;
+ case 2:
+ edge = new toxi.Line2D(
+ new toxi.Vec2D(this.x, this.y + this.height),
+ new toxi.Vec2D(this.x + this.width, this.y + this.height)
+ );
+ break;
+ case 3:
+ edge = new toxi.Line2D(
+ new toxi.Vec2D(this.x,this.y),
+ new toxi.Vec2D(this.x,this.y+this.height)
+ );
+ break;
+ default:
+ throw new Error("edge ID needs to be 0...3");
+ }
+ return edge;
+ },
+
+ getLeft: function(){
+ return this.x;
+ },
+
+ getRight: function(){
+ return this.x + this.width;
+ },
+
+ getTop: function(){
+ return this.y;
+ },
+
+ getTopLeft: function(){
+ return new toxi.Vec2D(this.x,this.y);
+ },
+
+ intersectsRay: function(ray,minDist,maxDist){
+ //returns Vec2D of point intersection
+ var invDir = ray.getDirection().reciprocal();
+ var signDirX = invDir.x < 0;
+ var signDirY = invDir.y < 0;
+ var min = this.getTopLeft();
+ var max = this.getBottomRight();
+ var bbox = signDirX ? max : min;
+ var tmin = (bbox.x - ray.x) * invDir.x;
+ bbox = signDirX ? min : max;
+ var tmax = (bbox.x - ray.x) * invDir.x;
+ bbox = signDirY ? min : max;
+ var tymin = (bbox.y - ray.y) * invDir.y;
+ bbox = signDirY ? min : max;
+ var tymax = (bbox.y - ray.y) * invDir.y;
+ if((tmin > tymax) || (tymin > tmax)){
+ return undefined;
+ }
+ if(tymin > tmin){
+ tmin = tymin;
+ }
+ if (tymax < tmax) {
+ tmax = tymax;
+ }
+ if ((tmin < maxDist) && (tmax > minDist)) {
+ return ray.getPointAtDistance(tmin);
+ }
+ return undefined;
+ },
+
+ intersectsRect: function(r){
+ return !(this.x > r.x + r.width || this.x + this.width < r.x || this.y > r.y + r.height || this.y + this.height < r.y);
+ },
+
+ scale: function(s){
+ var c = this.getCentroid();
+ this.width *= s;
+ this.height *= s;
+ this.x = c.x - this.width * 0.5;
+ this.y = c.y - this.height * 0.5;
+ return this;
+ },
+
+ set: function(x,y,width,height){
+ if(arguments.length ==- 1){
+ this.y = x.y;
+ this.width = x.width;
+ this.height = x.height;
+ this.x = x.x;
+ } else if(arguments.length === 4) {
+ this.x = x;
+ this.y = y;
+ this.width = width;
+ this.height = height;
+ } else {
+ throw new Error("toxi.Rect set() received wrong parameters");
+ }
+ },
+
+ setDimenions: function(dim){
+ this.width = dim.x;
+ this.height = dim.y;
+ return this;
+ },
+
+ setPosition: function(pos){
+ this.x = pos.x;
+ this.y = pos.y;
+ return this;
+ },
+
+ toPolygon2D: function(){
+ var poly = new toxi.Polygon2D();
+ poly.add(new toxi.Vec2D(this.x,this.y));
+ poly.add(new toxi.Vec2D(this.x+this.width,this.y));
+ poly.add(new toxi.Vec2D(this.x+this.width,this.y+this.height));
+ poly.add(new toxi.Vec2D(this.x,this.y+this.height));
+ return poly;
+ },
+
+ toString: function(){
+ return "rect: {x: "+this.x +", y: "+this.y+ ", width: "+this.width+ ", height: "+this.height+"}";
+ },
+
+ union: function(r){
+ var tmp = MathUtils.max(this.x + this.width, r.x + r.width);
+ this.x = MathUtils.min(this.x,r.x);
+ this.width = tmp - this.x;
+ tmp = MathUtils.max(this.y + this.height, r.y + r.height);
+ this.y = MathUtils.min(this.y,r.y);
+ this.height = tmp - this.y;
+ return this;
+ }
+};
+
+
+
+
@@ -0,0 +1,165 @@
+toxi.physics2d.VerletParticle2D = function(x,y,w){
+ if(x instanceof toxi.Vec2D){
+ if(x instanceof toxi.physics2d.VerletParticle2D){
+
+ y = x.y;
+ w = x.weight;
+ x = x.x;
+ this.isLocked = x.isLocked;
+
+ } else if(y === undefined){
+ y = x.y;
+ w = 1;
+ x = x.x;
+ } else {
+ w = y;
+ y = x.y;
+ x = x.x;
+ }
+ }
+ this.parent.init(x,y);
+ this.prev = new toxi.Vec2D(this);
+ this.temp = new toxi.Vec2D();
+ this.setWeight(w || 1);
+}
+
+toxi.extend(toxi.physics2d.VerletParticle2D,toxi.Vec2D);
+
+toxi.physics2d.VerletParticle2D.prototype.addBehavior = function(behavior,timeStep){
+ if(this.behaviors === undefined){
+ this.behaviors = [];
+ }
+ this.behaviors.configure(timeStep);
+ this.behaviors.push(behavior);
+ return this;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.addConstraint = function(c){
+ if(this.constraints === undefined){
+ this.constraints = [];
+ }
+ this.constraints.push(c);
+ return this;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.addForce = function(f){
+ this.force.addSelf(f);
+ return this;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.addVelocity = function(v){
+ this.prev.subSelf(v);
+ return this;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.applyBehaviors = function(){
+ if(this.behaviors !== undefined){
+ var i = 0;
+ for(i = 0;i<this.behaviors.length;i++){
+ this.behaviors[i].apply(this);
+ }
+ }
+};
+
+toxi.physics2d.VerletParticle2D.prototype.applyConstraints = function(){
+ if(this.constraints !== undefined){
+ var i =0;
+ for(i =0;i<this.constraints.length;i++){
+ this.constraints[i].apply(this);
+ }
+ }
+};
+
+//protected
+toxi.physics2d.VerletParticle2D.prototype.applyForce = function(){
+ this.temp.set(this);
+ this.addSelf(
+ this.sub(this.prev).addSelf(
+ this.force.scale(this.weigth)
+ )
+ );
+ this.prev.set(this.temp);
+ this.force.clear();
+};
+
+toxi.physics2d.VerletParticle2D.prototype.clearForce = function(){
+ this.force.clear();
+ return this;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.clearVelocity = function(){
+ this.prev.set(this);
+ return this;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.getInvWeight = function(){
+ return this.invWeight;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.getPreviousPosition = function(){
+ return this.prev;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.getVelocity = function(){
+ return this.sub(this.prev);
+};
+
+toxi.physics2d.VerletParticle2D.prototype.getWeight = function(){
+ return this.weight;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.isLocked = function(){
+ return this.isLocked;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.lock = function(){
+ this.isLoced = true;
+ return this;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.removeAllBehaviors = function(){
+ this.behaviors = [];
+ return this;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.removeAllConstraints = function(){
+ this.constraints = [];
+ return this;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.removeBehavior = function(b){
+ return toxi.physics2d.removeItemFrom(b,this.behaviors);
+};
+
+toxi.physics2d.VerletParticle2D.prototype.removeConstraint = function(c){
+ return toxi.physics2d.removeItemFrom(c,this.constraints);
+};
+
+toxi.physics2d.VerletParticle2D.prototype.scaleVelocity = function(scl){
+ this.prev.interpolateToSelf(this,1 - scl);
+ return this;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.setPreviousPosition = function(p){
+ this.prev.set(p);
+ return this;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.setWeight = function(w){
+ this.weight = w;
+ this.invWeight = (w !== 0) ? 1 / w : 0; //avoid divide by zero
+};
+
+toxi.physics2d.VerletParticle2D.prototype.unlock = function() {
+ this.clearVelocity();
+ this.isLocked = false;
+ return this;
+};
+
+toxi.physics2d.VerletParticle2D.prototype.update = function(){
+ if(!this.isLocked){
+ this.applyBehaviors();
+ this.applyForce();
+ this.applyConstraints();
+ }
+};
Oops, something went wrong.

0 comments on commit 1c6bec2

Please sign in to comment.