Calendar

November 2014
Mo Tu We Th Fr Sa Su
<< >>
12
3456789
10111213141516
17181920212223
24252627282930

Langs

Physaxe 1.2 example

Posted on Jan 02 2009

I was recently adjusting the stability of Physaxe, a Haxe 2D Physics Engine, and came up with good results and bug fixes, so I just released 1.2 version. Here's a funny example where you can move the mouse around to modify the gravity. There's currently no gravity-center so it will only change the direction and power of the gravity.

The material used has a very small friction, so it's easy for the blocks to slide on each other.

BTW Physaxe is still lacking joints support, so I'm looking for someone interested in helping on that topic, please write me if you are ;)

[phx_box.swf]

And of courses the sources, if you want to play with it :

  • install Haxe
  • run the command haxelib install physaxe to install the Physaxe library
  • copy and save the source code to Test.hx
  • run haxe -swf9 test.swf -main Test -lib physaxe to compile it
  • have fun !
class Test {

    var root : flash.display.MovieClip;
    var world : phx.World;

    function new(root) {
        this.root = root;
        var w = root.stage.stageWidth;
        var h = root.stage.stageHeight;
        world = new phx.World(new phx.col.AABB(0,0,w,h),new phx.col.SortedList());
        var p0 = phx.Const.DEFAULT_PROPERTIES;
        p0.biasCoef = 0.3;
        p0.maxDist = 2;
        world.sleepEpsilon = 0; // no sleep (gravity can change anytime)
        var floor = new phx.Body(0,0);
        floor.addShape(phx.Shape.makeBox(w,30,0,0));
        floor.addShape(phx.Shape.makeBox(w,30,0,h-30));
        floor.addShape(phx.Shape.makeBox(30,h,0,0));
        floor.addShape(phx.Shape.makeBox(30,h,w-30,0));
        floor.isStatic = true;
        world.addBody(floor);
        var mat = new phx.Material(0,0.3,1);
        for( i in 0...100 ) {
            var points = new Array();
            var size = (10 + Std.random(20)) * 0.7;
            var a = 2 * Math.PI;
            do {
                points.push(new phx.Vector(Math.cos(a) * size,Math.sin(a) * size));
                a -= (Std.random(20) + 5) * 0.1;
            } while( a > 0 );
            var shape = new phx.Body(Std.random(w - 100) + 50,Std.random(h - 100) + 50);
            shape.addShape(new phx.Polygon(points,new phx.Vector(0,0),mat));
            world.addBody(shape);
        }
    }

    function update() {
        var w = root.stage.stageWidth;
        var h = root.stage.stageHeight;
        var xf = (root.mouseX / w) * 2 - 1;
        var yf = (root.mouseY / h) * 2 - 1;
        world.gravity.x = xf * 0.5;
        world.gravity.y = yf * 0.5;
        world.step(1.0,20);
        root.graphics.clear();
        var d = new phx.FlashDraw(root.graphics);
        d.shape.lineSize = 0.5;
        d.staticShape.line = null;
        d.drawWorld(world);
    }

    public static function main() {
        var mc = flash.Lib.current;
        var t = new Test(mc);
        mc.stage.quality = flash.display.StageQuality.MEDIUM;
        mc.addEventListener(flash.events.Event.ENTER_FRAME,function(_) t.update());
    }

}
8 comments
  • Jan 05, 2009 at 14:37

    Wow! Nice!

  • Jan 05, 2009 at 22:49

    Wow, a new version already, huh? Nice!

    This may not be exactly the sort of joints you're interested in, but I threw together a quick "DistanceJoint" which should be enough to add simple stickman stuff.

    Check it out:
    http://brianin3d.googlepages.com/phystickman.html
    http://code.google.com/p/brianin3d-phystickman/

    Keep up the good work!

  • Jan 06, 2009 at 14:21

    Nice.

    Quick question, why didn't you type the contents of the Array 'points'?

    i.e.

    var points:Array<phx.Vector> = new Array()

  • Jan 07, 2009 at 21:05

    Hi, Tink
    The content of the Array is guessed at the time of the .push() , so there's no need to type it, thanks to Haxe type inference.

  • l levin
    Feb 10, 2009 at 00:40

    Nicolas,
    Ever since haxe/php came around I have been thinking about whether it would be doable for haxe to compile to python as well. If yes, do you think that applications relying on math and oop-heavy stuff such as physaxe would run quicker as python than as php ?
    Thanks !

  • Feb 14, 2009 at 11:44

    If you're looking for additional physics performances, you can use Hugh Anderson HXCPP which compiles Haxe to C++ :

    http://gamehaxe.com/2008/12/23/hxcpp-03-released/

  • Feb 02, 2010 at 18:42

    Hi Nicolas,
    and thank you for your very fast physics engine!
    ;-)
    Since I have not found any physaxe example with moving background objects, I developed a pinball-like game and I shared the source code.
    It is possible to find the game and the sources at: http://thegameinventor.blogspot.com/2010/02/zicky.html

  • Lars
    May 21, 2010 at 22:11

    Thanks for Physaxe... but... bummer, the main setup I was envisioning uses joints, and now I see physaxe doesn't support them. :-S

    What are the possibilities of incorporating joint support? Like Brian's submission?

Name : Email : Website : Message :