0

我正在尝试实现碰撞检测和一个精灵从多点触控旋转的另一个精灵的基于物理的反弹。在图像中,看到两个黄点是同时的触摸点,我已经根据ccTouchesBegan方法和方法中的这些触摸计算了云的正确位置和角度ccTouchesMoved。当用户移动和旋转这两个触摸时,云会跟随并相应地旋转。

在此处输入图像描述

当鸟与云碰撞时,我尝试将云的角度传递给另一种方法,CGRectIntersectsRect并使用数学滚动我自己的碰撞/反弹,但这并没有产生预期的结果,所以我转向了 Box2D。

在阅读了 Ray Wenderlich 的 4 或 5 篇教程之后,我无法将使用多点触控云创建的运动与 Box2D 的主体、固定装置、鼠标关节和世界进行网格化。

我读过这个:Cocos2d - 旋转精灵的碰撞检测

...并且已经经历过:Ray Wenderlich 碰撞检测

我的最新ccTouchesMoved方法如​​下所示:

 - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    if (_mouseJoint != NULL) return;

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);


    if (_cloudFixture->TestPoint(locationWorld)) {
        b2MouseJointDef md;
        md.bodyA = _groundBody;
        md.bodyB = _cloudBody;
        md.target = locationWorld;
        md.collideConnected = true;
        md.maxForce = 1000.0f * _cloudBody->GetMass();

        _mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);
        _cloudBody->SetAwake(true);
    }

    NSSet *allTouches = [event allTouches];
    if([allTouches count] == 2){
        UITouch * touch1 = [[allTouches allObjects] objectAtIndex:0];
        CGPoint location1 = [touch1 locationInView: [touch1 view]];
        location1 = [[CCDirector sharedDirector] convertToGL:location1];

        UITouch * touch2 = [[allTouches allObjects] objectAtIndex:1];
        CGPoint location2 = [touch2 locationInView: [touch2 view]];
        location2 = [[CCDirector sharedDirector] convertToGL:location2];

        //EASIER TO WORK WITH INTS
        int touch1X = location1.x;
        int touch2X = location2.x;

        int touch1Y = location1.y;
        int touch2Y = location2.y;

        //FIND THE LEFT-MOST TOUCH
        int minX = (touch1X < touch2X) ? touch1X : touch2X;
        int maxX = (minX == touch1X) ? touch2X : touch1X;

        //FIND THE BOTTOM-MOST TOUCH
        int minY = (touch1Y < touch2Y) ? touch1Y : touch2Y;
        int maxY = (minY == touch1Y) ? touch2Y : touch1Y;

        int touchXDiff = maxX - minX;
        int touchYDiff = maxY - minY;

        [_cloud setPosition:ccp(touchXDiff/2 + minX, touchYDiff/2 + minY)];

        //ROTATE CLOUD AS IT MOVES

        int offDiffX = touch1X - touch2X;
        int offDiffY = touch1Y - touch2Y;

        float angleRadians = atanf((float)offDiffY / (float)offDiffX);
        float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
        float cloudAngle = -1 * angleDegrees;

        //[_cloud setRotation:cloudAngle];
        _cloudBody->SetTransform( _cloudBody->GetPosition(), cloudAngle );
    }

}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    if (_mouseJoint == NULL) return;

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    NSSet *allTouches = [event allTouches];
    if([allTouches count] == 2){
        UITouch * touch1 = [[allTouches allObjects] objectAtIndex:0];
        CGPoint location1 = [touch1 locationInView: [touch1 view]];
        location1 = [[CCDirector sharedDirector] convertToGL:location1];

        UITouch * touch2 = [[allTouches allObjects] objectAtIndex:1];
        CGPoint location2 = [touch2 locationInView: [touch2 view]];
        location2 = [[CCDirector sharedDirector] convertToGL:location2];

        //EASIER TO WORK WITH INTS
        int touch1X = location1.x;
        int touch2X = location2.x;

        int touch1Y = location1.y;
        int touch2Y = location2.y;

        //FIND THE LEFT-MOST TOUCH
        int minX = (touch1X < touch2X) ? touch1X : touch2X;
        int maxX = (minX == touch1X) ? touch2X : touch1X;

        //FIND THE BOTTOM-MOST TOUCH
        int minY = (touch1Y < touch2Y) ? touch1Y : touch2Y;
        int maxY = (minY == touch1Y) ? touch2Y : touch1Y;

        int touchXDiff = maxX - minX;
        int touchYDiff = maxY - minY;


        //ROTATE CLOUD AS IT MOVES

        int offDiffX = touch1X - touch2X;
        int offDiffY = touch1Y - touch2Y;

        float angleRadians = atanf((float)offDiffY / (float)offDiffX);
        float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
        float cloudAngle = -1 * angleDegrees;

        //BEFORE BOX2D
        //[_cloud setRotation:cloudAngle];
        //[_cloud setPosition:ccp(touchXDiff/2 + minX, touchYDiff/2 + minY)];

        //WITH BOX2D
        _cloudBody->SetTransform( _cloudBody->GetPosition(), cloudAngle );
        b2Vec2 locationWorld = b2Vec2((touchXDiff/2 + minX)/PTM_RATIO, (touchYDiff/2 + minY)/PTM_RATIO);
        _mouseJoint->SetTarget(locationWorld);
    }

}

编辑(为清楚起见):发生的情况是,当实现 Box2D 代码时,云仅响应第一次触摸,不调整触摸之间创建的角度并且不跟随两次触摸。我怎样才能用 Box2D 做到这一点?

有没有人有什么建议?

4

1 回答 1

0

可爱的艺术品!

您需要为您的多点触控身体注入速度和位置信息,以便从与其交互的其他身体获得适当的碰撞响应。

运动体实际上非常适合此目的。您所做的是设置云的几何形状,并随着多点触控位置的更新,计算将云从其当前位置移动到由触摸定义的新目标位置所需的线速度和角速度。这就是您移动运动体的方式(使用速度)。你可以让它轻轻地漂移或严格地跟随运动 --- 使用运动体,它会刚性地碰撞并且不会因碰撞而对自身施加任何动量。

您还需要弄清楚如何处理运动体的实际长度会随着手指移动而变化的事实。

希望有帮助

于 2013-02-02T15:52:46.440 回答