some bugs and remark

hello

i take the new version using SVN and i have some bugs when i try to compile the examples 😮

1- (b2ContactSolver.as)
Code:
const k_maxConditionNumber:Number = 100.0;

1049: Affectation illégale à une variable déclarée comme une constante.

2- (b2ContactSolver.as)
Code:
const k_errorTol:Number  = 0.001;

1049: Affectation illégale à une variable déclarée comme une constante.

3- (HelloWorld.as)
Code:
m_world.Step(m_timeStep, m_iterations);

1136: Nombre d'arguments incorrect. 3 attendus.


for 1 and 2, i use var instead const .... you use the const inside a loop !

for 3, i use m_world.Step(m_timeStep, m_iterations, m_iterations);



some remarks about Box2DFlash :
Box2D is a very powerfull physic engine (i try all for AS3 and Box2D have the best performance) but the naming convention is ignoble and nonsense !
a REAL listener for listening collision will be very useful (using listener convention in AS3 (addEventListener)).



keep doing a good job
please, can you do something for the naming convention ? 😉
The const to var is an annoyance. You seem to suggest it's an intrinsic part of the language now, so I guess we'll have to change it back.

3) Minor changes are made to the API quite often. That's the price you pay to use a more recent version.

I'm not sure what's wrong the naming convention, could you be more specific?
Perhaps you mean that the API doesn't use Flash idioms very much, such as no getters/setters, use of Arrays and Functions or standard style events. Par of the course for keeping a fairly close port. I expect it to change, but very slowly.

I should point out the listener system will never be adapted to the Flash one. There's no point in having multiple listeners for one event, and it would be prohibitive to performance. You write your own b2ContactListener, and tell that to raise Flash events for what you desire in about 6 lines of code per event, so this hardly seems a glaring deficiency.
BorisTheBrave wrote:
I'm not sure what's wrong the naming convention, could you be more specific?
Perhaps you mean that the API doesn't use Flash idioms very much, such as no getters/setters, use of Arrays and Functions or standard style events. Par of the course for keeping a fairly close port. I expect it to change, but very slowly.

yes, the lack of getter/setter is the most disturbing the first time i start with Box2D but there is more :
-Class name need to always start with a uppercase (in AS3 but in other languages too (C/C++/Delphi/Java/...)) and sometimes preceding with a "C" (Delphi/C++/...)
the "b2" at the beginning is not usefull in AS3 because we use the package name when we have a conflict name with our own class.
-package name is always in lowercase (for avoiding confusion with the class name ❓)
-methods start with a lowercase like for properties/variable
-normaly constant is all in uppercase
- ....

but if you have to make the AS3 version from the C++ version, i understand that it's difficult to have 2 naming convention 🙁

thank's !




in b2Shape.as (and other files), you have C++ enum declaration :
Code:
package Box2D.Collision.Shapes{
   //.............
   //.............
   //enum b2ShapeType
   //{
      static b2internal const e_unknownShape:int =    -1;
      static b2internal const e_circleShape:int =    0;
      static b2internal const e_polygonShape:int =    1;
      static b2internal const e_edgeShape:int =       2;
      static b2internal const e_shapeTypeCount:int =    3;
   //};

maybe you can replace this in AS3 by using a dedicated class (in an other file) with "internal static const" variable ?
and avoid the use of namespace ?
ie :
Code:
package Box2D.Collision.Shapes{
   class ShapeType
   {
      internal static const e_unknownShape:int =    -1;
      internal static const e_circleShape:int =    0;
      internal static const e_polygonShape:int =    1;
      internal static const e_edgeShape:int =       2;
      internal static const e_shapeTypeCount:int =    3;
   }
}

the use while be limited to the same package : Box2D.Collision.Shapes




in b2Distance.as you use Math.abs() several time, it's better for high performance in AS to use (x < 0 ? -x : x)
same remark for Math.min/max (x > y ? x : y) b2Segment.as / b2World.as / b2Distance.as / b2TimeOfImpact.as
all Math fonction in Flash is coded with the feet 😐
int/uint is faster than Number
etc ...
These are mostly valid observations and it sounds like you'd be interested in helping optimize the engine. However, I've been programming for a while and I can tell you there is no 'one' coding style. Everyone likes their own style/ the style they learned. I agree that the engine is set up in a way that makes more sense for c++, but I don't see anything wrong with the naming convention.

If you are interested in optimizing box2DAS3, then check out the thread I started - http://www.box2d.org/forum/viewtopic.php?f=8&t=3014
VengantMjolnir wrote:
These are mostly valid observations and it sounds like you'd be interested in helping optimize the engine. However, I've been programming for a while and I can tell you there is no 'one' coding style. Everyone likes their own style/ the style they learned. I agree that the engine is set up in a way that makes more sense for c++, but I don't see anything wrong with the naming convention.

i don't agree with you, in all language where i have good knowledge (PHP/AS/Delphi/C#/JS/Java/...), the naming convention are the same, there is just some little difference related to specificity of each language but the basic is the same for all ...

C++ : http://geosoft.no/development/cppstyle. ... onventions
AS3 : http://flex.mentalaxis.com/?p=8
PHP : http://framework.zend.com/manual/en/cod ... tions.html
JAVA : http://java.sun.com/docs/codeconv/html/ ... 8.html#367

you can see that the basic naming convention are the same for all language :
Class/Interface : first letter uppercase and the first letter of each internal word uppercase (UpperCamelCase)
method/variable : first letter lowercase and the first letter of each internal word uppercase (lowerCamelCase)
package : all lowercase
constant : all uppercase with words separated by underscores
I'm calling bullshit on this.
C++ varies enormously on style - you've clearly just selected a style guide that suits you. FWIW, the standard library doesn't conform. Similarly, PHP doesn't have this style - they use underscores everywhere as even the linked guide states, though it seems again you selected this rather than looked at the standard library or other modules. Don't mislead in an attempt to prove your point, that's not cool.
But largely yes, what you've described is the most popular prevailing convention, and matches the AS3 standard library (the most compelling reason, imho). The b2 thing particularly irks me. But it is silly to change conventions just because you dislike it slightly - one serves just as well as another. It is not worth breaking compatibiltiy with the C++ version and old code. You yourself complained that the number of arguments to b2World had changed, can you imagine the problems from wider changes.

Enum classes should probably be taken up with Erin as it's concievable to apply to all ports. But in general I don't see much benefit from the change.

As the hammer says, work on optimizing Box2D is a continuing battle. If you want to submit a patch replacing all the abs,max, and min calls, I"d be happy to take it. As far as I know, int/Number are used appropriately everywhere though. Physics is not the sort of thing that will cope well with rounding errors in a lot of places, so Numbers are are usually necessary.
ok ok ok ... i still disagree about naming convention, but i don't want to start another round 🙂


int/uint/Number : the time difference between each is at the maximum 5 <> 30%, it's not relevant.
but the difference between Math.abs and x < 0 ? -x : x is 200 <> 350% (same for Math.min/max) ...

this is the bench for b2Distance.as -> InPoints() between the original and the new

------------------ InPointsOriginal ------------------
2023 Millieme
------------------ InPointsLolo ------------------
761 Millieme
------------------ InPointsOriginal ------------------
1840 Millieme
------------------ InPointsLolo ------------------
616 Millieme
------------------ InPointsOriginal ------------------
1705 Millieme
------------------ InPointsLolo ------------------
769 Millieme

the new InPoints method :
Code:
      static public function InPoints(w:b2Vec2, points:Array, pointCount:int):Boolean{
         const k_tolerance:Number = 100 * Number.MIN_VALUE;
         for (var i:int = 0; i < pointCount; i++){
            var points_i:b2Vec2 = points[i];
            var dX:Number = w.x - points_i.x < 0 ? -(w.x - points_i.x) : w.x - points_i.x;
            var dY:Number = w.y - points_i.y < 0 ? -(w.y - points_i.y) : w.y - points_i.y;
            var mX:Number = w.x < 0 ? -w.x : w.x >  points_i.x < 0 ? -points_i.x : points_i.x ? w.x < 0 ? -w.x : w.x : points_i.x < 0 ? -points_i.x : points_i.x;
            var mY:Number = w.x < 0 ? -w.y : w.y >  points_i.y < 0 ? -points_i.y : points_i.y ? w.y < 0 ? -w.y : w.y : points_i.y < 0 ? -points_i.y : points_i.y;
            
            if (dX < k_tolerance * (mX + 1) &&
               dY < k_tolerance * (mY + 1)){
               return true;
            }
         }
         return false;
      }




another change to made (no bench this time) :
b2BroadPhase.as -> BinarySearch()
Code:
   static public function BinarySearch(bounds:Array, count:int, value:uint):uint
   {
      var low:int = 0;
      var high:int = count --;
      while (low <= high)
      {
         var mid:int = ((low + high) >> 1);
         var bound:b2Bound = bounds[mid];
         if (bound.value > value)
         {
            high = mid --;
         }
         else if (bound.value < value)
         {
            low = mid ++;
         }
         else
         {
            return uint(mid);
         }
      }
      
      return uint(low);
   }

i just search the maximum speed !
all the change i have made can apply to C++ too !
there's a lot of change to made to all the class, i don't know the range (and use) of the value for each variable, maybe i can do more and better with this knowing ...

this is all for now.
The optimization to b2distance is awesome, thank you for that. However, the one for b2broadphase is slight, or maybe even non existant.
If you don't believe me, try this site which has quite a number of tests available. (make sure to run the test more than once.... you'll be surprised by the variation )

http://businessintelligence.me/projects/performance_tester/performanceTester.html

Basically, the difference between /2 or bitshift is so low it may as well not exist and its not guaranteed to exist in the first place. Also, your milage may vary but I get no consistent results that ++ is faster than x = x + 1.

Erin has said he is replacing the broadphase so for myself I'm not going to bother looking into optimizing it at all.
I've patched InPoints (barring 100.0->100 and ++i->i++ which seem to serve little point), Thanks for that. For the record, I'd ideally prefer patches as that's much less effort for me.

I'll leave off the other part for the reasons given above.

Slowly we'll get the engine faster and faster, but it's difficult to say how much difference each step makes individually.
VengantMjolnir wrote:
Basically, the difference between /2 or bitshift is so low it may as well not exist and its not guaranteed to exist in the first place


no no, the difference between /2 or *2 (/ and * with all pair number (2468)) against bitwise operator is not low !!
make your own test and you well see, it's nearly 25 to 75% !
i have make this bench several time :
------------------ x / 2 ------------------
1945 Millieme
------------------ x >> 1 ------------------
1418 Millieme
------------------ x / 2 ------------------
1414 Millieme
------------------ x >> 1 ------------------
1273 Millieme
------------------ x / 2 ------------------
1583 Millieme
------------------ x >> 1 ------------------
1193 Millieme
------------------ x / 2 ------------------
1409 Millieme
------------------ x >> 1 ------------------
1254 Millieme


yes, i agree about x++, x+=1, x=x+1 ... no real difference can be mesured. it's just for the code beauty 😀


BorisTheBrave wrote:
I've patched InPoints (barring 100.0->100 and ++i->i++ which seem to serve little point), Thanks for that. For the record, I'd ideally prefer patches as that's much less effort for me.

thank's, the real optimization is to remove Math.min/max/abs (all the Math.xxx() methods)





B2DAS3 is really fast, but i think it's possible to win 50% speed at the minimum with just little optimization !
the only problem is that B2D it's not a final version ... and i don't know how B2D really work 😕 and which class are the most used or the more heavy 😕 ... etc

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