I might be doing something wrong or calling things in an incorrect state, but when I call body1.Merge(body2) I get an exception. Looking at the code the variable body1 and body2 are not set until line 587 but at line 584 body1's fixture count is set.
Code:
public function Merge(other:b2Body):void
{
var f:b2Fixture;
for (f = other.m_fixtureList; f; )
{
var next:b2Fixture = f.m_next;
// Remove fixture
other.m_fixtureCount--;
// Add fixture
f.m_next = m_fixtureList;
m_fixtureList = f;
m_fixtureCount++;
f.m_body = body2; // << body2 isn't set either, this doesn't seem right
f = next;
}
body1.m_fixtureCount = 0; // << problem line
// Recalculate velocities
var body1:b2Body = this;
var body2:b2Body = other;
// Compute consistent velocites for new bodies based on cached velocity
var center1:b2Vec2 = body1.GetWorldCenter();
var center2:b2Vec2 = body2.GetWorldCenter();
var velocity1:b2Vec2 = body1.GetLinearVelocity().Copy();
var velocity2:b2Vec2 = body2.GetLinearVelocity().Copy();
var angular1:Number = body1.GetAngularVelocity();
var angular:Number = body2.GetAngularVelocity();
// TODO
body1.ResetMassData();
SynchronizeFixtures();
}
{
var f:b2Fixture;
for (f = other.m_fixtureList; f; )
{
var next:b2Fixture = f.m_next;
// Remove fixture
other.m_fixtureCount--;
// Add fixture
f.m_next = m_fixtureList;
m_fixtureList = f;
m_fixtureCount++;
f.m_body = body2; // << body2 isn't set either, this doesn't seem right
f = next;
}
body1.m_fixtureCount = 0; // << problem line
// Recalculate velocities
var body1:b2Body = this;
var body2:b2Body = other;
// Compute consistent velocites for new bodies based on cached velocity
var center1:b2Vec2 = body1.GetWorldCenter();
var center2:b2Vec2 = body2.GetWorldCenter();
var velocity1:b2Vec2 = body1.GetLinearVelocity().Copy();
var velocity2:b2Vec2 = body2.GetLinearVelocity().Copy();
var angular1:Number = body1.GetAngularVelocity();
var angular:Number = body2.GetAngularVelocity();
// TODO
body1.ResetMassData();
SynchronizeFixtures();
}