I'm new to box2d and am trying to let the user play around with objects (b2body's) on the screen using touch.
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (mouseJoint != NULL) return;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationInWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
//Iterate over the bodies in the physics world
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
for (b2Fixture* f = b->GetFixtureList(); f; f->GetNext()) {
if (f->TestPoint(locationInWorld)) {
b2MouseJointDef md;
md.bodyA = groundBody;
md.bodyB = b;
md.target = locationInWorld;
md.collideConnected = YES;
md.maxForce = 1000.0f * b->GetMass();
mouseJoint = (b2MouseJoint *)world->CreateJoint(&md);
b->SetAwake(YES);
return;
}
}
}
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (mouseJoint == NULL) return;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationInWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
mouseJoint->SetTarget(locationInWorld);
}
-(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self ccTouchesEnded:touches withEvent:event];
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (mouseJoint) {
world->DestroyJoint(mouseJoint);
mouseJoint = NULL;
}
}
My app freezes when a touch is received. Any ideas?