Ok, I managed to get the ragdoll test bed to break (see attached file). It's a different error, but most likely related. Thanks for your help - appreciate it.
Latest update - growable broadphases
FlashThanks Mayo. I think I'm going to have to leave this for next weekend though - it doesn't promise to be a small job.
Hi Borris
After a few hours of cutting back my game to figure out what was causing an error, I think I've nailed it.
In the example below, bodies are added on mouse click and are destroyed when the y value drops below 200.
To get the error you need to do the following:
- Click about 10 times so 10 bodies have been added / destroyed.
- Click twice in the same spot so a new body is added while overlapping an old body.
Basically just keep clicking in the same place really fast.
SWF File
AS File
Note: I am using the latest version from SVN but changed the 'const' variables to 'var' in b2ContactSolver.as on lines 218 and 463 to avoid compile errors.
b2ContactSolver.as
After a few hours of cutting back my game to figure out what was causing an error, I think I've nailed it.
In the example below, bodies are added on mouse click and are destroyed when the y value drops below 200.
To get the error you need to do the following:
- Click about 10 times so 10 bodies have been added / destroyed.
- Click twice in the same spot so a new body is added while overlapping an old body.
Basically just keep clicking in the same place really fast.
Code:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Box2D.Collision::b2BroadPhase/MoveProxy()
at Box2D.Collision.Shapes::b2Shape/http://www.box2d.org/ns/b2internal::Synchronize()
at Box2D.Dynamics::b2Body/http://www.box2d.org/ns/b2internal::SynchronizeShapes()
at Box2D.Dynamics::b2World/http://www.box2d.org/ns/b2internal::Solve()
at Box2D.Dynamics::b2World/Step()
at HelloWorld/Update()
at Box2D.Collision::b2BroadPhase/MoveProxy()
at Box2D.Collision.Shapes::b2Shape/http://www.box2d.org/ns/b2internal::Synchronize()
at Box2D.Dynamics::b2Body/http://www.box2d.org/ns/b2internal::SynchronizeShapes()
at Box2D.Dynamics::b2World/http://www.box2d.org/ns/b2internal::Solve()
at Box2D.Dynamics::b2World/Step()
at HelloWorld/Update()
Code:
package{
import flash.display.Sprite;
import flash.events.Event;
// Classes used in this example
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import flash.events.MouseEvent;
public class HelloWorld extends Sprite {
public var m_world:b2World;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1.0/30.0;
public function HelloWorld(){
// Add event for main loop
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
// Creat world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-1000.0, -1000.0);
worldAABB.upperBound.Set(1000.0, 1000.0);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
// Allow bodies to sleep
var doSleep:Boolean = true;
// Construct a world object
m_world = new b2World(worldAABB, gravity, doSleep);
// set debug draw
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var debug_mc:Sprite = new Sprite();
addChild(debug_mc);
dbgDraw.SetSprite(debug_mc);
dbgDraw.SetDrawScale(30.0);
dbgDraw.SetFillAlpha(0.3);
dbgDraw.SetLineThickness(1.0);
dbgDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
m_world.SetDebugDraw(dbgDraw);
}
public function Update(e:Event):void
{
m_world.Step(m_timeStep, m_iterations, m_iterations);
// Any bodies that have y value above 200 get added to the destoy list
var destroyList:Array = [];
for (var bb:b2Body = m_world.GetBodyList(); bb; bb = bb.GetNext())
{
if (Math.abs(bb.GetPosition().y) > 200 / 30)
{
destroyList.push(bb);
}
}
// Destroy bodies
for each (var destroyBody:b2Body in destroyList)
{
m_world.DestroyBody(destroyBody);
}
}
// Add a circle on mouse click
protected function mouseDownHandler(evt:MouseEvent):void
{
var body:b2Body;
var bodyDef:b2BodyDef = new b2BodyDef();
var circleDef:b2CircleDef;
var radius:Number = 5;
circleDef = new b2CircleDef();
circleDef.radius = radius / 30;
circleDef.density = 1.0;
bodyDef.position.Set(mouseX / 30, 50 / 30);
body = m_world.CreateBody(bodyDef);
body.CreateShape(circleDef);
body.SetMassFromShapes();
}
}
}
import flash.display.Sprite;
import flash.events.Event;
// Classes used in this example
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import flash.events.MouseEvent;
public class HelloWorld extends Sprite {
public var m_world:b2World;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1.0/30.0;
public function HelloWorld(){
// Add event for main loop
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
// Creat world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-1000.0, -1000.0);
worldAABB.upperBound.Set(1000.0, 1000.0);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
// Allow bodies to sleep
var doSleep:Boolean = true;
// Construct a world object
m_world = new b2World(worldAABB, gravity, doSleep);
// set debug draw
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var debug_mc:Sprite = new Sprite();
addChild(debug_mc);
dbgDraw.SetSprite(debug_mc);
dbgDraw.SetDrawScale(30.0);
dbgDraw.SetFillAlpha(0.3);
dbgDraw.SetLineThickness(1.0);
dbgDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
m_world.SetDebugDraw(dbgDraw);
}
public function Update(e:Event):void
{
m_world.Step(m_timeStep, m_iterations, m_iterations);
// Any bodies that have y value above 200 get added to the destoy list
var destroyList:Array = [];
for (var bb:b2Body = m_world.GetBodyList(); bb; bb = bb.GetNext())
{
if (Math.abs(bb.GetPosition().y) > 200 / 30)
{
destroyList.push(bb);
}
}
// Destroy bodies
for each (var destroyBody:b2Body in destroyList)
{
m_world.DestroyBody(destroyBody);
}
}
// Add a circle on mouse click
protected function mouseDownHandler(evt:MouseEvent):void
{
var body:b2Body;
var bodyDef:b2BodyDef = new b2BodyDef();
var circleDef:b2CircleDef;
var radius:Number = 5;
circleDef = new b2CircleDef();
circleDef.radius = radius / 30;
circleDef.density = 1.0;
bodyDef.position.Set(mouseX / 30, 50 / 30);
body = m_world.CreateBody(bodyDef);
body.CreateShape(circleDef);
body.SetMassFromShapes();
}
}
}
SWF File
AS File
Note: I am using the latest version from SVN but changed the 'const' variables to 'var' in b2ContactSolver.as on lines 218 and 463 to avoid compile errors.
b2ContactSolver.as
I have been playing around with the newest release and just started getting the same error as well. I don't think it's necessarily creating bodies in the same space, because I get the error without ever doing that. I think it may actually have to do with destroying bodies. I am messing around with launching balls that destroy blocks, and if I turn off the destroying blocks part I never get the error no matter how many balls I create.
However, the error itself appears happen at any point when you are calling Query on the broadphase. It happens in mine sometimes when I create shapes, and sometimes when I call SetMassFromShapes, maybe other places too.
I will try to make a simple test case to reproduce it soon.
However, the error itself appears happen at any point when you are calling Query on the broadphase. It happens in mine sometimes when I create shapes, and sometimes when I call SetMassFromShapes, maybe other places too.
I will try to make a simple test case to reproduce it soon.
Ok, I think I've fixed it now. Try it out.
Thanks for your help, everyone.
For the record, it had to do with creating shapes while there were multiple spare proxies in the pool. I was boneheadly "moving" bounds by deleting them from one part of the array and inserting them into another, but the deletion targeted correctly.
Thanks for your help, everyone.
For the record, it had to do with creating shapes while there were multiple spare proxies in the pool. I was boneheadly "moving" bounds by deleting them from one part of the array and inserting them into another, but the deletion targeted correctly.
Awesome, I will definitely try this tonight!
I'm fairly knowledgeable about AS3 optimization so I'm willing to lend a hand with that as well. I've done a lot of optimization on my own code and dug into the AVM2 docs quite a bit (I actually wrote a bytecode disassembler!). Flex Builder has a profiler, but using flash.utils.getTimer() is probably a decent way to test.
I've been away from Box2D for a while but I'm getting back into it now, so I should be around more.
EDIT: It does indeed appear to have fixed the bug, thanks again!
I'm fairly knowledgeable about AS3 optimization so I'm willing to lend a hand with that as well. I've done a lot of optimization on my own code and dug into the AVM2 docs quite a bit (I actually wrote a bytecode disassembler!). Flex Builder has a profiler, but using flash.utils.getTimer() is probably a decent way to test.
I've been away from Box2D for a while but I'm getting back into it now, so I should be around more.
EDIT: It does indeed appear to have fixed the bug, thanks again!
Seems to have done the trick here!
No need to thank us Boris, great work.
No need to thank us Boris, great work.
Original thread: https://box2d.org/forum/viewtopic.php?t=2783