Um colega meu, “Ian Liu” do grupo de jogos me apresentou uma engine fisica 2D em flash para haXe: Physaxe. A proposta dele é que o grupo destinado a pesquisar desenvolvimento de jogos em 2D busque soluções que incorporem coisas assim.
Parafraseando o próprio autor: “Um demo vale mais que 1000 palavras”.
Aqui em baixo, você pode testar um demo usando a tal engine: Use as teclas 1-8 para alterar o demo e clique em algum lugar do objeto abaixo para atirar blocos.
(Para saber mais sobre o sistema, clique no link “Continue lendo…”)
O interessante é q o binário gerado aqui é de apenas 28KB. Minúsculo e bem otimizado.
Para quese possa ver o consumo da CPU, aperte a tecla D para parar de desenhar e verifique no gerenciador de recursos do seu sistema operacional, o consumo do game funcionando e sem funcionar.
Outro exemplo interessante que vem com o código fonte é esse de gravidade:
Para rodar esse demo, faça isso:
- Instale haXe
- Execute o comando
haxelib install physaxepara instalar a biblioteca do Physaxe. - Copie e cole o código
Test.hx - Excecute:
haxe -swf9 test.swf -main Test -lib physaxepara compilar - Boa diversão!
Codigo:
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()); } }
