【问题标题】:Multitouch Sprite Rotation with Collision Detection带有碰撞检测的多点触控精灵旋转
【发布时间】:2013-02-01 14:49:24
【问题描述】:

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

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

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

我读过这个:Cocos2d - Collision Detection of Rotated Sprite

...已经经历过这个:Ray Wenderlich Collision Detection

我最新的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 完成此任务?

有人有什么建议吗?

【问题讨论】:

  • 这里看不到任何问题...只是很多文本和代码...
  • 通常问题以“?”结束...... ...就像所有文本和代码之后的问题:“有人有什么建议吗?”也许您应该尝试提供帮助而不是拖钓一个很棒的网站。

标签: ios objective-c cocos2d-iphone box2d


【解决方案1】:

可爱的艺术品!

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

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

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

希望有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    相关资源
    最近更新 更多