optomized collisions to exclude half of the tests
[physics.git] / src / collisionManager.cpp
index df83319..5d2dff0 100644 (file)
@@ -42,6 +42,8 @@ bool getInfo(Ball* b1, Ball* b2, CollisionInfo* cInfo);
 
 const int xDivisions = 20;
 const int yDivisions = 16;
+const int screenX    = 800;
+const int screenY    = 600;
 
 setPhys divisions[xDivisions][yDivisions];
 
@@ -103,15 +105,15 @@ void placeEntity(PhysicsEntity* p)
     const float& yb     = b->positionRaw().y;
     const float& rad    = b->radius;
 
-    for( int x = (int)(xb - rad);
-             x < (int)(xb + rad);
+    for( int x =  static_cast<int>( (xb - rad) / (screenX / xDivisions) );
+             x <= static_cast<int>( (xb + rad) / (screenX / xDivisions) );
              x++ )
     {
         if(x < 0 || xDivisions <= x)
             break;
 
-        for( int y = (int)(yb - rad);
-                 y < (int)(yb + rad);
+        for( int y =  static_cast<int>( (yb - rad) / (screenY / yDivisions) );
+                 y <= static_cast<int>( (yb + rad) / (screenY / yDivisions) );
                  y++ )
         {
             if(y < 0 || yDivisions <= y)
@@ -140,10 +142,10 @@ void updateEntities()
                      it2 != divisions[x][y].end();
                      it2++ )
                 {
-                    if( *it1 != *it2 )
-                    {
-                        applyCollision(*it1, *it2);
-                    }
+                    if( *it1 == *it2 )
+                        break;
+
+                    applyCollision(*it1, *it2);
                 }
             }
         }