Problem with TestSegment converting from C++ manual example

Code:
      public static function RayCast( body:b2Body , startPos:b2Vec2 , endPos:b2Vec2 ):b2Vec2
      {
         var lambda:Number = 1.0;
         var normal:b2Vec2;
         var segment:b2Segment = new b2Segment;
         segment.p1 = startPos;
         segment.p2 = endPos;
         var shape:b2Shape = body.GetShapeList(); //get first shape
         
         var hit:Boolean = TestSegment( body.GetXForm() , &lambda, &normal, segment, 1 );  //This is C++ and I can't convert it... what is &?
         
         if (hit)
         {
            trace(hit);
            //return (1 - lambda) * segment.p1 + lambda * segment.p2;
         }
         return null;
      }//RayCast(



Here is my work in progress code. The goal is to use the startPos, endPos and other data to return a b2Vec2 which contains the exact position of the intersection so I can draw a shadow there at the end of the ray, under the player's feet.

So far the problem is converting from the manual example, which is in C++. It is hurting my head and there seems to be no AS3 version I can learn from. Forgive me for not knowing C++ well enough to convert it. The manual example is here: http://www.box2d.org/wiki/index.php?title=TestSegment

I hope someone can convert that simple example in the manual to AS3 code, then I will be able to do the rest of the work I believe...

Any help guys?
Yeah, this is pretty non obvious. I won't fully explain, but & in this case gets the address of an object, (i.e. it returns a pointer). Basically, it's used to ensure pass by reference semantics. As this is the default in AS3 for all objects, no worries.
b2Vec2 v;
myFunct(&v);
Becomes
var v:b2Vec2 = new b2Vec2();
myFunct(v);

The difficulty comes because lambda is an Number,not an object. We don't want to "edit inside it", we want to replace it fully. So we have to resort to a hack to emulate C references:
int i = 7;
myFunct2(&i);
becomes
var i:int = 7;
var ref:Array = [i];
myFunct2(ref);
i = ref[0];

See? Or would you still like a fully translated function.

The only occurrences of this are TestSegment and RaycastOne. I am amending the code comments, but if you'd like to update the wiki too, that'd be great.
Hi Boris, I would like a fully translated function as I'm still confused, and to be honest - embarassed deeply at my lack of programming knowledge. Thank you for helping!

AS3 seems considerably more user friendly than C++.
AS3 is more user friendly than C++ - you sacrifice a lot of execution speed for ease of programming. If you application doesn't need a ton of speed then using an easier to use language is great. Language discussion about what is better can go on forever so lets just say different languages are better for different things.

So, basically, this function is going to potentially return 3 values if there is a hit. First is the obvious return value, "hit" in your example. "Lambda" and "normal" are also potential return values because their pointer is passed into that function and TestSegment can modify them directly (in c++).

As Boris said, in AS3 non-object derived types like "int" or "number" are passed by value and not by reference so you have to do some kind of hack so that one function can return multiple values.

A better way to do this in AS3 would be having the function return a structure/object with all of its return data inside. Like...
Code:
var segmentTestResult:b2RayCastResult = TestSegment(body, segment, 1);

if (segmentTestResult.bHit)
{
  trace(segmentTestResult.normal);
}

... etc.
I will be porting this project to iPhone for sure once it's done so I'll be getting into C++ and objective C soon enough & will be able to contribute more to the community. However I am on a *really* tight deadline for this project and I cannot learn C++ right now until the game is shipped - can't afford to purchase a mac + iPhone just yet with a family to look after.

Can you if you have time, port the simple C++ test to AS3 please? It will be a massive help for sure. Here are the lines I need to port:

Code:
float lambda;
b2Vec2 normal;
b2Segment segment;
segment.p1 = start;
segment.p2 = end;
bool hit = pShape->TestSegment( pShape->GetBody()->GetXForm(), &lambda, &normal, segment, 1 );

b2Vec2 intersectionPoint = (1 - lambda) * segment.p1 + lambda * segment.p2;


It seems simple enough, however the problem lies in the TestSegment stuff. I'm still not able to get it working.

The information I'd like to get hold of is the precise location where my ray touches the target shape, I believe this is the intersection. Also confused how to port that last line (intersection) as its the lambda stuff with arrays again.
Fine, fine.

Code:
var lambda:Array = [0];
var normal:b2Vec2 = new b2Vect();
var segment:b2Segment = new b2Segment;
segment.p1 = start.Copy();
segment.p2 = end.Copy();
var hit:Boolean = pShape.TestSegment( pShape.GetBody().GetXForm(), lambda, normal, segment, 1 );
var intersectionPoint:b2Vec2 = new b2Vec2();

intersectionPoint.x  = (1 - lambda[0]) * segment.p1.x + lambda[0] * segment.p2.x;
intersectionPoint.y  = (1 - lambda[0]) * segment.p1.y + lambda[0] * segment.p2.y;


Note + and * are overloaded operators in the C++. They have equivalent functions in b2Math.as, but I've just inlined them instead.
Thank you for the great code! It works perfectly on flat platforms but the moment I touch a platform with an angle other than 0, the hit doesn't register.
Also note that TestSegment returns an int, not a bool: http://personal.boristhebrave.com/perma ... stSegment()

You need to check against !=b2Shape.e_missCollide, which is 0, usually, so your code still works (by design).

Other than that I still cannot see anything wrong with the ported code.

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