Browse Source

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

Nicolas Cannasse 2 years ago
parent
commit
b36458ead5

+ 9 - 1
h2d/col/Bounds.hx

@@ -7,7 +7,7 @@ import hxd.Math;
 	@see `Object.getBounds`
 	@see `Object.getBounds`
 	@see `Object.getSize`
 	@see `Object.getSize`
 **/
 **/
-class Bounds implements Collider {
+class Bounds extends Collider {
 
 
 	/** X-axis left-most bounding box point. **/
 	/** X-axis left-most bounding box point. **/
 	public var xMin : Float;
 	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);
 		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.
 		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.
 	The circular hitbox implementation of a 2D Collider.
 **/
 **/
-class Circle implements Collider {
+class Circle extends Collider {
 
 
 	/**
 	/**
 		Horizontal position of the Circle center.
 		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.
 	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.
 		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.
 	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.
 		The Polygons instance used for collision checks.
 	**/
 	**/
@@ -30,4 +30,12 @@ class PolygonCollider implements Collider {
 		return polygons.contains(p, isConvex);
 		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;
 package h3d.col;
 import hxd.Math;
 import hxd.Math;
 
 
-class Bounds implements Collider {
+class Bounds extends Collider {
 
 
 	public var xMin : Float;
 	public var xMin : Float;
 	public var xMax : Float;
 	public var xMax : Float;

+ 1 - 1
h3d/col/Capsule.hx

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

+ 8 - 8
h3d/col/Collider.hx

@@ -1,23 +1,23 @@
 package h3d.col;
 package h3d.col;
 
 
-interface Collider {
+abstract class Collider {
 
 
 	/**
 	/**
 		Returns the distance of intersection between the ray and the collider, or negative if no collision.
 		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.
 		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
 	#if !macro
-	public function makeDebugObj() : h3d.scene.Object;
+	public abstract function makeDebugObj() : h3d.scene.Object;
 	#end
 	#end
 }
 }
 
 
 
 
-class OptimizedCollider implements Collider {
+class OptimizedCollider extends Collider {
 
 
 	public var a : Collider;
 	public var a : Collider;
 	public var b : 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>;
 	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 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.
 	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.
 		When performing raycast check, tells by how much step we advance.

+ 1 - 1
h3d/col/ObjectCollider.hx

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

+ 2 - 2
h3d/col/Polygon.hx

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

+ 1 - 1
h3d/col/PolygonBuffer.hx

@@ -1,6 +1,6 @@
 package h3d.col;
 package h3d.col;
 
 
-class PolygonBuffer implements Collider {
+class PolygonBuffer extends Collider {
 
 
 	var buffer : haxe.ds.Vector<hxd.impl.Float32>;
 	var buffer : haxe.ds.Vector<hxd.impl.Float32>;
 	var indexes : haxe.ds.Vector<Int>;
 	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.col.PolygonBuffer)
 @:access(h3d.scene.Skin)
 @:access(h3d.scene.Skin)
-class SkinCollider implements Collider {
+class SkinCollider extends Collider {
 
 
 	var obj : h3d.scene.Skin;
 	var obj : h3d.scene.Skin;
 	var col : PolygonBuffer;
 	var col : PolygonBuffer;

+ 1 - 1
h3d/col/Sphere.hx

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

+ 1 - 1
h3d/col/TransformCollider.hx

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