home *** CD-ROM | disk | FTP | other *** search
- class org.cove.flade.surfaces.LineSurface extends org.cove.flade.surfaces.AbstractTile implements org.cove.flade.surfaces.Surface
- {
- var p1;
- var p2;
- var collNormal;
- var isVisible;
- var dmc;
- var faceNormal;
- var collisionDepth;
- var minY;
- var maxY;
- var minX;
- var maxX;
- var sideNormal;
- var minS;
- var maxS;
- var minF;
- var maxF;
- var rise;
- var run;
- var sign;
- var slope;
- var invB;
- var p3;
- var p4;
- var verts;
- function LineSurface(p1x, p1y, p2x, p2y, rootmc)
- {
- super(0,0,rootmc);
- this.p1 = new org.cove.flade.util.Vector(p1x,p1y);
- this.p2 = new org.cove.flade.util.Vector(p2x,p2y);
- this.calcFaceNormal();
- this.collNormal = new org.cove.flade.util.Vector(0,0);
- this.setCollisionDepth(60);
- }
- function clear()
- {
- this.p1 = new org.cove.flade.util.Vector(0,0);
- this.p2 = new org.cove.flade.util.Vector(1,1);
- this.calcFaceNormal();
- this.collNormal = new org.cove.flade.util.Vector(0,0);
- this.setCollisionDepth(30);
- this.paint();
- }
- function paint()
- {
- if(this.isVisible)
- {
- this.dmc.clear();
- this.dmc.lineStyle(0,2237064,100);
- org.cove.flade.graphics.Graphics.paintLine(this.dmc,this.p1.x,this.p1.y,this.p2.x,this.p2.y);
- }
- }
- function resolveCircleCollision(p, sysObj)
- {
- if(this.isCircleColliding(p))
- {
- this.onContact();
- p.resolveCollision(this.faceNormal,sysObj);
- p.SetCollision();
- }
- }
- function resolveRectangleCollision(p, sysObj)
- {
- if(this.isRectangleColliding(p))
- {
- this.onContact();
- p.resolveCollision(this.collNormal,sysObj);
- p.SetCollision();
- }
- }
- function setCollisionDepth(d)
- {
- this.collisionDepth = d;
- this.precalculate();
- }
- function isCircleColliding(p)
- {
- this.findClosestPoint(p.curr,p.closestPoint);
- var _loc3_ = p.closestPoint.minusNew(p.curr);
- _loc3_.normalize();
- if(this.inequality(p.curr))
- {
- var _loc5_ = Math.abs(_loc3_.x);
- _loc3_.x = this.faceNormal.x >= 0 ? - _loc5_ : _loc5_;
- _loc3_.y = Math.abs(_loc3_.y);
- }
- var _loc4_ = p.curr.plusNew(_loc3_.mult(p.radius));
- if(this.segmentInequality(_loc4_))
- {
- if(_loc4_.distance(p.closestPoint) > this.collisionDepth)
- {
- return false;
- }
- var _loc7_ = _loc4_.x - p.closestPoint.x;
- var _loc6_ = _loc4_.y - p.closestPoint.y;
- p.mtd.setTo(- _loc7_,- _loc6_);
- return true;
- }
- return false;
- }
- function isRectangleColliding(p)
- {
- p.getCardYProjection();
- var _loc7_ = this.testIntervals(p.bmin,p.bmax,this.minY,this.maxY);
- if(_loc7_ == 0)
- {
- return false;
- }
- p.getCardXProjection();
- var _loc8_ = this.testIntervals(p.bmin,p.bmax,this.minX,this.maxX);
- if(_loc8_ == 0)
- {
- return false;
- }
- p.getAxisProjection(this.sideNormal);
- var _loc10_ = this.testIntervals(p.bmin,p.bmax,this.minS,this.maxS);
- if(_loc10_ == 0)
- {
- return false;
- }
- p.getAxisProjection(this.faceNormal);
- var _loc9_ = this.testIntervals(p.bmin,p.bmax,this.minF,this.maxF);
- if(_loc9_ == 0)
- {
- return false;
- }
- var _loc4_ = Math.abs(_loc8_);
- var _loc3_ = Math.abs(_loc7_);
- var _loc6_ = Math.abs(_loc10_);
- var _loc5_ = Math.abs(_loc9_);
- if(_loc4_ <= _loc3_ && _loc4_ <= _loc6_ && _loc4_ <= _loc5_)
- {
- p.mtd.setTo(_loc8_,0);
- this.collNormal.setTo(p.mtd.x / _loc4_,0);
- }
- else if(_loc3_ <= _loc4_ && _loc3_ <= _loc6_ && _loc3_ <= _loc5_)
- {
- p.mtd.setTo(0,_loc7_);
- this.collNormal.setTo(0,p.mtd.y / _loc3_);
- }
- else if(_loc5_ <= _loc4_ && _loc5_ <= _loc3_ && _loc5_ <= _loc6_)
- {
- p.mtd = this.faceNormal.multNew(_loc9_);
- this.collNormal.copy(this.faceNormal);
- }
- else if(_loc6_ <= _loc4_ && _loc6_ <= _loc3_ && _loc6_ <= _loc5_)
- {
- p.mtd = this.sideNormal.multNew(_loc10_);
- this.collNormal.copy(this.sideNormal);
- }
- return true;
- }
- function precalculate()
- {
- this.rise = this.p2.y - this.p1.y;
- this.run = this.p2.x - this.p1.x;
- this.sign = this.run < 0 ? -1 : 1;
- this.slope = this.rise / this.run;
- this.invB = 1 / (this.run * this.run + this.rise * this.rise);
- this.createRectangle();
- this.calcSideNormal();
- this.setCardProjections();
- this.setAxisProjections();
- }
- function calcFaceNormal()
- {
- this.faceNormal = new org.cove.flade.util.Vector(0,0);
- var _loc3_ = this.p2.x - this.p1.x;
- var _loc2_ = this.p2.y - this.p1.y;
- this.faceNormal.setTo(_loc2_,- _loc3_);
- this.faceNormal.normalize();
- }
- function segmentInequality(toPoint)
- {
- var _loc2_ = this.findU(toPoint);
- var _loc3_ = this.inequality(toPoint);
- return _loc2_ >= 0 && _loc2_ <= 1 && _loc3_;
- }
- function inequality(toPoint)
- {
- var _loc2_ = (this.slope * (toPoint.x - this.p1.x) + (this.p1.y - toPoint.y)) * this.sign;
- return _loc2_ <= 0;
- }
- function findClosestPoint(toPoint, returnVect)
- {
- var _loc2_ = this.findU(toPoint);
- if(_loc2_ <= 0)
- {
- returnVect.copy(this.p1);
- return undefined;
- }
- if(_loc2_ >= 1)
- {
- returnVect.copy(this.p2);
- return undefined;
- }
- var _loc5_ = this.p1.x + _loc2_ * (this.p2.x - this.p1.x);
- var _loc4_ = this.p1.y + _loc2_ * (this.p2.y - this.p1.y);
- returnVect.setTo(_loc5_,_loc4_);
- }
- function findU(p)
- {
- var _loc2_ = (p.x - this.p1.x) * this.run + (p.y - this.p1.y) * this.rise;
- return _loc2_ * this.invB;
- }
- function createRectangle()
- {
- var _loc5_ = this.p2.x + (- this.faceNormal.x) * this.collisionDepth;
- var _loc3_ = this.p2.y + (- this.faceNormal.y) * this.collisionDepth;
- var _loc4_ = this.p1.x + (- this.faceNormal.x) * this.collisionDepth;
- var _loc2_ = this.p1.y + (- this.faceNormal.y) * this.collisionDepth;
- this.p3 = new org.cove.flade.util.Vector(_loc5_,_loc3_);
- this.p4 = new org.cove.flade.util.Vector(_loc4_,_loc2_);
- this.verts.push(this.p1);
- this.verts.push(this.p2);
- this.verts.push(this.p3);
- this.verts.push(this.p4);
- }
- function setAxisProjections()
- {
- var _loc2_ = undefined;
- this.minF = this.p2.dot(this.faceNormal);
- this.maxF = this.p3.dot(this.faceNormal);
- if(this.minF > this.maxF)
- {
- _loc2_ = this.minF;
- this.minF = this.maxF;
- this.maxF = _loc2_;
- }
- this.minS = this.p1.dot(this.sideNormal);
- this.maxS = this.p2.dot(this.sideNormal);
- if(this.minS > this.maxS)
- {
- _loc2_ = this.minS;
- this.minS = this.maxS;
- this.maxS = _loc2_;
- }
- }
- function calcSideNormal()
- {
- this.sideNormal = new org.cove.flade.util.Vector(0,0);
- var _loc3_ = this.p3.x - this.p2.x;
- var _loc2_ = this.p3.y - this.p2.y;
- this.sideNormal.setTo(_loc2_,- _loc3_);
- this.sideNormal.normalize();
- }
- }
-