broadphase.inRange

I'd like to point out a second possible bug in the same segment of code. Could I get one of you guys to confirm, I'm still apprehensive about committing, particularly in light of overflow bugs from adding 1.

I don't see what the above problem is, or how to fix it, but surely inserting a call to abs cannot be the right permanent solution?
ewjordan wrote:
Mmm, are you sure it does work in Java?


Well, I've checked your online demo, and when shapes violate worldAABB.upperBound.y and worldAABB.upperBound.x, they do indeed freeze. In my case, with Blaze, I would get the strange behavior previously posted.

My first inclination is to think that D may round differently via. casting than Java... but this answer seems too simplistic. I should probably do a detailed analysis of the math involved, but I'm fairly happy that abs() works for now... If there is a more elegant solution I would certainly implement it.

For the curious, compiling Blaze is easy.... I've posted a fairly clear (I hope) guide:
http://www.dsource.org/projects/blaze/wiki/GettingStarted
Alright, I'm coming out and admitting, straight up, that I don't understand a lot of the broadphase code, so I don't know if I have much to add here. I also don't know D very well.

Regardless, I took a look through and did some tests, and I can't seem to reproduce this bug: in the JBox2d testbed, I switched things around so that the BoundaryListener is used and bodies are buffered for destruction when they leave the world bounds (otherwise you get an assertion because the world is locked), and it seems to work fine when bodies leave any of the sides or corners.

One thing: in Java, casting a float that's greater than Integer.MAX_VALUE to an int returns Integer.MAX_VALUE - there's no overflow issue or anything like that, so those bounds come out as Integer.MAX_VALUE - 1 and Integer.MAX_VALUE. I wonder if maybe D's integer cast wraps around or does something else if you're out of integer range? In theory the value that's being cast should never be more than Integer.MAX_VALUE as long as m_quantizationFactor is set properly (the clamp call should make sure the result falls within positive integer range), but in the face of numerical precision issues, it might be possible that it's overshooting.

The above issue never comes up in Java anyways, though (I checked by adding some additional asserts), so even if a casting difference exists between Java and D, there's something else going wrong, too.

So if an abs statement makes a difference, it's masking a different bug. Maybe it's worth testing your clamp code to make sure it's doing what it should be? If you're seeing odd AABBs, that might be at fault - you should really just see the AABBs get clipped by the world boundary, with nothing strange happening, but it sounds like you're seeing some other behavior? I'd say that might be the thing to track down...
BorisTheBrave wrote:
I'd like to point out a second possible bug in the same segment of code. Could I get one of you guys to confirm, I'm still apprehensive about committing, particularly in light of overflow bugs from adding 1.

I don't quite understand the bug under discussion at that link - is there a genuine bug in the broadphase code, or is this more an issue specific to edge shapes or raycasts? Is there a bare bones test case that can display the raw broadphase problem if there is one?

As I mentioned in my above post, the overflow issue doesn't concern me too much in Java, because Java appears to perform clamped casts (something I didn't know until I checked). However, in C we get wraparound, so overflow is a definite worry:
Code:
JAVA:
float f = Integer.MAX_VALUE + 1.0f;
i = (int)f;
System.out.println("i: "+i);

>> i: 2147483647

C++:
float f = USHRT_MAX + 1.0f;
unsigned short i = (unsigned short)f;
cout << "i: " << i << endl;

>> i: 0


I suspect adding 1 in those statements, as proposed on the other thread, will cause this wraparound to show up at the edges of the world, which could cause trouble (as the upper values would end up less than the lower ones, a situation that I'm sure Box2d won't be very happy with).

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