gpt4 book ai didi

c++ - box2d:在 ResetMassData 上获取 SIGABRT,b2Assert(m_I > 0.0f) - 中心惯性、质心

转载 作者:行者123 更新时间:2023-11-30 02:46:00 25 4
gpt4 key购买 nike

我正在尝试制作一个桶体,当另一个特定物体接触到篮子的内部地板时,例如,一个苹果...

我现在遇到的问题是,在将篮子分成 3 种形状后,它现在在构建主体时崩溃:断言失败:(m_I > 0.0f),函数 ResetMassData,文件/Users/damianwilliams/Documents/XCode Projects/Will Replace Old/Kitty Katch/Kitty Katch/libs/Box2D/Dynamics/b2Body.cpp,第 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)
};

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

亲切的问候

最佳答案

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

enter image description here

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

关于c++ - box2d:在 ResetMassData 上获取 SIGABRT,b2Assert(m_I > 0.0f) - 中心惯性、质心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23945735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com