这是我在这里的第一个问题,如果我做错了或者这太长了,那就很抱歉。我一直在阅读Ray Wenderlich this tutorial,我修改了它,因此它更平坦,逐渐下山。基本上我有一个球滚下一个颠簸的山丘,但此刻球只从大约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);
}
}
然而,当两个碰撞应用程序崩溃时。崩溃错误是:
线程6 CVDisplayLink:程序接收信号:“SIGABRT”
错误发生在b2ContactSolver.cpp的第96行:
b2Assert(kNormal > b2_epsilon);
错误日志是:
断言失败:(kNormal&gt; 1.19209290e-7F),函数b2ContactSolver,文件/ Users / coooli01 / Documents / Xcode Projects / Cocos2d / Hill Slide / Hill Slide / libs / Box2D / Dynamics / Contacts / b2ContactSolver.cpp,第96行。
对不起,如果我漫游太长时间,我已经坚持了很久。
答案 0 :(得分:3)
这看起来并不健康:
for (int i=0; i<maxHillPoints-1; i++) {
CGPoint p0 = hillPoints[i-1];//first iteration accesses hillPoints[-1]
除此之外它对我有用(只是猜测这里没有给出的一些值)。
答案 1 :(得分:0)
当两个物体的质量都为零时,通常会发生这种崩溃。
你的地面不需要有质量,因为它是由边缘构成的,但你的身体应该有一个质量,(通过设置它的密度)。我看到你正在设置密度,但要确保它在碰撞时没有改变。