Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Patch to add Ray.group and Ray.sensor to allow rayCast filtering.
diff --git a/nape/cx-src/nape/dynamics/Collide.cx b/nape/cx-src/nape/dynamics/Collide.cx
index a9159c6..713de8c 100644
--- a/nape/cx-src/nape/dynamics/Collide.cx
+++ b/nape/cx-src/nape/dynamics/Collide.cx
@@ -395,22 +395,28 @@ class RayCast {
static public inline var FAIL:Float = 10.0; //>1 for easy comparisons
static public inline function rayCircle(r:Ray,c:Circle) {
- vec_new(ac); vec_sub(r.a,c.centre.p,ac);
- var A = vec_lsq(r.v);
- var B = 2*vec_dot(r.v,ac);
- var C = vec_lsq(ac) - c.r*c.r;
-
- var D = B*B-4*A*C;
- if(D>=0) {
- D = FastMath.sqrt(D);
- A = 1/(2*A);
-
- var t = (-B-D)*A;
- if(t<0) t += 2*A*D;
- if(t<0 || t>1) return RayCast.FAIL;
- else return t;
- }else
+ if (r.group!=0 && (r.group&c.group)==0)
+ return RayCast.FAIL;
+ else if (r.sensor!=0 && (r.sensor&c.sensor)==0)
return RayCast.FAIL;
+ else {
+ vec_new(ac); vec_sub(r.a,c.centre.p,ac);
+ var A = vec_lsq(r.v);
+ var B = 2*vec_dot(r.v,ac);
+ var C = vec_lsq(ac) - c.r*c.r;
+
+ var D = B*B-4*A*C;
+ if(D>=0) {
+ D = FastMath.sqrt(D);
+ A = 1/(2*A);
+
+ var t = (-B-D)*A;
+ if(t<0) t += 2*A*D;
+ if(t<0 || t>1) return RayCast.FAIL;
+ else return t;
+ }else
+ return RayCast.FAIL;
+ }
}
$(mixin raySegment(r,p0,p1) {
@@ -432,38 +438,44 @@ class RayCast {
});
static private inline function rayPoly(r:Ray,p:Polygon,n:Vec2) {
- var ret = RayCast.FAIL;
-
- var pre:Vec2 = null;
- var ite = p.paxi.begin();
- var pax = p.paxi.front();
- var rneg:Bool = false;
- CxIter(cur,p.pverts,{
- if(pre!=null) {
- var neg:Bool;
- var t; raySegment(r,pre,cur);
- if(t<ret) {
- ret = t;
- vec_set(n.p,pax.n);
- rneg = neg;
+ if (r.group!=0 && (r.group&p.group)==0)
+ return RayCast.FAIL;
+ else if (r.sensor!=0 && (r.sensor&p.sensor)==0)
+ return RayCast.FAIL;
+ else {
+ var ret = RayCast.FAIL;
+
+ var pre:Vec2 = null;
+ var ite = p.paxi.begin();
+ var pax = p.paxi.front();
+ var rneg:Bool = false;
+ CxIter(cur,p.pverts,{
+ if(pre!=null) {
+ var neg:Bool;
+ var t; raySegment(r,pre,cur);
+ if(t<ret) {
+ ret = t;
+ vec_set(n.p,pax.n);
+ rneg = neg;
+ }
}
+ pre = cur;
+ pax = ite.elem();
+ ite = ite.next;
+ });
+
+ var neg:Bool;
+ var t; raySegment(r,pre,p.pverts.front());
+ if(t<ret) {
+ ret = t;
+ vec_set(n.p,pax.n);
+ rneg = neg;
}
- pre = cur;
- pax = ite.elem();
- ite = ite.next;
- });
-
- var neg:Bool;
- var t; raySegment(r,pre,p.pverts.front());
- if(t<ret) {
- ret = t;
- vec_set(n.p,pax.n);
- rneg = neg;
+
+ if(rneg) vec_negate(n.p);
+
+ return ret;
}
-
- if(rneg) vec_negate(n.p);
-
- return ret;
}
static public inline function rayPolygon(r:Ray,p:Polygon,n:Vec2) {
diff --git a/nape/cx-src/nape/geom/Ray.cx b/nape/cx-src/nape/geom/Ray.cx
index 58f3d43..9d9f8f1 100644
--- a/nape/cx-src/nape/geom/Ray.cx
+++ b/nape/cx-src/nape/geom/Ray.cx
@@ -41,9 +41,14 @@ class Ray {
is taken to be the end of the line segment **/
vec_new(public,v);
+ public var group:Int;
+ public var sensor:Int;
+
/** Create a new ray **/
- public function new(origin:Vec2,direction:Vec2) {
+ public function new(origin:Vec2,direction:Vec2,?group:Int=0,?sensor:Int=0) {
vec_set(a,origin.p);
vec_set(v,direction.p);
+ this.group = group;
+ this.sensor = sensor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.