SHARE
TWEET
Untitled
a guest
Jan 29th, 2018
50
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- class Ray{
- constructor(a,b){
- this.A = a;
- this.B = b;
- this.Slope = (this.B.Y - this.A.Y)/(this.B.Y-this.A.Y);
- this.Intercept = this.B.Y - this.Slope*this.B.Y
- }
- Intersects(ray){
- let x = (ray.Intercept - this.Intercept) / (this.Slope - ray.Slope);
- let y = this.Slope*x + this.Intercept;
- // Check X value for THIS Ray
- let xR = new Engine.DirectedRange(this.A.X,this.B.X);
- let xPct = xR.Percent(x);
- if(xPct < 0 || xPct > 1){
- return false;
- }
- // Check X value for OTHER Ray
- xR = new Engine.DirectedRange(ray.A.X,ray.B.X);
- xPct = xR.Percent(x);
- if(xPct < 0 || xPct > 1){
- return false;
- }
- // Check Y value for THIS Ray
- let yR = new Engine.DirectedRange(this.A.Y,this.B.Y);
- let yPct = yR.Percent(y);
- if(yPct < 0 || yPct > 1){
- return false;
- }
- // Check Y value for OTHER Ray
- yR = new Engine.DirectedRange(ray.A.Y,ray.B.Y);
- yPct = yR.Percent(y);
- if(yPct < 0 || yPct > 1){
- return false;
- }
- return true;
- }
- }
- get Rays(){
- if(this._rays == null){
- this._rays = [];
- for(let i = 0; i < 4; i++){
- let a = this.Corners[i];
- let b = this.Corners[(i+1)%4];
- let ray = new Ray(a,b);
- this._rays.push(ray);
- }
- }
- return this._rays;
- }
- CollidesExact(other){
- for(let corner of other.Corners){
- if(this.ContainsPointExact(corner)){
- return true;
- }
- }
- for(let corner of this.Corners){
- if(other.ContainsPointExact(corner)){
- return true;
- }
- }
- return false;
- }
- ContainsPointExact(pt){
- let rayA = new Engine.Vector2(-100000,-100000);
- let rayCast = new Ray(rayA,pt);
- for(let ray of this.Rays){
- if(rayCast.Intersects(ray)){
- return true;
- }
- }
- return false;
- }
RAW Paste Data

