Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 24th, 2012  |  syntax: None  |  size: 1.98 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Soft Body collapses in Cocos2D
  2. #pragma mark - Touch Management
  3.  
  4. -(void) registerWithTouchDispatcher
  5. {
  6.     [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
  7. }
  8.  
  9.  
  10.  
  11. - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
  12.     BOOL beginTouch = NO;
  13.  
  14.     b2Vec2 ballVec = node.innerCircleBody->GetPosition();
  15.     CGPoint ballPoint = CGPointMake((ballVec.x*PTM_RATIO)-node.radius, (ballVec.y*PTM_RATIO)-node.radius);
  16.  
  17.     CGPoint location = [touch locationInView:[touch view]];
  18.     location = [[CCDirector sharedDirector] convertToGL:location];
  19.     b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
  20.  
  21.  
  22.     CGRect ballRect = CGRectMake(ballPoint.x, ballPoint.y, node.radius*2, node.radius*2);
  23.     if (CGRectContainsPoint(ballRect, location)) {
  24.         [node halt];
  25.  
  26.         b2MouseJointDef md;
  27.         md.bodyA = groundBody;
  28.         md.bodyB = node.innerCircleBody;
  29.         md.target = locationWorld;
  30.         md.collideConnected = true;
  31.         md.maxForce = 1000.0f * node.innerCircleBody->GetMass();
  32.         md.dampingRatio = 10.0f;
  33.         md.frequencyHz = 100.f;
  34.  
  35.         _mouseJoint = (b2MouseJoint *)world->CreateJoint(&md);
  36.         node.innerCircleBody->SetAwake(true);
  37.         beginTouch = YES;
  38.     }
  39.     return beginTouch;
  40. }
  41.  
  42. - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
  43.     if (_mouseJoint == NULL) return;
  44.  
  45.     CGPoint location = [touch locationInView:[touch view]];
  46.     location = [[CCDirector sharedDirector] convertToGL:location];
  47.     b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
  48.  
  49.     _mouseJoint->SetTarget(locationWorld);
  50. }
  51.  
  52. - (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event {
  53.     if (_mouseJoint) {
  54.         world->DestroyJoint(_mouseJoint);
  55.         _mouseJoint = NULL;
  56.     }
  57. }
  58.  
  59. - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
  60. //    [node bounce];
  61.     if (_mouseJoint == NULL) return;
  62.     world->DestroyJoint(_mouseJoint);
  63.     _mouseJoint = NULL;
  64. }