Explorar o código

collider is now a class instead of an interface (less allocs, faster calls)

Nicolas Cannasse %!s(int64=2) %!d(string=hai) anos
pai
achega
b36458ead5

+ 9 - 1
h2d/col/Bounds.hx

@@ -7,7 +7,7 @@ import hxd.Math;
 	@see `Object.getBounds`
 	@see `Object.getSize`
 **/
-class Bounds implements Collider {
+class Bounds extends Collider {
 
 	/** X-axis left-most bounding box point. **/
 	public var xMin : Float;
@@ -59,6 +59,14 @@ class Bounds implements Collider {
 		return !(xMin > b.xMax || yMin > b.yMax || xMax < b.xMin || yMax < b.yMin);
 	}
 
+	public inline function collideBounds( b : Bounds ) : Bool {
+		return intersects(b);
+	}
+
+	public inline function collideCircle( c : Circle ) : Bool {
+		return c.collideBounds(this);
+	}
+
 	/**
 		Tests if the Point `p` is inside the bounding box.
 	**/

+ 1 - 1
h2d/col/Circle.hx

@@ -4,7 +4,7 @@ import hxd.Math;
 /**
 	The circular hitbox implementation of a 2D Collider.
 **/
-class Circle implements Collider {
+class Circle extends Collider {
 
 	/**
 		Horizontal position of the Circle center.

+ 4 - 2
h2d/col/Collider.hx

@@ -3,11 +3,13 @@ package h2d.col;
 /**
 	A common interface for 2D Shapes to hit-test again the mouse or a specific point in space.
 **/
-interface Collider /* extends hxd.impl.Serializable.StructSerializable */ {
+abstract class Collider {
 
 	/**
 		Tests if Point `p` is inside the Collider.
 	**/
-	public function contains( p : Point ) : Bool;
+	public abstract function contains( p : Point ) : Bool;
+	public abstract function collideCircle( c : Circle ) : Bool;
+	public abstract function collideBounds( b : Bounds ) : Bool;
 
 }

+ 9 - 1
h2d/col/PolygonCollider.hx

@@ -3,7 +3,7 @@ package h2d.col;
 /**
 	A `Collider` wrapper around `Polygons` to enable using those for hit-testing testing.
 **/
-class PolygonCollider implements Collider {
+class PolygonCollider extends Collider {
 	/**
 		The Polygons instance used for collision checks.
 	**/
@@ -30,4 +30,12 @@ class PolygonCollider implements Collider {
 		return polygons.contains(p, isConvex);
 	}
 
+	public function collideCircle( c : Circle ) : Bool {
+		throw "Not implemented";
+	}
+
+	public function collideBounds( b : Bounds ) : Bool {
+		throw "Not implemented";
+	}
+
 }

+ 1 - 1
h3d/col/Bounds.hx

@@ -1,7 +1,7 @@
 package h3d.col;
 import hxd.Math;
 
-class Bounds implements Collider {
+class Bounds extends Collider {
 
 	public var xMin : Float;
 	public var xMax : Float;

+ 1 - 1
h3d/col/Capsule.hx

@@ -1,6 +1,6 @@
 package h3d.col;
 
-class Capsule implements Collider {
+class Capsule extends Collider {
 
 	public var a : Point;
 	public var b : Point;

+ 8 - 8
h3d/col/Collider.hx

@@ -1,23 +1,23 @@
 package h3d.col;
 
-interface Collider {
+abstract class Collider {
 
 	/**
 		Returns the distance of intersection between the ray and the collider, or negative if no collision.
 		If bestMatch is false, only negative/positive value needs to be returned, with no additional precision.
 	**/
-	public function rayIntersection( r : Ray, bestMatch : Bool ) : Float;
-	public function contains( p : Point ) : Bool;
-	public function inFrustum( f : Frustum, ?localMatrix : h3d.Matrix ) : Bool;
-	public function inSphere( s : Sphere ) : Bool;
+	public abstract function rayIntersection( r : Ray, bestMatch : Bool ) : Float;
+	public abstract function contains( p : Point ) : Bool;
+	public abstract function inFrustum( f : Frustum, ?localMatrix : h3d.Matrix ) : Bool;
+	public abstract function inSphere( s : Sphere ) : Bool;
 
 	#if !macro
-	public function makeDebugObj() : h3d.scene.Object;
+	public abstract function makeDebugObj() : h3d.scene.Object;
 	#end
 }
 
 
-class OptimizedCollider implements Collider {
+class OptimizedCollider extends Collider {
 
 	public var a : Collider;
 	public var b : Collider;
@@ -62,7 +62,7 @@ class OptimizedCollider implements Collider {
 
 }
 
-class GroupCollider implements Collider {
+class GroupCollider extends Collider {
 
 	public var colliders : Array<Collider>;
 

+ 1 - 1
h3d/col/HeightMap.hx

@@ -5,7 +5,7 @@ package h3d.col;
 	In order to use, you need to extends this class and override the getZ method
 	in order to return appropriate Z value based on X and Y coordinates.
 **/
-class HeightMap implements Collider {
+class HeightMap extends Collider {
 
 	/**
 		When performing raycast check, tells by how much step we advance.

+ 1 - 1
h3d/col/ObjectCollider.hx

@@ -1,6 +1,6 @@
 package h3d.col;
 
-class ObjectCollider implements Collider {
+class ObjectCollider extends Collider {
 
 	public var obj : h3d.scene.Object;
 	public var collider : Collider;

+ 2 - 2
h3d/col/Polygon.hx

@@ -1,7 +1,7 @@
 package h3d.col;
 
 @:allow(h3d.col.Polygon)
-class TriPlane implements Collider {
+class TriPlane extends Collider {
 
 	public var next : TriPlane = null;
 
@@ -159,7 +159,7 @@ class TriPlane implements Collider {
 }
 
 
-class Polygon implements Collider {
+class Polygon extends Collider {
 
 	var triPlanes : TriPlane;
 

+ 1 - 1
h3d/col/PolygonBuffer.hx

@@ -1,6 +1,6 @@
 package h3d.col;
 
-class PolygonBuffer implements Collider {
+class PolygonBuffer extends Collider {
 
 	var buffer : haxe.ds.Vector<hxd.impl.Float32>;
 	var indexes : haxe.ds.Vector<Int>;

+ 1 - 1
h3d/col/SkinCollider.hx

@@ -2,7 +2,7 @@ package h3d.col;
 
 @:access(h3d.col.PolygonBuffer)
 @:access(h3d.scene.Skin)
-class SkinCollider implements Collider {
+class SkinCollider extends Collider {
 
 	var obj : h3d.scene.Skin;
 	var col : PolygonBuffer;

+ 1 - 1
h3d/col/Sphere.hx

@@ -1,6 +1,6 @@
 package h3d.col;
 
-class Sphere implements Collider {
+class Sphere extends Collider {
 
 	public var x : Float;
 	public var y : Float;

+ 1 - 1
h3d/col/TransformCollider.hx

@@ -1,6 +1,6 @@
 package h3d.col;
 
-class TransformCollider implements Collider {
+class TransformCollider extends Collider {
 
 	public var collider : Collider;
 	public var mat(default, set) : h3d.Matrix;