HelloBoxWorld code attached. When I run this I get random stutters showing 100% CPU. Its very random. Sometimes I can run through the 80 boxes without a hitch. Sometimes it stutters immediately. Its more like a "stall" - if I don't close the tested movie right away, it leads to a Flash IDE crash.
The only major edit I made was pulling out x,y,w,h values for the addCrate function.
Did I miss something here?
The only major edit I made was pulling out x,y,w,h values for the addCrate function.
Did I miss something here?
Code:
package {
import flash.display.MovieClip;
import Box2D.Dynamics.b2World;
import Box2D.Collision.b2AABB;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2Body;
import flash.events.Event;
import flash.display.Sprite;
import Box2D.Dynamics.b2DebugDraw;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
public class HelloBoxWorld extends MovieClip {
protected var _world:b2World;
private static const RATIO:Number = 40;
private var _timeStep:Number = 1.0 / 40.0;
private var _velocityIterations:int = 6;
private var _positionIterations:int = 2;
private var _nextCrate:int;
public function HelloBoxWorld() {
if (stage) {
init();
} else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init():void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
setupWorld();
createWallsAndFloor();
setupDebugDraw();
addCrate();
addEventListener(Event.ENTER_FRAME, newFrameEvent);
}
private function setupDebugDraw():void {
var sprite:Sprite = new Sprite();
this.addChild(sprite);
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.SetSprite(sprite);
debugDraw.SetDrawScale(RATIO);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
debugDraw.SetLineThickness(2.0);
debugDraw.SetFillAlpha(0.6);
_world.SetDebugDraw(debugDraw);
}
private function newFrameEvent(e:Event):void {
_world.Step(_timeStep, _velocityIterations, _positionIterations);
if (_nextCrate-- <= 0 && _world.GetBodyCount() < 80) {
addRandomCrate();
_nextCrate = 5;
}
_world.ClearForces();
_world.DrawDebugData();
}
private function addRandomCrate():void {
addCrate(randomInt(15,530),randomInt(-100,-10),randomInt(0,60),randomInt(5,40),randomInt(5,40));
}
private function setupWorld():void {
var gravity:b2Vec2 = new b2Vec2(0,9.8);
var ignoreSleeping:Boolean = true;
_world = new b2World(gravity,ignoreSleeping);
//_world.SetContactListener(new ContactListener());
}
private function createWallsAndFloor():void {
var w = stage.stageWidth/2;
var h = 5.0/2;
var x = stage.stageWidth/2;
var y = stage.stageHeight - h;
addWall(x,y,w,h);
w = 5.0/2;
h = stage.stageHeight/2;
x = w;
y = stage.stageHeight/2;
addWall(x,y,w,h);
w = 5.0/2;
h = stage.stageHeight/2;
x = stage.stageWidth - w;
y = stage.stageHeight/2;
addWall(x,y,w,h);
}
private function addWall(x=10.0,y=10.0,w=10.0,h=10.0):void {
/*var w = stage.stageWidth/2;
var h = 5.0/2;
var x = stage.stageWidth/2;
var y = stage.stageHeight - h - 5.0;*/
//1. Define a body with a position
var groundBodyDef:b2BodyDef = new b2BodyDef();
//x,y of centre. set as integers, not floats
groundBodyDef.position.Set(x / RATIO, y/ RATIO);
//2. Use the world object to create the body using the body definition above
var groundBody = _world.CreateBody(groundBodyDef);
//3. Define fixtures with a shape, friction, density, etc.
var groundBox:b2PolygonShape = new b2PolygonShape();
//width ,height. set as integers, not floats
groundBox.SetAsBox(w / RATIO, h / RATIO);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = groundBox;
fixtureDef.density = 0.0; //0.0 by default
fixtureDef.restitution = 0.3; //0.0 by default
fixtureDef.friction = 0.5;
groundBody.CreateFixture(fixtureDef);
//trace ("My world has "+_world.GetBodyCount()+" bodies");
}
private function addCrate(x=300.0,y=100.0,w=10.0,h=50.0,r=30.0):void {
/*var x = 300.0;
var y = 100.0;
var w = 10.0;
var h = 50.0;
var r = 30;*/
//moving object
var bodyDef:b2BodyDef = new b2BodyDef();
//set the type of body here
bodyDef.type = b2Body.b2_dynamicBody; //b2_dynamicBody is a property of b2Body. !manual is confusing here.
bodyDef.position.Set(x / RATIO, y / RATIO); // x/y
bodyDef.angle = r * Math.PI / 180;
var body:b2Body = _world.CreateBody(bodyDef);
var dynamicBox:b2PolygonShape = new b2PolygonShape();
dynamicBox.SetAsBox(w / RATIO, h / RATIO); // w/h
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = dynamicBox;
fixtureDef.density = 0.8;
fixtureDef.friction = 0.3;
fixtureDef.restitution = 0.3; //bouncyness
body.CreateFixture(fixtureDef);
}
private function randomInt(low:int,high:int):int {
var lowVal;
var highVal;
if (low <= high) {
lowVal = low;
highVal = high;
} else {
lowVal = high;
highVal = low;
}
return lowVal + Math.floor(Math.random() * (highVal-lowVal+1));
}
}
}
import flash.display.MovieClip;
import Box2D.Dynamics.b2World;
import Box2D.Collision.b2AABB;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2Body;
import flash.events.Event;
import flash.display.Sprite;
import Box2D.Dynamics.b2DebugDraw;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
public class HelloBoxWorld extends MovieClip {
protected var _world:b2World;
private static const RATIO:Number = 40;
private var _timeStep:Number = 1.0 / 40.0;
private var _velocityIterations:int = 6;
private var _positionIterations:int = 2;
private var _nextCrate:int;
public function HelloBoxWorld() {
if (stage) {
init();
} else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init():void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
setupWorld();
createWallsAndFloor();
setupDebugDraw();
addCrate();
addEventListener(Event.ENTER_FRAME, newFrameEvent);
}
private function setupDebugDraw():void {
var sprite:Sprite = new Sprite();
this.addChild(sprite);
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.SetSprite(sprite);
debugDraw.SetDrawScale(RATIO);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
debugDraw.SetLineThickness(2.0);
debugDraw.SetFillAlpha(0.6);
_world.SetDebugDraw(debugDraw);
}
private function newFrameEvent(e:Event):void {
_world.Step(_timeStep, _velocityIterations, _positionIterations);
if (_nextCrate-- <= 0 && _world.GetBodyCount() < 80) {
addRandomCrate();
_nextCrate = 5;
}
_world.ClearForces();
_world.DrawDebugData();
}
private function addRandomCrate():void {
addCrate(randomInt(15,530),randomInt(-100,-10),randomInt(0,60),randomInt(5,40),randomInt(5,40));
}
private function setupWorld():void {
var gravity:b2Vec2 = new b2Vec2(0,9.8);
var ignoreSleeping:Boolean = true;
_world = new b2World(gravity,ignoreSleeping);
//_world.SetContactListener(new ContactListener());
}
private function createWallsAndFloor():void {
var w = stage.stageWidth/2;
var h = 5.0/2;
var x = stage.stageWidth/2;
var y = stage.stageHeight - h;
addWall(x,y,w,h);
w = 5.0/2;
h = stage.stageHeight/2;
x = w;
y = stage.stageHeight/2;
addWall(x,y,w,h);
w = 5.0/2;
h = stage.stageHeight/2;
x = stage.stageWidth - w;
y = stage.stageHeight/2;
addWall(x,y,w,h);
}
private function addWall(x=10.0,y=10.0,w=10.0,h=10.0):void {
/*var w = stage.stageWidth/2;
var h = 5.0/2;
var x = stage.stageWidth/2;
var y = stage.stageHeight - h - 5.0;*/
//1. Define a body with a position
var groundBodyDef:b2BodyDef = new b2BodyDef();
//x,y of centre. set as integers, not floats
groundBodyDef.position.Set(x / RATIO, y/ RATIO);
//2. Use the world object to create the body using the body definition above
var groundBody = _world.CreateBody(groundBodyDef);
//3. Define fixtures with a shape, friction, density, etc.
var groundBox:b2PolygonShape = new b2PolygonShape();
//width ,height. set as integers, not floats
groundBox.SetAsBox(w / RATIO, h / RATIO);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = groundBox;
fixtureDef.density = 0.0; //0.0 by default
fixtureDef.restitution = 0.3; //0.0 by default
fixtureDef.friction = 0.5;
groundBody.CreateFixture(fixtureDef);
//trace ("My world has "+_world.GetBodyCount()+" bodies");
}
private function addCrate(x=300.0,y=100.0,w=10.0,h=50.0,r=30.0):void {
/*var x = 300.0;
var y = 100.0;
var w = 10.0;
var h = 50.0;
var r = 30;*/
//moving object
var bodyDef:b2BodyDef = new b2BodyDef();
//set the type of body here
bodyDef.type = b2Body.b2_dynamicBody; //b2_dynamicBody is a property of b2Body. !manual is confusing here.
bodyDef.position.Set(x / RATIO, y / RATIO); // x/y
bodyDef.angle = r * Math.PI / 180;
var body:b2Body = _world.CreateBody(bodyDef);
var dynamicBox:b2PolygonShape = new b2PolygonShape();
dynamicBox.SetAsBox(w / RATIO, h / RATIO); // w/h
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = dynamicBox;
fixtureDef.density = 0.8;
fixtureDef.friction = 0.3;
fixtureDef.restitution = 0.3; //bouncyness
body.CreateFixture(fixtureDef);
}
private function randomInt(low:int,high:int):int {
var lowVal;
var highVal;
if (low <= high) {
lowVal = low;
highVal = high;
} else {
lowVal = high;
highVal = low;
}
return lowVal + Math.floor(Math.random() * (highVal-lowVal+1));
}
}
}