gpt4 book ai didi

objective-c - Box2d Cocos2d 圆与地面接触时崩溃

转载 作者:行者123 更新时间:2023-12-03 15:40:05 25 4
gpt4 key购买 nike

这是我在这里的第一个问题,很抱歉,如果我做错了什么或者这太长了。我一直在阅读this tutorial作者 Ray Wenderlich,我对其进行了修改,使其更平坦并逐渐下坡。基本上我有一个球从颠簸的山上滚下来,但目前球只从上方大约 100 像素下降。当触摸应用程序崩溃时(该应用程序是 Mac Cocos2d Box2d 应用程序)。球码是这样的:

CGSize winSize = [CCDirector sharedDirector].winSize;
self.oeva = [CCSprite spriteWithTexture:[[CCTextureCache sharedTextureCache] addImage:@"Ball.png"]rect:CGRectMake(0, 0, 64, 64)];
_oeva.position = CGPointMake(68, winSize.height/2);
[self addChild:_oeva z:1];
b2BodyDef oevaBodyDef;
oevaBodyDef.type = b2_dynamicBody;
oevaBodyDef.position.Set(68/PTM_RATIO, (winSize.height/2)/PTM_RATIO);
// oevaBodyDef.userData = _oeva;
_oevaBody = world->CreateBody(&oevaBodyDef);
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;

bodyDef.position.Set(60/PTM_RATIO, 400/PTM_RATIO);
bodyDef.userData = _oeva;
b2Body *body = world->CreateBody(&bodyDef);

// Define another box shape for our dynamic body.
b2CircleShape dynamicBox;
dynamicBox.m_radius = 70/PTM_RATIO;//These are mid points for our 1m box

// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);

这很好用。这是地形代码,这也可以正常工作:
-(void)generateTerrainWithWorld: (b2World *) inputWorld: (int) hillSize;{
b2BodyDef bd;
bd.position.Set(0, 0);

body = inputWorld->CreateBody(&bd);

b2PolygonShape shape;
b2FixtureDef fixtureDef;
currentSlope = 0;

CGSize winSize = [CCDirector sharedDirector].winSize;
float xf = 0;
float yf = (arc4random() % 10)+winSize.height/3;
int x = 200;
for(int i = 0; i < maxHillPoints; ++i) {
hillPoints[i] = CGPointMake(xf, yf);
xf = xf+ (arc4random() % x/2)+x/2;
yf = ((arc4random() % 30)+winSize.height/3)-currentSlope;
currentSlope +=10;
}

int hSegments;

for (int i=0; i<maxHillPoints-1; i++) {
CGPoint p0 = hillPoints[i-1];
CGPoint p1 = hillPoints[i];
hSegments = floorf((p1.x-p0.x)/cosineSegmentWidth);
float dx = (p1.x - p0.x) / hSegments;
float da = M_PI / hSegments;
float ymid = (p0.y + p1.y) / 2;
float ampl = (p0.y - p1.y) / 2;

CGPoint pt0, pt1;
pt0 = p0;
for (int j = 0; j < hSegments+1; ++j) {

pt1.x = p0.x + j*dx;
pt1.y = ymid + ampl * cosf(da*j);

fullHillPoints[fullHillPointsCount++] = pt1;

pt0 = pt1;
}

}

b2Vec2 p1v, p2v;
for (int i=0; i<fullHillPointsCount-1; i++) {
p1v = b2Vec2(fullHillPoints[i].x/PTM_RATIO,fullHillPoints[i].y/PTM_RATIO);
p2v = b2Vec2(fullHillPoints[i+1].x/PTM_RATIO,fullHillPoints[i+1].y/PTM_RATIO);
shape.SetAsEdge(p1v, p2v);
body->CreateFixture(&shape, 0);
}

}

但是,当两者发生碰撞时,应用程序就会崩溃。崩溃错误是:

Thread 6 CVDisplayLink: Program received signal: "SIGABRT"



错误发生在 b2ContactSolver.cpp 的第 96 行:
b2Assert(kNormal > b2_epsilon);

错误日志是:

Assertion failed: (kNormal > 1.19209290e-7F), function b2ContactSolver, file /Users/coooli01/Documents/Xcode Projects/Cocos2d/Hill Slide/Hill Slide/libs/Box2D/Dynamics/Contacts/b2ContactSolver.cpp, line 96.



对不起,如果我漫无目的地说得太久了,我已经被困在这个问题上很多年了。

最佳答案

这看起来不健康:

for (int i=0; i<maxHillPoints-1; i++) {
CGPoint p0 = hillPoints[i-1];//first iteration accesses hillPoints[-1]

除此之外,它对我来说还可以(只是猜测这里没有给出的一些值)。

关于objective-c - Box2d Cocos2d 圆与地面接触时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8110775/

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