Hey guys, if anyone is interested in Pixel stuff, I was wondering if I could enlist your help.
There is this high optimization Physics engine called Nape and a guy from Photon Storm created an awesome little program which creates a physics object out of pixel bitmaps, see it here:
http://sandbox.photonstorm.com/pixelsmash/
He does claim that there is NO optimization done on it, but I can still run the Nape version of it at a steady 30fps.
I've just finished porting the nape version over to Box2D, but get a shocking 2-3fps, here is the main.as code:
I assume that the reason behind it being so heavy is that it generates an individual box2d object for each pixel, and then in the update function it has to iterate through every single object and update it's position so that the sprite can follow it.
Can anyone suggest some ways in which I can increase the FPS of this project?
Thanks!
SC
There is this high optimization Physics engine called Nape and a guy from Photon Storm created an awesome little program which creates a physics object out of pixel bitmaps, see it here:
http://sandbox.photonstorm.com/pixelsmash/
He does claim that there is NO optimization done on it, but I can still run the Nape version of it at a steady 30fps.
I've just finished porting the nape version over to Box2D, but get a shocking 2-3fps, here is the main.as code:
Code:
package
{
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import Box2D.Dynamics.b2Body;
import flash.*;
import flash.display.*;
import flash.events.*;
public class Main extends Sprite
{
[Embed(source = '../assets/face1.png')] private var spritePNG:Class;
//[Embed(source = '../assets/blob.png')] private var spritePNG:Class;
//[Embed(source = '../assets/ilija.png')] private var spritePNG:Class;
public var world_scale:Number = 30;
public var world:b2World = new b2World(new b2Vec2(0, 9.8), true);
public var body_array:Array = new Array;
private var stats:Stats;
private var blockSize:int = 4; // The blockSize controls how large a single pixel is represented as a Nape object
public function Main():void
{
if (stage)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
body_array = [];
makeSpace();
makeSprite();
debug_draw();
stage.quality = StageQuality.LOW;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
addEventListener(Event.ENTER_FRAME, mainLoop);
stats = new Stats();
addChild(stats);
}
private function makeSpace():void
{
//floor
draw_walls(320, 480, 640, 2, false, null);
//left
draw_walls(0, 240, 2, 480, false, null);
//top
draw_walls(320, 0, 640, 2, false, null);
//right
draw_walls(640, 240, 2, 480, false, null);
// The walls are invisible, they just exist in the physics world, there's no visual representation of them
}
private function draw_walls(posx:int, posy:int, width:int, height:int, sensor:Boolean, userData:Sprite):void {
var my_body:b2BodyDef = new b2BodyDef;
my_body.position.Set(posx / world_scale, posy / world_scale);
var my_box:b2PolygonShape = new b2PolygonShape();
var my_fixture:b2FixtureDef = new b2FixtureDef();
my_box.SetAsBox((width/2)/world_scale, (height/2)/world_scale);
my_fixture.shape = my_box;
my_fixture.friction = 1;
var world_body:b2Body = world.CreateBody(my_body);
my_body.userData = userData;
world_body.CreateFixture(my_fixture);
}
private function debug_draw():void {
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
addChild(debug_sprite);
debug_draw.SetSprite(debug_sprite);
debug_draw.SetDrawScale(world_scale);
debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
world.SetDebugDraw(debug_draw);
}
private function makeSprite():void
{
var tempA:Bitmap = new spritePNG;
for (var sy:int = 0; sy < tempA.height; sy++)
{
for (var sx:int = 0; sx < tempA.width; sx++)
{
if (tempA.bitmapData.getPixel32(sx, sy) > 0)
{
var b:Sprite = makePixelSprite(blockSize, tempA.bitmapData.getPixel(sx, sy));
draw_crate(260 + (sx * blockSize), 64 + (sy * blockSize), blockSize, blockSize, false, b);
addChild(b);
}
}
}
}
public function draw_crate(x:Number, y:Number, width:Number, height:Number, boolean:Boolean, userData:Sprite):void
{
var my_body:b2BodyDef = new b2BodyDef;
my_body.position.Set(x / world_scale, y / world_scale);
my_body.type=b2Body.b2_dynamicBody;
var my_box:b2PolygonShape = new b2PolygonShape();
var my_fixture:b2FixtureDef = new b2FixtureDef();
my_box.SetAsBox((width/2)/world_scale, (height/2)/world_scale);
my_fixture.shape = my_box;
my_fixture.friction = 1;
var world_body:b2Body = world.CreateBody(my_body);
world_body.SetUserData(userData);
body_array.push(my_body);
world_body.CreateFixture(my_fixture);
}
private function makePixelSprite(size:int, color:int):Sprite
{
var pixel:Sprite = new Sprite();
pixel.graphics.beginFill(color, 1);
pixel.graphics.drawRect( -(size/2), -(size/2), size, size);
pixel.graphics.endFill();
return pixel;
}
private function mainLoop(event:Event):void
{
world.Step(1 / stage.frameRate, 10, 10);
world.ClearForces();
world.DrawDebugData();
for (var bb:b2Body = world.GetBodyList(); bb; bb = bb.GetNext()){
if (bb.GetUserData() is Sprite){
var sprite:Sprite = bb.GetUserData() as Sprite;
sprite.x = bb.GetPosition().x * 30;
sprite.y = bb.GetPosition().y * 30;
sprite.rotation = bb.GetAngle() * (180/Math.PI);
}
}
}
}
}
{
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import Box2D.Dynamics.b2Body;
import flash.*;
import flash.display.*;
import flash.events.*;
public class Main extends Sprite
{
[Embed(source = '../assets/face1.png')] private var spritePNG:Class;
//[Embed(source = '../assets/blob.png')] private var spritePNG:Class;
//[Embed(source = '../assets/ilija.png')] private var spritePNG:Class;
public var world_scale:Number = 30;
public var world:b2World = new b2World(new b2Vec2(0, 9.8), true);
public var body_array:Array = new Array;
private var stats:Stats;
private var blockSize:int = 4; // The blockSize controls how large a single pixel is represented as a Nape object
public function Main():void
{
if (stage)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
body_array = [];
makeSpace();
makeSprite();
debug_draw();
stage.quality = StageQuality.LOW;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
addEventListener(Event.ENTER_FRAME, mainLoop);
stats = new Stats();
addChild(stats);
}
private function makeSpace():void
{
//floor
draw_walls(320, 480, 640, 2, false, null);
//left
draw_walls(0, 240, 2, 480, false, null);
//top
draw_walls(320, 0, 640, 2, false, null);
//right
draw_walls(640, 240, 2, 480, false, null);
// The walls are invisible, they just exist in the physics world, there's no visual representation of them
}
private function draw_walls(posx:int, posy:int, width:int, height:int, sensor:Boolean, userData:Sprite):void {
var my_body:b2BodyDef = new b2BodyDef;
my_body.position.Set(posx / world_scale, posy / world_scale);
var my_box:b2PolygonShape = new b2PolygonShape();
var my_fixture:b2FixtureDef = new b2FixtureDef();
my_box.SetAsBox((width/2)/world_scale, (height/2)/world_scale);
my_fixture.shape = my_box;
my_fixture.friction = 1;
var world_body:b2Body = world.CreateBody(my_body);
my_body.userData = userData;
world_body.CreateFixture(my_fixture);
}
private function debug_draw():void {
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
addChild(debug_sprite);
debug_draw.SetSprite(debug_sprite);
debug_draw.SetDrawScale(world_scale);
debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
world.SetDebugDraw(debug_draw);
}
private function makeSprite():void
{
var tempA:Bitmap = new spritePNG;
for (var sy:int = 0; sy < tempA.height; sy++)
{
for (var sx:int = 0; sx < tempA.width; sx++)
{
if (tempA.bitmapData.getPixel32(sx, sy) > 0)
{
var b:Sprite = makePixelSprite(blockSize, tempA.bitmapData.getPixel(sx, sy));
draw_crate(260 + (sx * blockSize), 64 + (sy * blockSize), blockSize, blockSize, false, b);
addChild(b);
}
}
}
}
public function draw_crate(x:Number, y:Number, width:Number, height:Number, boolean:Boolean, userData:Sprite):void
{
var my_body:b2BodyDef = new b2BodyDef;
my_body.position.Set(x / world_scale, y / world_scale);
my_body.type=b2Body.b2_dynamicBody;
var my_box:b2PolygonShape = new b2PolygonShape();
var my_fixture:b2FixtureDef = new b2FixtureDef();
my_box.SetAsBox((width/2)/world_scale, (height/2)/world_scale);
my_fixture.shape = my_box;
my_fixture.friction = 1;
var world_body:b2Body = world.CreateBody(my_body);
world_body.SetUserData(userData);
body_array.push(my_body);
world_body.CreateFixture(my_fixture);
}
private function makePixelSprite(size:int, color:int):Sprite
{
var pixel:Sprite = new Sprite();
pixel.graphics.beginFill(color, 1);
pixel.graphics.drawRect( -(size/2), -(size/2), size, size);
pixel.graphics.endFill();
return pixel;
}
private function mainLoop(event:Event):void
{
world.Step(1 / stage.frameRate, 10, 10);
world.ClearForces();
world.DrawDebugData();
for (var bb:b2Body = world.GetBodyList(); bb; bb = bb.GetNext()){
if (bb.GetUserData() is Sprite){
var sprite:Sprite = bb.GetUserData() as Sprite;
sprite.x = bb.GetPosition().x * 30;
sprite.y = bb.GetPosition().y * 30;
sprite.rotation = bb.GetAngle() * (180/Math.PI);
}
}
}
}
}
I assume that the reason behind it being so heavy is that it generates an individual box2d object for each pixel, and then in the update function it has to iterate through every single object and update it's position so that the sprite can follow it.
Can anyone suggest some ways in which I can increase the FPS of this project?
Thanks!
SC