Openfl Box2d Example Conversion


I am searching for box2d examples in openfl, please add some if available.

I also have tried myself to convert as3 examples to openfl and pretty much close in completing rackdoll example, only one problem is left i.e.

Unable to convert B2Joint to B2MouseJoint

        public var m_mouseJoint = null;
        // Mouse Drag 
        public function MouseDrag():Void{
            // mouse press
            if (Input.mouseDown && m_mouseJoint==null){
                var body:B2Body = GetBodyAtMouse();
                if (body!=null)
                    var md:B2MouseJointDef = new B2MouseJointDef();
                    md.bodyA =;
                    md.bodyB = body;
          , mouseYWorldPhys);
                    md.collideConnected = true;
                    md.maxForce = 300.0 * body.getMass();
                    //var md1:B2MouseJointDef = new B2MouseJointDef();
                    m_mouseJoint =;// cast b2MouseJoint;

                    trace("Creating joint");
            // mouse release
            if (!Input.mouseDown){
                if (m_mouseJoint!=null)
                    m_mouseJoint = null;
            // mouse move
            if (m_mouseJoint!=null)
                var p2:B2Vec2 = new B2Vec2(mouseXWorldPhys, mouseYWorldPhys);
                //m_mouseJoint = cast B2MouseJoint;                   

Above function successfully hang the body on touch/clicking if I comment out the last line “m_mouseJoint.setTarget(p2);” but do not drag the body, uncommenting the line throws error B2Joint has no field setTarget.

AS3 use “as” keyword to convert b2joint to b2mousejoint, how can I achieve same using Haxe Openfl

Thanks in advance

according to openfl guide:

AS3 casting

var car:Car = vehicle as Car;
var toString:String = String (10);
var toNumber:Number = Number ("10");
var toInteger:int = int (10.1);

Openfl casting

var car:Car = cast vehicle;
// or for a safe cast: 
var car = cast (vehicle, Car);
var toString = Std.string (10);
var toNumber = Std.parseFloat ("10");
var toInteger = (10.1);

Thanks friend, I have successfully ported the example to openfl.

Awesome! Glad it’s working :slight_smile: :success:

Thanks :smile:

I am working on Buoyancy test, as it has few issues like BuoyancyController errors.

I just committed a round of fixes that may help :smile:

I’m wondering, where did this set of Box2D demos come from? This seems like something that would be good to include in the repository as a sample

Hurray :smiley: Yes, sure I would like to merge them, believe me it wasn’t hard, credit goes to Openfl and you of-course.

1 Like

Where does the original source come from?

These were AS3 demos, I just converted them to Haxe Openfl. Have sent you a pull request.

Thanks! I see the pull request, but no samples, did you forget to commit them?

They are inside “Tests” folder, Should I rename it ?