【问题标题】:Drag and Drop the Physics Body in cocos2d 3.1?在 cocos2d 3.1 中拖放物理体?
【发布时间】:2014-07-03 05:34:21
【问题描述】:

我是cocos2d v3.1的新手

如何在cocos2d v3.1中拖放Physics body。

然后是如何检查两个物理体之间的碰撞和检测。

提前谢谢你

【问题讨论】:

    标签: ios cocos2d-iphone drag-and-drop collision-detection physics


    【解决方案1】:

    首先,您需要一个名为 CCTouchJoint 的类。

    CCTouchJoint.h

    #import "Box2D.h"
    
    @interface CCTouchJoint : NSObject
    {
    @public
    b2MouseJoint *mouseJoint;
    UITouch *touch;
    }
     @property (assign) b2MouseJoint *mouseJoint;
     @property (nonatomic, retain) UITouch *touch;
    
    - (id)initLocal:(UITouch *)touch withMouseJoint:(b2MouseJoint *)mouseJoint;
    + (id)touch:(UITouch *)touch withMouseJoint:(b2MouseJoint *)mouseJoint;
    
     // Public methods
    
    /**
    * Destroy the touch joint in the Box2d world.
    */
     - (void)destroyTouchJoint;
    
    @end
    

    CCTouchJoint.mm

    #import "CCTouchJoint.h"
    
     @implementation CCTouchJoint
     @synthesize mouseJoint;
     @synthesize touch;
    
    - (void)dealloc
     {
      [touch release];
      [super dealloc];
      }
    
    - (id)initLocal:(UITouch *)_touch withMouseJoint:(b2MouseJoint *)_mouseJoint
     {
      if ((self = [super init]))
      {
        self.touch = _touch;
        mouseJoint = _mouseJoint;
     }
     return self;
     }
    
    + (id)touch:(UITouch *)_touch withMouseJoint:(b2MouseJoint *)_mouseJoint
     {
     return [[self alloc] initLocal:_touch withMouseJoint:_mouseJoint];
      }
    
    #pragma mark -
     #pragma mark CCTouchJoint Public Methods
    
     - (void)destroyTouchJoint
       {
      if (mouseJoint != NULL)
       {
        mouseJoint->GetBodyA()->GetWorld()->DestroyJoint(mouseJoint);
       }
     } 
    
    #pragma mark CCTouchJoint Private Methods
    
    @end
    

    第二。 self.touchEnabled = YES; 你需要 NSMutableArray *touchJointList; b2Body *groundBody 和 b2Body *b; 这个触摸代码:

      - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
     {
       NSSet *allTouches = [event allTouches];
    
    
      for(UITouch *touch in allTouches)
      {
        CGPoint location = [touch locationInView:touch.view];
    
        location = [[CCDirector sharedDirector] convertToGL:location];
        b2Vec2 worldLoc = b2Vec2(ptm(location.x), ptm(location.y));
    
        for (b = world->GetBodyList(); b; b = b->GetNext())
        {
            if (b->GetType() == b2_dynamicBody)
    
    
                for (b2Fixture *f = b->GetFixtureList(); f; f = f->GetNext())
                {    
    
    
                    // Hit!
                    if (f->TestPoint(worldLoc))
                    {
    
                        /// Mouse joint definition
                        b2MouseJointDef md;
                        md.bodyA = groundBody;
                        md.bodyB = b;
                        md.target = worldLoc;
                        md.maxForce = 3000.0 * b->GetMass();
                        md.dampingRatio = 5;
                        md.frequencyHz = 60;
    
    
                        // Joint of bodys
                        b2MouseJoint *m_touchJoint;
                        m_touchJoint = (b2MouseJoint *)world->CreateJoint(&md);
    
                        CCTouchJoint *tj = [CCTouchJoint touch:touch withMouseJoint:m_touchJoint];
                        [touchJointList addObject:tj];
    
                        b->SetAwake(true);
    
                        }
                        break;
                    }
                }
                }
           }
    
    
    
    
    - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
      {
    
       for (CCTouchJoint *tj in touchJointList)
       {
        if([tj.touch phase] == UITouchPhaseMoved)
          {
            // Update if it is moved
            CGPoint location = [tj.touch locationInView:tj.touch.view];
            location = [[CCDirector sharedDirector] convertToGL:location];
    
            b2Vec2 worldLocation = b2Vec2(ptm(location.x), ptm(location.y));
            tj.mouseJoint->SetTarget(worldLocation);
            }
        }
     }
    
      - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
      {
    
      NSSet *allTouches = [event allTouches];
    
       NSMutableArray *discardedItems = [NSMutableArray array];
    
       for(UITouch *touch in allTouches)
         {
        for (CCTouchJoint *tj in touchJointList)
        {
            if (tj.touch == touch)
            {
                // Defensive programming - assertion
                NSAssert([tj isKindOfClass:[CCTouchJoint class]], @"node is not a touchJoint!");
    
                // If safe - loop through
                if ([tj.touch phase] == UITouchPhaseEnded)
                {
                    [discardedItems addObject:tj];
    
                    [tj destroyTouchJoint];
                    [tj release];
                }
            }
        }
    
     }
    
    
      [touchJointList removeObjectsInArray:discardedItems];
    
      }
    
      - (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
     {
    
      [touchJointList removeAllObjects];
    
        }
    

    记住:更改 md.dampingRatio 和 md.frequencyHz 会影响鼠标关节的行为。

    【讨论】:

    • 抱歉最近的回复。谢谢你,现在我将完成它。
    【解决方案2】:

    尝试以下链接进行碰撞检测。对你有帮助。

    http://www.cocos2d-x.org/wiki/Chapter_5_-_How_to_Detect_the_Collisions 
    

    【讨论】:

    • 感谢您的回复。但我正在寻找 iphone
    猜你喜欢
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    相关资源
    最近更新 更多