Box2d Cocos2d circle crash on contact with ground
- by Oliver Cooper
this is my first question here so sorry if I do something wrong or this is too long. I have been reading this tutorial by Ray Wenderlich, I have modified it so it is flatter and gradually goes down hill. Basically I have a ball roll down a bumpy hill, but at the moment the ball only drops from about 100 pixels above. When ever the touch the app crashes (the app is a Mac Cocos2d Box2d app). The ball code is this:
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);
That works fine. This is the terrain code, this also works fine:
-(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);
}
}
However when ever the two collide the app crashes. The crash error is:
Thread 6 CVDisplayLink: Program received signal: "SIGABRT"
The error occurs on line 96 of b2ContactSolver.cpp:
b2Assert(kNormal > b2_epsilon);
The error log is:
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.
Sorry if I rambled on for too long, i've been stuck on this for ages.