浏览代码

Fixed inFrustum for OBB

TothBenoit 6 月之前
父节点
当前提交
6500157361
共有 2 个文件被更改,包括 38 次插入7 次删除
  1. 17 0
      h3d/col/Frustum.hx
  2. 21 7
      h3d/col/OrientedBounds.hx

+ 17 - 0
h3d/col/Frustum.hx

@@ -131,4 +131,21 @@ class Frustum {
 		return true;
 	}
 
+	public function hasOrientedBounds( b : OrientedBounds ) @:privateAccess {
+		if( b.testPlane(pleft) < 0 )
+			return false;
+		if( b.testPlane(pright) < 0 )
+			return false;
+		if( b.testPlane(ptop) < 0 )
+			return false;
+		if( b.testPlane(pbottom) < 0 )
+			return false;
+		if ( checkNearFar ) {
+			if( b.testPlane(pnear) < 0 )
+				return false;
+			if( b.testPlane(pfar) < 0 )
+				return false;
+		}
+		return true;
+	}
 }

+ 21 - 7
h3d/col/OrientedBounds.hx

@@ -280,12 +280,10 @@ class OrientedBounds extends Collider {
 		);
 	}
 
-	public function inFrustum(f:Frustum, ?localMatrix:Matrix):Bool {
-		for (i in 0...8) {
-			var v = inline getVertice(i);
-			if (f.hasPoint(v)) return true;
-		}
-		return false;
+	public function inFrustum(f:Frustum, ?m:Matrix) : Bool {
+		if( m != null )
+			throw "Not implemented";
+		return f.hasOrientedBounds(this);
 	}
 
 	public function inSphere(s:Sphere):Bool {
@@ -332,7 +330,22 @@ class OrientedBounds extends Collider {
 		return new Point();
 	}
 
-	public function makeDebugObj():h3d.scene.Object {
+	inline function testPlane( p : Plane ) {
+		var n = new h3d.Vector(p.nx, p.ny, p.nz);
+		var ax = new h3d.Vector(xx, xy, xz);
+		var ay = new h3d.Vector(yx, yy, yz);
+		var az = new h3d.Vector(zx, zy, zz);
+		var c = new h3d.Vector(centerX, centerY, centerZ);
+
+		var r = hx * hxd.Math.abs(n.dot(ax)) +
+				hy * hxd.Math.abs(n.dot(ay)) +
+				hz * hxd.Math.abs(n.dot(az));
+		var s = n.dot(c) - p.d;
+		return s + r;
+	}
+
+	#if !macro
+	public function makeDebugObj() : h3d.scene.Graphics {
 		var g = new h3d.scene.Graphics();
 
 		var verts : Array<Vector> = getVertices();
@@ -354,4 +367,5 @@ class OrientedBounds extends Collider {
 
 		return g;
 	}
+	#end
 }