Pārlūkot izejas kodu

colliders: add optional matrix to inFrustum, implemented only on Sphere for now

trethaller 6 gadi atpakaļ
vecāks
revīzija
71bea1af33

+ 3 - 2
h3d/Matrix.hx

@@ -225,8 +225,9 @@ class Matrix {
 		_44 = vw;
 	}
 
-	public inline function getScale() {
-		var v = new Vector();
+	public inline function getScale(?v: h3d.Vector) {
+		if(v == null)
+			v = new Vector();
 		v.x = Math.sqrt(_11 * _11 + _12 * _12 + _13 * _13);
 		v.y = Math.sqrt(_21 * _21 + _22 * _22 + _23 * _23);
 		v.z = Math.sqrt(_31 * _31 + _32 * _32 + _33 * _33);

+ 3 - 1
h3d/col/Bounds.hx

@@ -18,7 +18,9 @@ class Bounds implements Collider {
 		empty();
 	}
 
-	public inline function inFrustum( f : Frustum ) {
+	public inline function inFrustum( f : Frustum, ?m: h3d.Matrix ) {
+		if( m != null )
+			throw "Not implemented";
 		return f.hasBounds(this);
 	}
 

+ 3 - 1
h3d/col/Capsule.hx

@@ -89,7 +89,9 @@ class Capsule implements Collider {
 		return -1;
 	}
 
-	public function inFrustum( f : Frustum ) {
+	public function inFrustum( f : Frustum, ?m : h3d.Matrix ) {
+		if( m != null )
+			throw "Not implemented";
 		tmpSphere.load(a.x + (b.x-a.x), a.y + (b.y-a.y), a.z + (b.z-a.z), (b.distance(a)/2 + r));
 		return tmpSphere.inFrustum(f);
 	}

+ 5 - 5
h3d/col/Collider.hx

@@ -8,7 +8,7 @@ interface Collider extends hxd.impl.Serializable.StructSerializable {
 	**/
 	public function rayIntersection( r : Ray, bestMatch : Bool ) : Float;
 	public function contains( p : Point ) : Bool;
-	public function inFrustum( f : Frustum ) : Bool;
+	public function inFrustum( f : Frustum, ?localMatrix : h3d.Matrix ) : Bool;
 	public function inSphere( s : Sphere ) : Bool;
 }
 
@@ -33,8 +33,8 @@ class OptimizedCollider implements hxd.impl.Serializable implements Collider {
 		return a.contains(p) && b.contains(p);
 	}
 
-	public function inFrustum( f : Frustum ) {
-		return a.inFrustum(f) && b.inFrustum(f);
+	public function inFrustum( f : Frustum, ?m : h3d.Matrix ) {
+		return a.inFrustum(f, m) && b.inFrustum(f, m);
 	}
 
 	public function inSphere( s : Sphere ) {
@@ -77,9 +77,9 @@ class GroupCollider implements Collider {
 		return false;
 	}
 
-	public function inFrustum( f : Frustum ) {
+	public function inFrustum( f : Frustum, ?m : h3d.Matrix) {
 		for( c in colliders )
-			if( c.inFrustum(f) )
+			if( c.inFrustum(f, m) )
 				return true;
 		return false;
 	}

+ 1 - 1
h3d/col/HeightMap.hx

@@ -32,7 +32,7 @@ class HeightMap implements Collider {
 		return getZ(pt.x, pt.y) > pt.z;
 	}
 
-	public function inFrustum( f : Frustum ) : Bool {
+	public function inFrustum( f : Frustum, ?m : h3d.Matrix) : Bool {
 		throw "Not implemented : is infinite";
 	}
 

+ 4 - 3
h3d/col/ObjectCollider.hx

@@ -33,9 +33,10 @@ class ObjectCollider implements Collider implements hxd.impl.Serializable {
 		return b;
 	}
 
-	public function inFrustum( f : Frustum ) {
-		throw "Not implemented";
-		return false;
+	public function inFrustum( f : Frustum, ?m : h3d.Matrix ) {
+		if( m != null )
+			throw "Not implemented";
+		return collider.inFrustum(f, obj.getAbsPos());
 	}
 
 	public function inSphere( s : Sphere ) {

+ 1 - 1
h3d/col/Point.hx

@@ -19,7 +19,7 @@ class Point {
 		z *= v;
 	}
 
-	public inline function inFrustum( f : Frustum ) {
+	public inline function inFrustum( f : Frustum, ?m : h3d.Matrix ) {
 		return f.hasPoint(this);
 	}
 

+ 2 - 2
h3d/col/Polygon.hx

@@ -100,7 +100,7 @@ class TriPlane implements Collider {
 		return nx * p.x + ny * p.y + nz * p.z - d >= 0;
 	}
 
-	public function inFrustum( f : Frustum ) {
+	public function inFrustum( f : Frustum, ?m : h3d.Matrix ) {
 		throw "Not implemented";
 		return false;
 	}
@@ -252,7 +252,7 @@ class Polygon implements Collider {
 		return best;
 	}
 
-	public function inFrustum( f : Frustum ) {
+	public function inFrustum( f : Frustum, ?m : h3d.Matrix ) {
 		throw "Not implemented";
 		return false;
 	}

+ 1 - 1
h3d/col/PolygonBuffer.hx

@@ -41,7 +41,7 @@ class PolygonBuffer implements Collider {
 		return true;
 	}
 
-	public function inFrustum( f : Frustum ) {
+	public function inFrustum( f : Frustum, ?m : h3d.Matrix ) {
 		throw "Not implemented";
 		return false;
 	}

+ 3 - 1
h3d/col/SkinCollider.hx

@@ -21,7 +21,9 @@ class SkinCollider implements hxd.impl.Serializable implements Collider {
 		return transform.contains(p);
 	}
 
-	public function inFrustum(p) {
+	public function inFrustum(p, ?m : h3d.Matrix ) {
+		if( m != null )
+			throw "Not implemented";
 		applyTransform();
 		return transform.inFrustum(p);
 	}

+ 18 - 1
h3d/col/Sphere.hx

@@ -47,10 +47,27 @@ class Sphere implements Collider {
 		return 1 - t;
 	}
 
-	public inline function inFrustum( f : Frustum ) {
+	public inline function inFrustum( f : Frustum, ?m : h3d.Matrix ) {
+		if( m != null ) return inFrustumMatrix(f,m);
 		return f.hasSphere(this);
 	}
 
+	var tmpVec = new h3d.Vector();
+	function inFrustumMatrix( f : Frustum, m : h3d.Matrix ) {
+		var oldX = x, oldY = y, oldZ = z, oldR = r;
+		m.getScale(tmpVec);
+		x += m.tx;
+		y += m.ty;
+		z += m.tz;
+		r *= Math.max(Math.max(tmpVec.x, tmpVec.y), tmpVec.z);
+		var res = f.hasSphere(this);
+		x = oldX;
+		y = oldY;
+		z = oldZ;
+		r = oldR;
+		return res;
+	}
+
 	public inline function inSphere( s : Sphere ) {
 		return new Point(x,y,z).distanceSq(new Point(s.x,s.y,s.z)) < (s.r + r)*(s.r + r);
 	}