|
@@ -2,7 +2,10 @@ package h3d.col;
|
|
|
|
|
|
interface Collider {
|
|
|
|
|
|
- public function rayIntersection( r : Ray, ?p : Point ) : Null<Point>;
|
|
|
+ /**
|
|
|
+ Returns the distance of intersection between the ray and the collider, or negative if no collision.
|
|
|
+ **/
|
|
|
+ public function rayIntersection( r : Ray, bestMatch : Bool ) : Float;
|
|
|
public function contains( p : Point ) : Bool;
|
|
|
public function inFrustum( mvp : h3d.Matrix ) : Bool;
|
|
|
|
|
@@ -19,18 +22,54 @@ class OptimizedCollider implements Collider {
|
|
|
this.b = b;
|
|
|
}
|
|
|
|
|
|
- public function rayIntersection( r : Ray, ?p : Point ) : Null<Point> {
|
|
|
- if( a.rayIntersection(r, p) == null )
|
|
|
- return null;
|
|
|
- return b.rayIntersection(r, p);
|
|
|
+ public function rayIntersection( r : Ray, bestMatch : Bool ) : Float {
|
|
|
+ if( a.rayIntersection(r, bestMatch) < 0 )
|
|
|
+ return -1;
|
|
|
+ return b.rayIntersection(r, bestMatch);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public function contains( p : Point ) {
|
|
|
return a.contains(p) && b.contains(p);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public function inFrustum( mvp : h3d.Matrix ) {
|
|
|
return a.inFrustum(mvp) && b.inFrustum(mvp);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+class GroupCollider implements Collider {
|
|
|
+
|
|
|
+ public var colliders : Array<Collider>;
|
|
|
+
|
|
|
+ public function new(colliders) {
|
|
|
+ this.colliders = colliders;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function rayIntersection( r : Ray, bestMatch : Bool ) : Float {
|
|
|
+ var best = -1.;
|
|
|
+ for( c in colliders ) {
|
|
|
+ var d = c.rayIntersection(r, bestMatch);
|
|
|
+ if( d >= 0 ) {
|
|
|
+ if( !bestMatch ) return d;
|
|
|
+ if( best < 0 || d < best ) best = d;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return best;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function contains( p : Point ) {
|
|
|
+ for( c in colliders )
|
|
|
+ if( c.contains(p) )
|
|
|
+ return true;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function inFrustum( mvp : h3d.Matrix ) {
|
|
|
+ for( c in colliders )
|
|
|
+ if( c.inFrustum(mvp) )
|
|
|
+ return true;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|