I am fairly new to the world of box2d, and i am trying to make a game, in part of which the user can pick up a ball. As there are many balls on the screen, i have created a for loop to search through all of the bodies and their fixtures to see if the user is touching them (code included below). The app freezes when it gets to the for loop, and the debugger highlights the "return m_fixtureList" in the box2d sources.
Please help me as i am still very much learning
Here is the code:
//Level.mm ccTouchesBegan: method
-(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (mouseJoint != NULL) return NO;
NSSet *allTouches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
if ([allTouches count] == 1) {
b2Vec2 locationWorld = b2Vec2(touchLocation.x, touchLocation.y);
if (touchLocation.x < throwWidth) {
b2Fixture *f;
b2Fixture *fix;
b2Body *b = world->GetBodyList();
FOR (F=B->GETFIXTURELIST(); F; B = B->GETNEXT(), F = B->GETFIXTURELIST()) { //THIS CAUSES THE FREEZE
if (f->TestPoint(locationWorld)) {
fix = f;
break;
}
}
mouseJointDef.bodyA = groundBody;
mouseJointDef.bodyB = bod;
mouseJointDef.target = locationWorld;
mouseJointDef.collideConnected = true;
mouseJointDef.maxForce = 1000000.0f * bod->GetMass();
mouseJoint = (b2MouseJoint *)world->CreateJoint(&mouseJointDef);
bod->SetAwake(true);
}
}
return YES;
}
The creation of the physics of the ball(s) in CannonBall.mm:
CCSprite *sprite = [CCSprite spriteWithFile:@"CannonBall.png"];
[tempMap addChild:sprite];
sprite.position = ccp(x, y);
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(x, y);
bodyDef.userData = sprite;
body = worl->CreateBody(&bodyDef);
circle.m_radius = 25;
fixtureDef.shape = &circle;
fixtureDef.density = 0.7f;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.2f;
body->CreateFixture(&fixtureDef);
the code that the debugger points me to in b2body.h:
inline b2Fixture* b2Body::GetFixtureList()
{
return m_fixtureList; //THIS LINE IS HIGHLIGHTED IN RED BY THE DEBUGGER
}
Please help me as i am not very good and it will be much appreciated. If you have read through all of that code, thank you so much, I know that there is a lot of it and it is not exactly well commented/written.