|
| 1 | +package differ; |
| 2 | + |
| 3 | +import differ.math.*; |
| 4 | +import differ.shapes.*; |
| 5 | +import differ.data.*; |
| 6 | + |
| 7 | +class Collision { |
| 8 | + |
| 9 | + /** Test a single shape against another shape. |
| 10 | + When no collision is found between them, this function returns null. |
| 11 | + Returns a `ShapeCollision` if a collision is found. */ |
| 12 | + public static function shapeWithShape( shape1:Shape, shape2:Shape ) : ShapeCollision { |
| 13 | + |
| 14 | + return shape1.test(shape2); |
| 15 | + |
| 16 | + } //test |
| 17 | + |
| 18 | + /** Test a single shape against multiple other shapes. |
| 19 | + When no collision is found, this function returns an empty array, this function will never return null. |
| 20 | + Returns a list of `ShapeCollision` information for each collision found. */ |
| 21 | + public static function shapeWithShapes( shape1:Shape, shapes:Array<Shape> ) : Array<ShapeCollision> { |
| 22 | + |
| 23 | + var results = []; |
| 24 | + |
| 25 | + //:todo: pair wise |
| 26 | + for(other_shape in shapes) { |
| 27 | + |
| 28 | + var result = shapeWithShape(shape1, other_shape); |
| 29 | + if(result != null) { |
| 30 | + results.push(result); |
| 31 | + } |
| 32 | + |
| 33 | + } //for all shapes passed in |
| 34 | + |
| 35 | + return results; |
| 36 | + |
| 37 | + } //testShapes |
| 38 | + |
| 39 | + /** Test a line between two points against a list of shapes. |
| 40 | + When no collision is found, this function returns null. |
| 41 | + Returns a `RayCollision` if a collision is found. */ |
| 42 | + public static function rayWithShape( ray:Ray, shape:Shape ) : RayCollision { |
| 43 | + |
| 44 | + return shape.testRay(ray); |
| 45 | + |
| 46 | + } //rayShape |
| 47 | + |
| 48 | + /** Test a ray between two points against a list of shapes. |
| 49 | + When no collision is found, this function returns an empty array, this function will never return null. |
| 50 | + Returns a list of `RayCollision` information for each collision found. */ |
| 51 | + public static function rayWithShapes( ray:Ray, shapes:Array<Shape> ) : Array<RayCollision> { |
| 52 | + |
| 53 | + var results = []; |
| 54 | + |
| 55 | + for(shape in shapes) { |
| 56 | + var result = shape.testRay(ray); |
| 57 | + if(result != null) { |
| 58 | + results.push(result); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return results; |
| 63 | + |
| 64 | + } //rayShapes |
| 65 | + |
| 66 | + /** Test a ray against another ray. |
| 67 | + When no collision is found, this function returns null. |
| 68 | + Returns a `RayIntersection` if a collision is found. */ |
| 69 | + public static function rayWithRay( ray1:Ray, ray2:Ray ) : RayIntersection { |
| 70 | + |
| 71 | + return Collision2D.testRayVsRay(ray1, ray2); |
| 72 | + |
| 73 | + } //rayRay |
| 74 | + |
| 75 | + /** Test a ray against a list of other rays. |
| 76 | + When no collision is found, this function returns an empty array, this function will never return null. |
| 77 | + Returns a list of `RayIntersection` information for each collision found. */ |
| 78 | + public static function rayWithRays( ray:Ray, rays:Array<Ray> ) : Array<RayIntersection> { |
| 79 | + |
| 80 | + var results = []; |
| 81 | + |
| 82 | + for(other in rays) { |
| 83 | + var result = Collision2D.testRayVsRay(ray, other); |
| 84 | + if(result != null) { |
| 85 | + results.push(result); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return results; |
| 90 | + |
| 91 | + } //rayRays |
| 92 | + |
| 93 | + /** Test if a given point lands inside the given polygon. |
| 94 | + Returns true if it does, false otherwise. */ |
| 95 | + public static function pointInPoly( point:Vector, poly:Polygon ) : Bool { |
| 96 | + |
| 97 | + var sides:Int = poly.transformedVertices.length; //amount of sides the polygon has |
| 98 | + var i:Int = 0; |
| 99 | + var j:Int = sides - 1; |
| 100 | + var oddNodes:Bool = false; |
| 101 | + |
| 102 | + for(i in 0 ... sides) { |
| 103 | + |
| 104 | + if( (poly.transformedVertices[i].y < point.y && poly.transformedVertices[j].y >= point.y) || |
| 105 | + (poly.transformedVertices[j].y < point.y && poly.transformedVertices[i].y >= point.y)) |
| 106 | + { |
| 107 | + if( poly.transformedVertices[i].x + |
| 108 | + (point.y - poly.transformedVertices[i].y) / |
| 109 | + (poly.transformedVertices[j].y - poly.transformedVertices[i].y) * |
| 110 | + (poly.transformedVertices[j].x - poly.transformedVertices[i].x) < point.x) |
| 111 | + { |
| 112 | + oddNodes = !oddNodes; |
| 113 | + } //second if |
| 114 | + |
| 115 | + } //first if |
| 116 | + |
| 117 | + j = i; |
| 118 | + |
| 119 | + } //for each side |
| 120 | + |
| 121 | + return oddNodes; |
| 122 | + |
| 123 | + } //pointInPoly |
| 124 | + |
| 125 | + |
| 126 | +} //Collision |
0 commit comments