Tuesday, October 18, 2011

Box2D Pixel Smash... Or not.

Hey guys,

A while ago I came across this awesome little program made by Photon Storm called Pixel Smash, which is essentially a really cool way to mix pixel 8-bit like games with modern day graphics. Check it out here, as you can see, there are quite a number of possibilities that come to mind when you've got what is essentially a destructible pixel environment (think Modern Warfare 2D or similar) how cool!!?

One thing that this particular demo got me on to was of course Nape, as this is the engine that is powering it. However, as many flash developers are aware, the major physics engine of favor in and among game titles is of course Box2D, so I went about porting the Nape Pixel Smash example over to Box2D, with outstandingly disappointing results, I use a simple plugin class called "stats" which was developed by a guy called Mr Doob, and can be githubbed here, which gives you an awesome looking run down of what framerate you're running, how many objects you've got in your scene and also monitors memory and even refresh rates.

Here's the annoying thing though, the Pixel Smash Nape demo gave me a steady 60fps, with 1017 objects and a refresh rate of around 17ms. Now, when I run my Box2D version of it:

Suffice to say, pretty disappointing, and this is with an image that only generates 236 objects! For the purpose of this I've opened up a github account and y'all can browse and pickup the code there.


Be gentle with me, still learning all the stuff that goes with github, so I hope it's all working.

Anyway, basically, the major problem with the Box2D version (I think) is that when the objects spawn into the world, they are all colliding off of one another. So if every object has 8 objects surrounding it, all colliding together, then that's a problem and very computationally hard.

So, with that, I am still at a loss as to how to improve the performance of the Box2D version, if you feel so inclined, feel free to grab the code off of github. And remember, credit given where it's due, this isn't my code to start with, I simple ported it. It's really Rich Daveys' from Photon Storm, I just played with it and got it to Box2D. I'm now in the process of re-writing the code in Nape with the most recent milestone, and will probably end up making a little movie out of it, we shall see.

In the mean time, yeah, get on it! Github it up people!!!


Friday, October 14, 2011

Nape and Flash Develop - Tutorial 1: Hello Nape World

Hey guys, Just made another movie for ya'll, this one is a implementation of the much lighter physics engine Nape, here it is! Let me know your thoughts!

 import flash.display.Sprite;
 import flash.events.Event;
 import nape.geom.Vec2;
 import nape.phys.Body;
 import nape.phys.BodyType;
 import nape.shape.Polygon;
 import nape.space.Space;
 import nape.util.ShapeDebug;
  * ...
  * @author Sp Games
 public class Main extends Sprite 
  public function Main():void 
   var space:Space = new Space(new Vec2(0, 600));
   var debug:ShapeDebug = new ShapeDebug(600, 600, 0x333333);
   var border:Body = new Body(BodyType.STATIC);
    border.shapes.add(new Polygon(Polygon.rect(0, 0, -40, 600)));
    border.shapes.add(new Polygon(Polygon.rect(600,0,40,600)));
    border.shapes.add(new Polygon(Polygon.rect(0,0,600,-40)));
    border.shapes.add(new Polygon(Polygon.rect(0, 600, 600, 40)));
   border.space = space;
   var block:Polygon = new Polygon(Polygon.box(50, 50));
   var body:Body = new Body(BodyType.DYNAMIC);
   body.position.setxy(stage.stageWidth / 2, stage.stageHeight / 2);
   body.space = space;
   addEventListener(Event.ENTER_FRAME, function (_:Event):void {
    space.step(1 / stage.frameRate, 10, 10);
Thanks again guys.

Thursday, October 13, 2011

Tutorial 1: Hello Box World!

Hey Team, Here is the first in my series of Flash Develop Tutorials, this is entitled: Box2D Hello Box World! Super excited to be hearing my voice on the YouTubes. It was only a matter of time...

  Here is the source code that you will end up with once you have finished this tutorial. In the future, and as the tutorials get bigger, I'll get a github account to upload to otherwise my blogs are going to get even longer :O.
 import Box2D.Collision.Shapes.b2CircleShape;
 import Box2D.Common.Math.b2Vec2;
 import Box2D.Dynamics.b2BodyDef;
 import Box2D.Dynamics.b2Body;
 import Box2D.Dynamics.b2DebugDraw;
 import Box2D.Dynamics.b2FixtureDef;
 import Box2D.Dynamics.b2World;
 import flash.display.Sprite;
 import flash.events.Event;
  * ...
  * @author Sp Games
 public class Main extends Sprite 
  public var world_scale:Number = 30;
  public var world:b2World = new b2World(new b2Vec2(0, 9.8), true);
  public function Main():void 
   addEventListener(Event.ENTER_FRAME, update);
  public function add_circle():void 
   var my_body:b2BodyDef = new b2BodyDef();
   my_body.position.Set((stage.stageWidth / 2) / world_scale, 
                                                        (stage.stageHeight / 2) / world_scale);
   my_body.type = b2Body.b2_dynamicBody;
   var my_circle:b2CircleShape = new b2CircleShape(10 / world_scale);
   var my_fixture:b2FixtureDef = new b2FixtureDef();
   my_fixture.shape = my_circle;
   my_fixture.density = 1.0;
   my_fixture.friction = 1;
   var body:b2Body = world.CreateBody(my_body);
  public function debug_draw():void {    
   var debug_draw:b2DebugDraw = new b2DebugDraw();
   var debug_sprite:Sprite = new Sprite();
  private function update(e:Event):void 
   world.Step(1 / 30, 10, 10);