broadphase.inRange

Hello,

I think I may have discovered a bug in this function when using the boundary listener to destroy bodies that violate the world AABB. Bodies that breach m_worldAABB.upperBound.y caused a memory allocation problem for me, although destroying bodies that violated any of the other three sides of the world AABB worked fine.

Replacing the code with this seemed to do the trick:

Code:
public bool inRange(bzAABB aabb) {
        return(aabb.lowerBound.x > m_worldAABB.lowerBound.x &&
                 aabb.lowerBound.y > m_worldAABB.lowerBound.y &&
                 aabb.upperBound.x < m_worldAABB.upperBound.x &&
                 aabb.upperBound.y < m_worldAABB.upperBound.y);
}


Can anyone else reproduce this bug, or is it only an issue with my D port?
There cannot be any bugs in the original code, surely? It's too short for something to be hiding in? Not to mention it's been working reliably for many years?

So, two thoughts:
AABB or m_world.AABB is wrong. I found a possible bug recently in calculating AABBs, to do with odd widths and heights. What is your world AABB?

Second:
I notice the following code in the C++:
Code:
template <typename T>
inline T b2Max(T a, T b)
{
   return a > b ? a : b;
}

inline b2Vec2 b2Max(const b2Vec2& a, const b2Vec2& b)
{
   return b2Vec2(b2Max(a.x, b.x), b2Max(a.y, b.y));
}


Have you copied that exactly? If so, it's possible it's doing something different in C#, as the C++ code is a bit weird. The second b2Max should be a total template specialization of the first, as I understand it, not an overload.
BorisTheBrave wrote:
What is your world AABB?


I'm using the same world AABB from the testBed:

Code:
worldAABB.lowerBound.set(-200.0f, -100.0f);
worldAABB.upperBound.set(200.0f, 200.0f);


I'm also using the same exact method to update a shape's aabb at body synchronization.

BorisTheBrave wrote:
I notice the following code in the C++:


Here's the D code I'm using for the vector max:
Code:
bzVec2 bzMax(bzVec2 a, bzVec2 b) {
    bzVec2 c;
    c.x = max(a.x, b.x);
    c.y = max(a.y, b.y);
    return c;
}


max() is a direct binding to c's fmax() function, so as far as I can tell, I was pretty much using the same code..

If this is a bug, I'm thinking that it may have been easy to overlook, considering most people may have their gravity vector pointing down and may not breach worldAABB.upperBound.y very often? The only reason I found this bug is because I added a gravity vector widget to my testBed framework and was throwing objects all over the place.... Here is a link to my demo; it includes sandbox features and the ability to create joints, springs, and bungees with the Tool Mode drop down:

http://svn.dsource.org/projects/blaze/downloads/blazeDemos.zip
Here is the C++ code:
Code:
inline bool b2BroadPhase::InRange(const b2AABB& aabb) const
{
   b2Vec2 d = b2Max(aabb.lowerBound - m_worldAABB.upperBound, m_worldAABB.lowerBound - aabb.upperBound);
   return b2Max(d.x, d.y) < 0.0f;
}


This test requires overlap, not containment. If aabb.lowerBound.y = 101 and m_worldAABB.upperBound.y = 100, then d.y >= 1 and the function returns false.

Do you know of an object AABB and world AABB where this function gives the wrong overlap status?
Erin Catto wrote:
This test requires overlap, not containment. If aabb.lowerBound.y = 101 and m_worldAABB.upperBound.y = 100, then d.y >= 1 and the function returns false. Do you know of an object AABB and world AABB where this function gives the wrong overlap status?


Yes, true; I was using the same (ported) broadphase.inRange code that you posted above. Without using the boundaryListener and destroying objects when they cause a violation, bodies that satisfy this function simply freeze when they leave worldAABB.

However, when using the original function, I kept having an object exception when destroying bodies who's shapes violate (aabb.lowerBound.y > m_worldAABB.upperBound.y). All three the other violations @ 90, -90, and 180 degrees, passed when I destroyed the bodies. Switching the test from overlap to containment by using (aabb.upperBound.y > m_worldAABB.upperBound.y) clears up my memory allocation problem. By replacing my code with the previously posted function, it works for me.

Am I missing something? Has anyone tried to destroy bodies that violate m_worldAABB.upperBound.y with success? Instead of freezing objects in the testbed example when they leave AABB, can you provide a callback example showcasing object destruction?

Many thanks for the quick response!
Ok, I've narrowed my assertion down to broadPhase.removeProxy. For some reason, when shapes leave the world bounds along worldAABB.upperbound.x and worldAABB.upperBound.y, (upperIndex - lowerIndex - 1) is < 0, and I get an exception.

As soon as my shapes begin to cross the worldAABB, their proxy bounds do not compute correctly, and I can observe very strange behavior with draw AABBs turned on. As soon as the box travels a little more outside the box the engine crashes with my memory error explained above.

So long as bodies stay within the world bounds, I have no problems with the engine.

I've double checked and rechecked my broadphase code, which is almost an exact copy from jBox2D, and I can't seem to find any glaring errors. If anyone has an idea of where I'm going wrong, I would certainly appreciate the help.

http://www.dsource.org/projects/blaze
When a shape is out of range, the proxy is destroyed and set to null. Is your code trying to draw an invalid proxy AABB?
Erin Catto wrote:
When a shape is out of range, the proxy is destroyed and set to null. Is your code trying to draw an invalid proxy AABB?


The box is still in range; I reverted back to your original test.... Strangely, if I pull the box back inside worldAABB with the mouse, the shape AABB goes back to normal.
Ok, after many hours of beating my head on the desk, I finally solved my problem!

Considering box2D utilizes ushort's throughout broadPhase and jBox2D uses int values, I had a suspicion that there may have been a sign error somwehere. Also, my bug would only pop up when a proxy interacted with the worldAABB, and this lead me to broadPhase.computeBounds. By adding an abs() to the lowerValues and upperValues assignment, I managed to prevent the memory exception in broadPhase.removeProxy. Here is the code:

Code:
private void computeBounds(int[] lowerValues, int[] upperValues, bzAABB aabb) {

        assert(aabb.upperBound.x >= aabb.lowerBound.x);
        assert(aabb.upperBound.y >= aabb.lowerBound.y);

        bzVec2 minVertex = bzClamp(aabb.lowerBound, m_worldAABB.lowerBound, m_worldAABB.upperBound);
        bzVec2 maxVertex = bzClamp(aabb.upperBound, m_worldAABB.lowerBound, m_worldAABB.upperBound);

        // 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 int conversion.
        lowerValues[0] = abs(cast(int) (m_quantizationFactor.x * (minVertex.x - m_worldAABB.lowerBound.x)) & (int.max - 1));
        upperValues[0] = abs(cast(int) (m_quantizationFactor.x * (maxVertex.x - m_worldAABB.lowerBound.x)) | 1);
        lowerValues[1] = abs(cast(int) (m_quantizationFactor.y * (minVertex.y - m_worldAABB.lowerBound.y)) & (int.max - 1));
        upperValues[1] = abs(cast(int) (m_quantizationFactor.y * (maxVertex.y - m_worldAABB.lowerBound.y)) | 1);
   }


Why casting int values works in Java but not in D, I don't have a good answer....
Zzzzrrr wrote:
Why casting int values works in Java but not in D, I don't have a good answer....

Mmm, are you sure it does work in Java? 🙂 I never specifically checked, so it's quite possible the same bug exists in JBox2d - I just added checking on that to my todo list.

The unsigned/signed issue worried me as I was porting that code, and I'm not sure that it was ever properly resolved, I may just never have run into the bug.

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