【问题标题】:box2d: Getting a SIGABRT on ResetMassData, b2Assert(m_I > 0.0f) - Center inertia, centre of massbox2d:在 ResetMassData 上获取 SIGABRT,b2Assert(m_I > 0.0f) - 中心惯性,质心
【发布时间】:2014-05-30 01:39:57
【问题描述】:

我正在尝试制作一个桶体,当另一个特定的物体接触篮子的内层时,它会做出响应,例如,一个苹果......

我现在遇到的问题是,在将篮子分成 3 个形状后,它现在在构建主体时崩溃:Assertion failed: (m_I > 0.0f), function ResetMassData, file /Users/damianwilliams/Documents/XCode Projects/Will Replace Old/Kitty Katch/Kitty Katch/libs/Box2D/Dynamics/b2Body.cpp, line 319.

但是,如果我将主体声明为静态主体,则不会发生这种情况...

如果我只有一个(任何一个)侧面和底部要添加到身体,这也不会发生!

这是构建它的所有代码,我猜这是一个简单的修复,但我看不到它:(

@implementation HelloWorldLayer
{
    CCSprite *_theBag;
    b2Body *_basketBody;
    b2Fixture *_basketLeftBodyFix;
    b2Fixture *_basketRightBodyFix;
    b2Fixture *_basketBottomFixture;
}

-(id) init { if( (self=[super initWithColor:ccc4(50, 180, 220, 255)]) )
{
    // Create the Basket body--------------------------
    _theBag = [CCSprite spriteWithFile:@"Basket.png"];
    _theBag.opacity = 30;
    _theBag.scale = 0.5;
    [self addChild:_theBag z:35];

    b2BodyDef theBasketBodyDef;
    theBasketBodyDef.type = b2_staticBody;
    theBasketBodyDef.position.Set(220/PTM_RATIO, 150/PTM_RATIO);
    theBasketBodyDef.userData = (__bridge void*)_theBag;
    theBasketBodyDef.angularDamping = 25.0;
    _basketBody = _world->CreateBody(&theBasketBodyDef);

    // Create basket shape
    int scalledPTMRatio = PTM_RATIO * (10 / (_theBag.scale * 10)); // Needed if the sprite image is scalled...
    int numLeftVerts = 5;
    b2Vec2 leftVerts[] = {
        b2Vec2(-98.4f / scalledPTMRatio, 51.2f / scalledPTMRatio),
        b2Vec2(-65.6f / scalledPTMRatio, 38.6f / scalledPTMRatio),
        b2Vec2(-59.6f / scalledPTMRatio, -73.0f / scalledPTMRatio),
        b2Vec2(-71.0f / scalledPTMRatio, -68.8f / scalledPTMRatio),
        b2Vec2(-97.8f / scalledPTMRatio, 51.1f / scalledPTMRatio)
    };

    b2PolygonShape basketLeftShape;
    basketLeftShape.Set(leftVerts, numLeftVerts);

     int numRightVerts = 5;
     b2Vec2 rightVerts[] = {
        b2Vec2(98.2f / scalledPTMRatio, 43.7f / scalledPTMRatio),
        b2Vec2(56.6f / scalledPTMRatio, 36.6f / scalledPTMRatio),
        b2Vec2(45.9f / scalledPTMRatio, -80.0f / scalledPTMRatio),
        b2Vec2(53.8f / scalledPTMRatio, -78.4f / scalledPTMRatio),
        b2Vec2(98.5f / scalledPTMRatio, 41.2f / scalledPTMRatio)
    };

    b2PolygonShape basketRightShape;
    basketRightShape.Set(rightVerts, numRightVerts);

    // Create shape def and add to body
    b2FixtureDef theBasketLeftShapeDef;
     theBasketLeftShapeDef.shape = &basketLeftShape;
     theBasketLeftShapeDef.density = 5.0f;
    theBasketLeftShapeDef.friction = 0.9f;
    theBasketLeftShapeDef.restitution = 0.2f;
    _basketLeftBodyFix = _basketBody->CreateFixture(&theBasketLeftShapeDef);

    b2FixtureDef theBasketRightShapeDef;
    theBasketRightShapeDef.shape = &basketRightShape;
    theBasketRightShapeDef.density = 5.0f;
    theBasketRightShapeDef.friction = 0.9f;
    theBasketRightShapeDef.restitution = 0.2f;
    _basketRightBodyFix = _basketBody->CreateFixture(&theBasketRightShapeDef);

    // Create the bottom of the basket
    b2PolygonShape basketBottomShape;
    basketBottomShape.SetAsBox(0.7, 0.1, b2Vec2(-0.1, -1), 0.0);

    b2FixtureDef basketBottomShapeDef;
    basketBottomShapeDef.shape = &basketBottomShape;
    basketBottomShapeDef.density = 10.0f;
    basketBottomShapeDef.friction = 1.0f;
    basketBottomShapeDef.restitution = 0.0f;
    MyUserData *thebasketBottomUserData = (MyUserData*)malloc(sizeof(MyUserData));
    thebasketBottomUserData->myTag = 4;
    basketBottomShapeDef.userData = thebasketBottomUserData;
    _basketBottomFixture = _basketBody ->CreateFixture(&basketBottomShapeDef);
}

这是b2Body.cpp文件中发生崩溃的地方...

if (m_I > 0.0f && (m_flags & e_fixedRotationFlag) == 0)
{
    // Center the inertia about the center of mass.
    m_I -= m_mass * b2Dot(localCenter, localCenter);
    b2Assert(m_I > 0.0f);
    m_invI = 1.0f / m_I;
}

回溯..

* thread #1: tid = 0x2ba286, 0x02ae8952 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x02ae8952 libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x02aac167 libsystem_pthread.dylib`pthread_kill + 101
frame #2: 0x0281b9c9 libsystem_sim_c.dylib`abort + 127
frame #3: 0x027e653b libsystem_sim_c.dylib`__assert_rtn + 284
* frame #4: 0x001020d8 Kitty Katch`b2Body::ResetMassData(this=0x0f2cd1e0) + 744 at b2Body.cpp:319
frame #5: 0x00101d30 Kitty Katch`b2Body::SetType(this=0x0f2cd1e0, type=b2_dynamicBody) + 208 at b2Body.cpp:128
frame #6: 0x000b8cdc Kitty Katch`-[HelloWorldLayer startGame](self=0x0b7958c0, _cmd=0x0a910d1f) + 60 at HelloWorldLayer.mm:340
frame #7: 0x000ba41d Kitty Katch`-[HelloWorldLayer startGameStub:](self=0x0b7958c0, _cmd=0x0013862f, sender=0x0c782e70) + 77 at HelloWorldLayer.mm:471
frame #8: 0x0240e82b libobjc.A.dylib`-[NSObject performSelector:withObject:] + 70
frame #9: 0x0006d367 Kitty Katch`__49-[CCMenuItemLabel initWithLabel:target:selector:]_block_invoke(.block_descriptor=<unavailable>, sender=0x0c782e70) + 71 at CCMenuItem.m:193
frame #10: 0x0006cc5c Kitty Katch`-[CCMenuItem activate](self=0x0c782e70, _cmd=0x00c32eb8) + 108 at CCMenuItem.m:135
frame #11: 0x0006d890 Kitty Katch`-[CCMenuItemLabel activate](self=0x0c782e70, _cmd=0x00c32eb8) + 160 at CCMenuItem.m:246
frame #12: 0x001187e9 Kitty Katch`-[CCMenu ccTouchEnded:withEvent:](self=0x0c7844f0, _cmd=0x0012f7fa, touch=0x0b791aa0, event=0x0b685d60) + 297 at CCMenu.m:228
frame #13: 0x0240e880 libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 77
frame #14: 0x001261ae Kitty Katch`-[CCTouchDispatcher touches:withEvent:withTouchType:](self=0x0c39e2e0, _cmd=0x0013b5e2, touches=0x0c3b9130, event=0x0b685d60, idx=2) + 1534 at CCTouchDispatcher.m:292
frame #15: 0x00126d83 Kitty Katch`-[CCTouchDispatcher touchesEnded:withEvent:](self=0x0c39e2e0, _cmd=0x00c0e017, touches=0x0c3b9130, event=0x0b685d60) + 115 at CCTouchDispatcher.m:366
frame #16: 0x000b44be Kitty Katch`-[CCGLView touchesEnded:withEvent:](self=0x0c397440, _cmd=0x00c0e017, touches=0x0c3b9130, event=0x0b685d60) + 110 at CCGLView.m:349
frame #17: 0x0047dddd UIKit`-[UIWindow _sendTouchesForEvent:] + 852
frame #18: 0x0047e9d1 UIKit`-[UIWindow sendEvent:] + 1117
frame #19: 0x004505f2 UIKit`-[UIApplication sendEvent:] + 242
frame #20: 0x0043a353 UIKit`_UIApplicationHandleEventQueue + 11455
frame #21: 0x0344e77f CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
frame #22: 0x0344e10b CoreFoundation`__CFRunLoopDoSources0 + 235
frame #23: 0x0346b1ae CoreFoundation`__CFRunLoopRun + 910
frame #24: 0x0346a9d3 CoreFoundation`CFRunLoopRunSpecific + 467
frame #25: 0x0346a7eb CoreFoundation`CFRunLoopRunInMode + 123
frame #26: 0x032375ee GraphicsServices`GSEventRunModal + 192
frame #27: 0x0323742b GraphicsServices`GSEventRun + 104
frame #28: 0x0043cf9b UIKit`UIApplicationMain + 1225
frame #29: 0x00126f06 Kitty Katch`main(argc=1, argv=0xbfffed54) + 134 at main.m:14
frame #30: 0x00002e85 Kitty Katch`start + 53

我还按照其他用户的建议查看了重叠点。有一些不好的地方,但是修复它们并没有帮助解决这个错误。我完全重做了有问题的对象,但错误仍然存​​在......

int scalledPTMRatio = PTM_RATIO ; //scalled...
int numLeftVerts = 6;
b2Vec2 leftVerts[] = {
    b2Vec2(-18.2f / scalledPTMRatio, -17.1f / scalledPTMRatio),
    b2Vec2(-20.6f / scalledPTMRatio, -31.0f / scalledPTMRatio),
    b2Vec2(-29.0f / scalledPTMRatio, -27.7f / scalledPTMRatio),
    b2Vec2(-38.6f / scalledPTMRatio, 20.3f / scalledPTMRatio),
    b2Vec2(-24.3f / scalledPTMRatio, 16.1f / scalledPTMRatio),
    b2Vec2(-21.2f / scalledPTMRatio, -13.2f / scalledPTMRatio)
};

b2PolygonShape basketLeftShape;
basketLeftShape.Set(leftVerts, numLeftVerts);

int numRightVerts = 6;
b2Vec2 rightVerts[] = {
    b2Vec2(14.6f / scalledPTMRatio, -13.0f / scalledPTMRatio),
    b2Vec2(11.6f / scalledPTMRatio, -32.9f / scalledPTMRatio),
    b2Vec2(21.8f / scalledPTMRatio, -31.3f / scalledPTMRatio),
    b2Vec2(39.2f / scalledPTMRatio, 17.1f / scalledPTMRatio),
    b2Vec2(20.4f / scalledPTMRatio, 14.4f / scalledPTMRatio),
    b2Vec2(17.7f / scalledPTMRatio, -10.5f / scalledPTMRatio)
};

我不确定我在寻找什么或为什么会收到错误消息。除了上面引用的错误之外,调试窗口中没有什么有趣的东西,我希望有人可以将我的代码放在上面,看看他们的问题,然后提供有关如何诊断问题并最终帮助我解决问题的说明。

亲切的问候

【问题讨论】:

    标签: c++ ios objective-c cocos2d-iphone box2d


    【解决方案1】:

    我认为这是由于多边形自相交造成的。

    iirc 当多边形顺时针缠绕或面积为零(由于所有点共线)时,您也可以点击此断言。当所有这些事情都正确但多边形太小时,还有另一个类似的断言可以在某处发生。

    【讨论】:

    • 哇......如果这是正确的,向你致敬,先生!因为在测试时,我发现有些物体正在从多边形中掉下来。这个屏幕截图是什么软件,谢谢...!
    • 我不确定是不是这样,但真的希望是这样。我从头开始完全重做这些点,让腰点远离任何活动。但它没有帮助......物体是否可能太小?
    • 点也是顺时针缠绕的。删除最后一点(因为它几乎在第一个之上并且有点多余)并颠倒顺序。
    • 这就是解决方案! - 首先,我了解到最后一行是由框自动插入的……您对我最后一点多余的评论是那里的提示。但是有一件事我被困住了……为什么倒车点会起到刹车的作用?
    • Box2D 中多边形的点应按逆时针顺序给出,并形成一个不超过 8 个顶点的凸面、非自相交多边形。当点顺时针缠绕时,面积计算变为负数。我不想再次将您指向外部站点,但 Box2D 手册列出了所有细节,值得一读:box2d.org/manual.html :) 该软件是 RUBE Box2D 编辑器。
    【解决方案2】:

    这不是原始的“崩溃”,而是断言失败。

    Assert 失败了,我们来看看。

    在运行时,m_I &gt; 0.0ffalse,所以 m_I &lt;= 0.0ftrue。 因为m_I &gt; 0.0f如果为真(它是if的一个条件),我们就扣掉m_mass * b2Dot(localCenter, localCenter) &lt;= 0.0f

    所以要么m_mass &lt;= 0.0f 要么b2Dot(localCenter, localCenter) &lt;= 0.0f。使用调试器快速查看生成的内核将显示它们的值:它们中的任何一个都不应该是负数,并且是,或者您的断言是错误的。

    【讨论】:

    • 调试窗口非常空...它有标准的东西,并且在我的问题中引用了一个...不确定我在寻找什么或为什么我收到错误。我希望有人可以使用我上面的代码,查看问题所在,然后提供有关如何诊断问题并最终解决问题的说明。
    • 调试窗口和控制台窗口真的不是一回事:调试器可以在断言失败之前为您提供变量值(查看回溯)。
    • 是的,我相信我正在查看的是调试窗口,以及变量窗口?说“所有输出”......说(lldb)?我可以在哪里输入 bt 进行回溯? - 稍后将添加回溯到问题...如果这不正确,你能说一下如何得到它吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 2020-09-24
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多