1

cocos2d と Tiled (マップ用) を使用して、iPhone 用のプラットフォーム ゲームを作成しようとしています。私がネットで見たすべてのチュートリアルは、Tiled の Layers を使用して衝突検出を行っています。オブジェクトを使用してそれを行いたい...レイヤーではありません。オブジェクトを使用すると、ゲームにより良い「リアリティ」を与えることができるカスタム シェイプを作成できます。私が意味することの例を挙げると: ここに画像の説明を入力

背景として地面を描き、その上にオブジェクト レイヤーを作成しました。背景タイルではなく、それとのプレイヤーの衝突を検出したい。

現在、最も有名なチュートリアルを使用しています: http : //www.raywenderlich.com/15230/how-to-make-a-platform-game-like-super-mario-brothers-part-1代わりにオブジェクトの衝突をチェックするメソッド。問題は、Tiled では座標系が cocos2d とは異なることです。Tiled は左上隅から始まり、cocos2d は左下隅から始まります....それだけでなく... Tiled のオブジェクト プロパティの幅と高さが (おそらく) iphone デバイスと同じに対応していないことに気付きました。上記の長方形にはプロパティがあります。

ここに画像の説明を入力

w/h は 480/128 タイル (Retina デバイスの場合) です。つまり、このように保持すると、マップ内でおそらく巨大になります。私の推測では、これを 2 で除算する必要があります。これまでのところ、次のようになりました。

-(void)checkForAndResolveObjCollisions:(Player *)p {

    CCTiledMapObjectGroup *objectGroup = [map objectGroupNamed:@"Collision"];
    NSArray* tiles = [objectGroup objects];

    CGFloat x, y, wobj, hobj;
    for (NSDictionary *dic in tiles) {
        CGRect pRect = [p collisionBoundingBox]; //3
        x = [[dic valueForKey:@"x"] floatValue];
        y = [[dic valueForKey:@"y"] floatValue];
        wobj = [[dic valueForKey:@"width"] floatValue];
        hobj = [[dic valueForKey:@"height"] floatValue];

        CGPoint position = CGPointMake(x, y);
        CGPoint objPos = [self tileForPosition:position];
        CGRect tileRect = CGRectMake(objPos.x, objPos.y, wobj/2, hobj/2);

            if (CGRectIntersectsRect(pRect, tileRect)) {
                CCLOG(@"INTERSECT");
                CGRect intersection = CGRectIntersection(pRect, tileRect);
                NSUInteger tileIndx = [tiles indexOfAccessibilityElement:dic];
                if (tileIndx == 0) {
                    //tile is directly below player
                    p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y + intersection.size.height);
                    p.velocity = ccp(p.velocity.x, 0.0);
                    p.onGround = YES;
                } else if (tileIndx == 1) {
                    //tile is directly above player
                    p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y - intersection.size.height);
                    p.velocity = ccp(p.velocity.x, 0.0);
                } else if (tileIndx == 2) {
                    //tile is left of player
                    p.desiredPosition = ccp(p.desiredPosition.x + intersection.size.width, p.desiredPosition.y);
                } else if (tileIndx == 3) {
                    //tile is right of player
                    p.desiredPosition = ccp(p.desiredPosition.x - intersection.size.width, p.desiredPosition.y);
                } else {
                    if (intersection.size.width > intersection.size.height) {
                        //tile is diagonal, but resolving collision vertially
                        p.velocity = ccp(p.velocity.x, 0.0);
                        float resolutionHeight;
                        if (tileIndx > 5) {
                            resolutionHeight = -intersection.size.height;
                            p.onGround = YES;
                        } else {
                            resolutionHeight = intersection.size.height;
                        }

                        p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y + resolutionHeight );

                    } else {
                        float resolutionWidth;
                        if (tileIndx == 6 || tileIndx == 4) {
                            resolutionWidth = intersection.size.width;
                        } else {
                            resolutionWidth = -intersection.size.width;
                        }
                        p.desiredPosition = ccp(p.desiredPosition.x + resolutionWidth , p.desiredPosition.y);

                    }
                }
            }
     //   }
    }
    p.position = p.desiredPosition; //8
}




- (CGPoint)tileForPosition:(CGPoint)p

{
    NSInteger x = (NSInteger)(p.x / map.tileSize.width);
    NSInteger y = (NSInteger)(((map.mapSize.height * map.tileSize.width) - p.y) / map.tileSize.width);
    return ccp(x, y);

}

オブジェクト x、y、w、h を取得し、それらを cocos2d の寸法とサイズに変換しようとしています。

上記は次のように変換されます。

Dimens: 480.000000, 128.000000
Coord: 0.000000, 40.000000

基本的にその混乱。そして、それは機能していません.....まったく。プレイヤーはただ通り抜けます。私が間違っていない限り、これまでオブジェクトに基づいて衝突検出を行った人がいないことに驚いています。

これを行うことができるかどうか、またはどのように行うことができるかを誰かが知っていますか? 彼がここで何をしているのか : https://www.youtube.com/watch?feature=player_detailpage&v=2_KB4tOTH6w#t=30

長い投稿で申し訳ありません。事前にご回答いただきありがとうございます。

4

0 に答える 0