Raycast missing shapes

I have also noticed errors with the RaycastOne and related methods which do not pop up via exhaustive testSegment testing, but I cannot say what are the cases that cause them. All I know is that I have a laser that reflects off all bodies and is created inside a box made from static bodies, but it occasionally is capable of going through the box walls.

Has this been fixed in the svn?
No, I didn't try it, I didn't have time last night. I have now, and I see the problem you are referring to. It's the contrary one to what I had thought.
But now I look closer, I see that shaktools code also includes a very similar provision to yours, just with a smaller constant. Set it large enough, and it works fine.

So the real question, is why does the constant have to be so high? The original constant of 100*epsilon should be large enough to cover any rounding errors, and deferring the division doesn't help the accuracy.

The bug in your code, is you return m_normal, which is in local co-ordinates, instead of the world normal. Very minor.

Edit: dc443. That sounds like a legitimate raycasting bug rather than a TestSegment one. Do you think you could mock up an example demonstrating it, perhaps using the laser explosion technique FrankForce used.
I looked into this problem a little further, here's what I noticed....

First I changed the movement code to something like viewCenter.x += moveSpeed * viewZoom. This allows me to still move the camera around while I zoom in very closely on problem areas. I modified my laser blaster to fire a single ray per frame in the up direction (angle = b2_pi/2). Running with original edge edge shape code I was able to comb against the static edges test shape and find at least small holes that the raycast was passing through. We already know that the glitch occurs roughly the point where two edges connect. Turning on AABB's we can see that the AABBs of connected edges overlap somewhat. In cases where the glitch occurs you can fire the ray directly between two edges where both AABB's overlap and the raycast fails. So in these cases where the ray is clearly intersecting both AABBs the b2EdgeShape::TestSegment() should be called twice.

Doing some breakpoint tests I found that in cases where the raycast test was getting through b2EdgeShape::TestSegment() was only being called once per raycast. In other words it was only testing one of the edges and not the other that was connected at that point. I believe that if the test was done against the other connecting segment it would have returned a collision. So in this case QuereySegment() is throwing out edge shapes that I guess its currently considering a narrow miss.

I also noticed that in some cases AABB (at least what is visually displayed) seem to be slightly off for edges in some cases not containing the original end points but I'm not sure if that is related.
If you give me the parameters of a raycast that you think isn't calling enough TestSegments, I can take it from there and trace into the raycasting code for a possible bug.
Boris, ok, this code should give you a good test case to work with. Use it with the static edges test. You should see the ray passing through the top of the loop while also passing through the AABBs of both edge shapes.

Code:
float32 lambda;
b2Vec2 normal;
b2Segment segment = {b2Vec2(6.3530765f, 10.028790f), b2Vec2(6.3530722f, 110.02879f)};
b2Shape* shape = m_world->RaycastOne(segment,&lambda,&normal,false,NULL);
if (shape)
   m_debugDraw.DrawSegment(segment.p1,(1-lambda)*segment.p1+lambda*segment.p2,b2Color(255,0,0));
else
   m_debugDraw.DrawSegment(segment.p1,segment.p2,b2Color(0,0,255));
Thanks, that's really helpful!
I've tracked down a super-obscure bug that was causing the problem, and I think I've fixed it. But unfortunately it hasn't fixed the general problem of leaking raycasts, just that particular leak. I'll try look into it further tomorrow, but for now, I'm providing the tentative patch. It uses the goto statement, but I think this is a genuinely legitimate case for it.

Attachment:
patch.patch [2.5 KiB]
Downloaded 48 times


Also, I discovered another possible bug:
Code:
   // Bump lower bounds downs and upper bounds up. This ensures correct sorting of
   // lower/upper bounds that would have equal values.
   // TODO_ERIN implement fast float to uint16 conversion.
   lowerValues[0] = (uint16)(m_quantizationFactor.x * (minVertex.x - m_worldAABB.lowerBound.x)) & (B2BROADPHASE_MAX - 1);
   upperValues[0] = (uint16)(m_quantizationFactor.x * (maxVertex.x - m_worldAABB.lowerBound.x)) | 1;

If (m_quantizationFactor.x * (minVertex.x - m_worldAABB.lowerBound.x)) is (say) 1.5, then this will list the bounds as 0 and 1. That cannot be right?
Nice, looks like you tracked down part of the problem. I applied your patch and yes that particular glitch is fixed. I think we are very close to a full solution. First here are a few more test segments...

Code:
const b2Segment segment1 = {b2Vec2(-7.8907800f, 6.1776128f), b2Vec2(-3.8292155f, 105.38913f)};
const b2Segment segment2 = {b2Vec2(-7.0022640f, 5.9507990f), b2Vec2(-84.407677f, 69.667091f)};
const b2Segment segment3 = {b2Vec2(2.7193356f, 7.3012047f), b2Vec2(48.245796f, 95.472626f)};


Now if you look at those raycasts you will see that this time the lines do not intersect the AABB of the edge shape that it was probably supposed to. This stems back to the probelem of the AABBs of the edge segments not containing their end points. So a possible fix I have is to add 1 to the upper values in b2BroadPhase::ComputeBounds() ...

Code:
lowerValues[0] = (uint16)(m_quantizationFactor.x * (minVertex.x - m_worldAABB.lowerBound.x)) & (B2BROADPHASE_MAX - 1);
upperValues[0] = (uint16)(m_quantizationFactor.x * (maxVertex.x - m_worldAABB.lowerBound.x) + 1) | 1;
lowerValues[1] = (uint16)(m_quantizationFactor.y * (minVertex.y - m_worldAABB.lowerBound.y)) & (B2BROADPHASE_MAX - 1);
upperValues[1] = (uint16)(m_quantizationFactor.y * (maxVertex.y - m_worldAABB.lowerBound.y) + 1) | 1;


Just looking at the math you can see that without the 1 the lower and upper bound would not be containing the top part of the shape. This effects all shapes not just edges. So without this change you can zoom in and see that even for poly shapes and circles their AABB's does not fully contain the shape. Then again maybe that's how it is supposed to be since it is all quantized? In that case there is probably code in test segment has a similar off by 1 error.
No, I think it's that code itself that is buggy. I noted it a long time ago when writing the raycast thing, but just assumed it worked as no bugs had been reported.

I would have liked to fix it with:
lower = (uint16)(a);
upper = (uint16)(a+1);
But that causes an assert elsewhere. Apparently the lower and upper bounds must be even and odd. So I'd stick with your suggestion, unless Erin weighs in with a better fix.

Final patch:
Attachment:
patch.patch [4.02 KiB]
Downloaded 54 times


I'll hold onto this for a while, then commit if there are no further complaints.
I'm a bit late but I have games running on box 2.0.1 since 2 years and I just recently needed to heavily use ray casting in one of them. It was often missing shapes but after implementing BorisTheBrave fixes it is now working perfectly! 😀

Thkx a lot Boris!!!

Edit :
Well, I must let you know that sadly I now have a problem with the m_pairCount going too high (exceeding b2_maxPairs) in some of my simulations where I have a lot of objects 😕 Its seems to bust as soon as I have 130 objects and more (when I should be able to have up to b2_maxProxies = 512)

Original thread: https://box2d.org/forum/viewtopic.php?t=1967