box2d中为何摧毁b2World时仅摧毁fixture,而不会摧毁body和joint;而摧毁body时,依附其上的fixture和joint都会自动被摧毁

box2d中为什么摧毁b2World时仅摧毁fixture,而不会摧毁body和joint;而摧毁body时,依附其上的fixture和joint都会自动被摧毁
本帖最后由 szuzsq 于 2015-04-29 10:50:19 编辑
如题....

b2World::~b2World()
{
// Some shapes allocate using b2Alloc.
b2Body* b = m_bodyList;
while (b)
{
b2Body* bNext = b->m_next;

b2Fixture* f = b->m_fixtureList;
while (f)
{
b2Fixture* fNext = f->m_next;
f->m_proxyCount = 0;
                        //仅看到摧毁fixture的调用-------------------------------------------------------------
f->Destroy(&m_blockAllocator);
f = fNext;
}

b = bNext;
}
}





void b2World::DestroyBody(b2Body* b)
{
b2Assert(m_bodyCount > 0);
b2Assert(IsLocked() == false);
if (IsLocked())
{
return;
}

// Delete the attached joints.
b2JointEdge* je = b->m_jointList;
while (je)
{
b2JointEdge* je0 = je;
je = je->next;

if (m_destructionListener)
{
m_destructionListener->SayGoodbye(je0->joint);
}
                //摧毁joint-----------------------------------------------------------------------------------------
DestroyJoint(je0->joint);

b->m_jointList = je;
}
b->m_jointList = NULL;

// Delete the attached contacts.
b2ContactEdge* ce = b->m_contactList;
while (ce)
{
b2ContactEdge* ce0 = ce;
ce = ce->next;
m_contactManager.Destroy(ce0->contact);
}
b->m_contactList = NULL;

// Delete the attached fixtures. This destroys broad-phase proxies.
b2Fixture* f = b->m_fixtureList;
while (f)
{
b2Fixture* f0 = f;
f = f->m_next;

if (m_destructionListener)
{
m_destructionListener->SayGoodbye(f0);
}

f0->DestroyProxies(&m_contactManager.m_broadPhase);
                //摧毁fixture--------------------------------------------------------------------------------------------------------
f0->Destroy(&m_blockAllocator);
f0->~b2Fixture();
m_blockAllocator.Free(f0, sizeof(b2Fixture));

b->m_fixtureList = f;
b->m_fixtureCount -= 1;
}
b->m_fixtureList = NULL;
b->m_fixtureCount = 0;

// Remove world body list.
if (b->m_prev)
{
b->m_prev->m_next = b->m_next;
}

if (b->m_next)
{
b->m_next->m_prev = b->m_prev;
}

if (b == m_bodyList)
{
m_bodyList = b->m_next;
}

--m_bodyCount;
b->~b2Body();
m_blockAllocator.Free(b, sizeof(b2Body));
}

------解决思路----------------------
 If obj is a Component it will remove the component from the GameObject and destroy it. If obj is a GameObject it will destroy the GameObject, all its components and all transform children of the GameObject. 


是不是和对象的类型有关系吧,我看API上关于Destroy方法解释如上,如果是component的话,只删本身,如果是GameObject的话,就是删除所有。