|
Tutorial
Quick start to use physaxe
Featured InstallationIn order to use physaxe, you need to install the following :
World, Body, ShapeUsing physaxe is pretty easy. First you have to create a World which is a virtual space for the simulation. A world can contain several Body instances and each body can contain several Shape instances. Here's a haXe class that creates for example a world with three bodies and one floor : class Test {
static var world : phx.World;
static function main() {
// define the size of the world
var size = new phx.col.AABB(-1000,-1000,1000,1000);
// create the broadphase : this is the algorithm used to optimize collision detection
var bf = new phx.col.SortedList();
// initialize the world
world = new phx.World(size,bf);
// create one 50x50 box body at x=210,y=-50
var b1 = new phx.Body(210,-50);
b1.addShape( phx.Shape.makeBox(50,50) );
// create one 30 radius circle at x=200,y=250
var b2 = new phx.Body(200,250);
b2.addShape( new phx.Circle(30,new phx.Vector(0,0)) );
// create one 20x20 box body at x=100,y=270
var b3 = new phx.Body(100,270);
b3.addShape( phx.Shape.makeBox(20,20) );
// add the created bodies to the world
world.addBody(b1);
world.addBody(b2);
world.addBody(b3);
// creates a 270x50 box at x=0,y=280
var floor = phx.Shape.makeBox(270,50,0,280);
// the floor is static, it can't move
world.addStaticShape(floor);
// setup gravity
world.gravity = new phx.Vector(0,0.9);
// for every frame, call the loop method
flash.Lib.current.addEventListener(flash.events.Event.ENTER_FRAME,loop);
}
static function loop(_) {
// update the world
world.step(1,20);
// clear the graphics
var g = flash.Lib.current.graphics;
g.clear();
// draw the world
var fd = new phx.FlashDraw(g);
fd.drawCircleRotation = true;
fd.drawWorld(world);
}
}You can compile this code with the following test.hxml file : -swf test.swf -swf-version 9 -swf-header 400:300:30 -main Test -lib physaxe The following shapes are available :
SteppingEach call to world.step will simulate the physics of the bodies :
After a call to world.step, the Body x and y fields contain the body position and the a field the body rotation (in radiants). You can then use these informations to update your graphics, for example set your MovieClip position to the virtual Body one. You can also use phx.FlashDraw which can draw the whole world. Please notice that the whole world is redrawn everytime so this is quite slow if a lot of objects are involved. CustomizingThe following things can be customized :
AS3 VersionIf you are using AS3 and want to give physaxe a try, you can generate the AS3 sourcecode from the haXe source code by running the following commands : haxe -lib physaxe -as3 phx -main Main --no-inline mxmlc -output phx.swf phx/__main__.as The first command will generate the AS3 sourcecode in the phx directory, the second one will compile the AS3 sourcecode as phx.swf. Since the code is compiled with Adobe MXMLC, all haXe compiler optimizations are disabled and the resulting SWF speed will be reduced. ThanksThanks to Box2D author Erin Catto for his great software and to Richard Jewson (Glaze) since I started working on Physaxe based on a port of Glaze to haXe, with heavy changes and optimizations. |
Are there any other tutorials for this? I am not the most advanced Flash programmer in the world, and this tutorial doesn't give the intermediate flash person enough information. (I know you probably did this in your spare time and I'm totally not bitching). I'm not sure where to put my test.swf, and I have no idea how to compile a hxml file. Also, im working with AS3 and I tried to run the AS3 sourcecode commands and they do not work (using osx's terminal). Are you planning on making a more detailed tutorial in the future, or should I just go ahead and cry myself to sleep? haha!
> I'm not sure where to put my test.swf, and I have no idea how to compile a hxml file
test.swf will be compiled from test.hxml with this command: haxe test.hxml
It's written in haXe, not ActionScript?. You compile haXe into SWF.
Here are the instructions for dummies like me ;).
May i ask why i cannot get accurate results when i render my own movie clips? The objects seem to not collide like they should.
How do I detect the 'receiving body' onCollision? Is there a callback where I can do world.removeBody(receivingObject)?
I would sure like some getting started tutorials and example hx files.
Thank you William!
I am also having problems getting correct collisions. Shapes seems to go into each other as if they were made of jelly or elastic instead hitting the edge and stopping or bouncing. I am making a little tank on blocks that shoots projectiles and it seems like half the time a solid wall of blocks will stop a projectile, and the other half of the time the projectile will go straight through the wall. I am using the FlashDraw? as well and it appears that the shapes are actually forming a solid wall of boxes, so I am confused why this would happen. I just want to get results similar to what I see in the demo.
Set frame rate of swf to 100 or above and change: world.gravity = new phx.Vector(0,0.15); // or less
this should work... :)