daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Ray{
  2.    constructor(a,b){
  3.         this.A = a;
  4.         this.B = b;
  5.         this.Slope = (this.B.Y - this.A.Y)/(this.B.Y-this.A.Y);
  6.         this.Intercept = this.B.Y - this.Slope*this.B.Y
  7.    }
  8.    Intersects(ray){
  9.       let x = (ray.Intercept - this.Intercept) / (this.Slope - ray.Slope);
  10.       let y = this.Slope*x + this.Intercept;
  11.      
  12.       // Check X value for THIS Ray
  13.       let xR = new Engine.DirectedRange(this.A.X,this.B.X);
  14.       let xPct = xR.Percent(x);
  15.       if(xPct < 0 || xPct > 1){
  16.         return false;
  17.       }
  18.      
  19.       // Check X value for OTHER Ray
  20.       xR = new Engine.DirectedRange(ray.A.X,ray.B.X);
  21.       xPct = xR.Percent(x);
  22.       if(xPct < 0 || xPct > 1){
  23.         return false;
  24.       }
  25.      
  26.       // Check Y value for THIS Ray
  27.       let yR = new Engine.DirectedRange(this.A.Y,this.B.Y);
  28.       let yPct = yR.Percent(y);
  29.       if(yPct < 0 || yPct > 1){
  30.         return false;
  31.       }
  32.      
  33.       // Check Y value for OTHER Ray
  34.       yR = new Engine.DirectedRange(ray.A.Y,ray.B.Y);
  35.       yPct = yR.Percent(y);
  36.       if(yPct < 0 || yPct > 1){
  37.         return false;
  38.       }
  39.       return true;
  40.    }
  41. }
  42. get Rays(){
  43.    if(this._rays == null){
  44.        this._rays = [];
  45.        for(let i = 0; i < 4; i++){
  46.           let a = this.Corners[i];
  47.           let b = this.Corners[(i+1)%4];
  48.           let ray = new Ray(a,b);
  49.           this._rays.push(ray);
  50.        }      
  51.    }
  52.    return this._rays;
  53. }
  54.  
  55. CollidesExact(other){
  56.      for(let corner of other.Corners){
  57.         if(this.ContainsPointExact(corner)){
  58.            return true;
  59.         }
  60.      }
  61.      for(let corner of this.Corners){
  62.         if(other.ContainsPointExact(corner)){
  63.            return true;
  64.         }
  65.      }
  66.      return false;
  67. }
  68. ContainsPointExact(pt){
  69.    let rayA = new Engine.Vector2(-100000,-100000);
  70.    let rayCast = new Ray(rayA,pt);
  71.    for(let ray of this.Rays){
  72.       if(rayCast.Intersects(ray)){
  73.          return true;
  74.       }
  75.    }
  76.    return false;
  77. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top