Edit: Please ignore this bug post. I see now my normal it totally wrong... I really did void the warranty by modifying the code I guess. Sorry for wasting your time.
Hi, my name is Justin and this is my first post here.
First I'd like to thank Erin and all other contributors to Box2D -- you don't need me to tell you all how amazing this software is.
I think I did come across some unexpected results in the b2PolygonShape::RayCast() code. In a game I'm working on (iOS for the record) I wanted to use raycasting to generate shadows with Box2D geometry. I had done some simple line-segment intersection code in the past but opted to try to utilize the Box2D raycasts because the rest of the API is so over my head I figured it must be doing the calculations smarter/faster/etc.
Sometimes I would miss intersections though with the Box2D RayCast() code -- which I admittedly extracted from its original b2PolygonShape context and modified VERY SLIGHTLY thus voiding it's warranty, but I was very careful with it and below have one of the conditions/states I managed to isolate that was giving me trouble.
Truth be told, I don't really know how the Box2D method and my method work entirely (I got my formula off of some math webpage over a year ago) so I can't really prove that one is right or wrong, beyond the results I see in my application (rendering the two segments it's clear that they should intersect). Maybe I'm missing something or messed something up though.
Anyway, thanks again to everyone who has helped make Box2D possible.
Edit:
P.S. I'm working out of cocos2D v.1.0.0
Hi, my name is Justin and this is my first post here.
First I'd like to thank Erin and all other contributors to Box2D -- you don't need me to tell you all how amazing this software is.
I think I did come across some unexpected results in the b2PolygonShape::RayCast() code. In a game I'm working on (iOS for the record) I wanted to use raycasting to generate shadows with Box2D geometry. I had done some simple line-segment intersection code in the past but opted to try to utilize the Box2D raycasts because the rest of the API is so over my head I figured it must be doing the calculations smarter/faster/etc.
Sometimes I would miss intersections though with the Box2D RayCast() code -- which I admittedly extracted from its original b2PolygonShape context and modified VERY SLIGHTLY thus voiding it's warranty, but I was very careful with it and below have one of the conditions/states I managed to isolate that was giving me trouble.
Code:
b2Vec2 a1(267.315063f, 160.510284f);
b2Vec2 a2(2976.31348f, -3624.70898f);
b2Vec2 b1(-211.07962f, -116.96814f);
b2Vec2 b2(588.92041f, -116.96814f);
b2Vec2 bNormal(1, 0); // note the common Y-value of b1 and b2
//the following snippet is adapted from b2PolygonShape::RayCast()
b2Vec2& p1 = a1;
b2Vec2& p2 = a2;
b2Vec2 d = p2 - p1;
b2Vec2& v1 = b1;
b2Vec2& v2 = b2;
b2Vec2& normal = bNormal;
// q = p1 + t * d
// dot(normal, q - v1) = 0
// dot(normal, p1 - v1) + t * dot(normal, d) = 0
float32 numerator = b2Dot(normal, v1 - p1);
float32 denominator = b2Dot(normal, d);
if (denominator != 0.0f)
{
float32 t = numerator / denominator;
if (t >= 0.0f && 1.0f >= t)
{
b2Vec2 q = p1 + t * d;
// q = v1 + s * r
// s = dot(q - v1, r) / dot(r, r)
b2Vec2 r = v2 - v1;
float32 rr = b2Dot(r, r);
if (rr != 0.0f)
{
float32 s = b2Dot(q - v1, r) / rr;
if (s >= 0.0f && 1.0f >= s)
{
//intersection
}
}
}
}
//the following snippet is adapted from my own intersection code
b2Vec2 dP = p2 - p1; // A1 - A0 (segment A delta)
b2Vec2 dV = v2 - v1; // B1 - B0 (segment B delta)
float_t under = (dV.x * dP.y) - (dV.y * dP.x); // deltaA cross deltaB (denominator)
if (under != 0)
{
b2Vec2 d0 = v1 - p1; // A0 - B0 (i.e. initial points delta or deltaC)
float_t progressP = ((dP.x * d0.y) - (dP.y * d0.x)) / under; // (deltaC cross deltaA) / denominator (i.e. relative intersection point on segment A)
if (progressP >= 0 && progressP <= 1)
{
float_t progressV = ((dV.x * d0.y) - (dV.y * d0.x)) / under; // (deltaC cross deltaB) / denominator (i.e. relative intersection point on segment B)
if (progressV >= 0 && progressV <= 1)
{
//intersection
}
}
}
b2Vec2 a2(2976.31348f, -3624.70898f);
b2Vec2 b1(-211.07962f, -116.96814f);
b2Vec2 b2(588.92041f, -116.96814f);
b2Vec2 bNormal(1, 0); // note the common Y-value of b1 and b2
//the following snippet is adapted from b2PolygonShape::RayCast()
b2Vec2& p1 = a1;
b2Vec2& p2 = a2;
b2Vec2 d = p2 - p1;
b2Vec2& v1 = b1;
b2Vec2& v2 = b2;
b2Vec2& normal = bNormal;
// q = p1 + t * d
// dot(normal, q - v1) = 0
// dot(normal, p1 - v1) + t * dot(normal, d) = 0
float32 numerator = b2Dot(normal, v1 - p1);
float32 denominator = b2Dot(normal, d);
if (denominator != 0.0f)
{
float32 t = numerator / denominator;
if (t >= 0.0f && 1.0f >= t)
{
b2Vec2 q = p1 + t * d;
// q = v1 + s * r
// s = dot(q - v1, r) / dot(r, r)
b2Vec2 r = v2 - v1;
float32 rr = b2Dot(r, r);
if (rr != 0.0f)
{
float32 s = b2Dot(q - v1, r) / rr;
if (s >= 0.0f && 1.0f >= s)
{
//intersection
}
}
}
}
//the following snippet is adapted from my own intersection code
b2Vec2 dP = p2 - p1; // A1 - A0 (segment A delta)
b2Vec2 dV = v2 - v1; // B1 - B0 (segment B delta)
float_t under = (dV.x * dP.y) - (dV.y * dP.x); // deltaA cross deltaB (denominator)
if (under != 0)
{
b2Vec2 d0 = v1 - p1; // A0 - B0 (i.e. initial points delta or deltaC)
float_t progressP = ((dP.x * d0.y) - (dP.y * d0.x)) / under; // (deltaC cross deltaA) / denominator (i.e. relative intersection point on segment A)
if (progressP >= 0 && progressP <= 1)
{
float_t progressV = ((dV.x * d0.y) - (dV.y * d0.x)) / under; // (deltaC cross deltaB) / denominator (i.e. relative intersection point on segment B)
if (progressV >= 0 && progressV <= 1)
{
//intersection
}
}
}
Truth be told, I don't really know how the Box2D method and my method work entirely (I got my formula off of some math webpage over a year ago) so I can't really prove that one is right or wrong, beyond the results I see in my application (rendering the two segments it's clear that they should intersect). Maybe I'm missing something or messed something up though.
Anyway, thanks again to everyone who has helped make Box2D possible.
Edit:
P.S. I'm working out of cocos2D v.1.0.0