- Soft Body collapses in Cocos2D
- #pragma mark - Touch Management
- -(void) registerWithTouchDispatcher
- {
- [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
- }
- - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
- BOOL beginTouch = NO;
- b2Vec2 ballVec = node.innerCircleBody->GetPosition();
- CGPoint ballPoint = CGPointMake((ballVec.x*PTM_RATIO)-node.radius, (ballVec.y*PTM_RATIO)-node.radius);
- CGPoint location = [touch locationInView:[touch view]];
- location = [[CCDirector sharedDirector] convertToGL:location];
- b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
- CGRect ballRect = CGRectMake(ballPoint.x, ballPoint.y, node.radius*2, node.radius*2);
- if (CGRectContainsPoint(ballRect, location)) {
- [node halt];
- b2MouseJointDef md;
- md.bodyA = groundBody;
- md.bodyB = node.innerCircleBody;
- md.target = locationWorld;
- md.collideConnected = true;
- md.maxForce = 1000.0f * node.innerCircleBody->GetMass();
- md.dampingRatio = 10.0f;
- md.frequencyHz = 100.f;
- _mouseJoint = (b2MouseJoint *)world->CreateJoint(&md);
- node.innerCircleBody->SetAwake(true);
- beginTouch = YES;
- }
- return beginTouch;
- }
- - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
- if (_mouseJoint == NULL) return;
- CGPoint location = [touch locationInView:[touch view]];
- location = [[CCDirector sharedDirector] convertToGL:location];
- b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
- _mouseJoint->SetTarget(locationWorld);
- }
- - (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event {
- if (_mouseJoint) {
- world->DestroyJoint(_mouseJoint);
- _mouseJoint = NULL;
- }
- }
- - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
- // [node bounce];
- if (_mouseJoint == NULL) return;
- world->DestroyJoint(_mouseJoint);
- _mouseJoint = NULL;
- }