简体   繁体   English

如何将PhysicsBody添加到JSTileMap生成的图块并将其更改为一个physicsBody

[英]How to add physicsBody to tiles generated by JSTileMap and change them into one physicsBody

I use JSTileMap to draw a level. 我使用JSTileMap绘制一个级别。 I changed it a little bit to add SKPhysicsBody to every tile but sometimes when I apply some impulse to main character and he hits the wall/ground/ceiling he is acting weird. 我做了一些更改,将SKPhysicsBody添加到每个图块,但是有时当我对主角施加一些冲动,并且他碰到墙/地面/天花板时,他的行为很奇怪。 He reflects from surfaces with the way that is contrary to the principles of physics. 他用与物理原理相反的方式从表面反射。 I think that it happens because the player hits the point where two physics bodies (for example two physics bodies of the ground) connects. 我认为发生这种情况是因为玩家碰到了两个物理物体(例如地面上的两个物理物体)连接的地方。 SKPhysicsBody class provides a method to create one physics body from different physics bodies + (SKPhysicsBody *)bodyWithBodies:(NSArray *)bodies; SKPhysicsBody类提供了一种从不同的物理物体+(SKPhysicsBody *)bodyWithBodies:(NSArray *)bodies创建一个物理物体的方法; Can I use this method to create one physics body from ale the tiles physics bodies? 我可以使用这种方法从瓷砖物理物体中创建一个物理物体吗?

Here's a method from JSTileMap where I add physics bodies: 这是JSTileMap的一种方法,其中添加了物理实体:

+(id) layerWithTilesetInfo:(NSArray*)tilesets layerInfo:(TMXLayerInfo*)layerInfo mapInfo:(JSTileMap*)mapInfo
{
    TMXLayer* layer = [TMXLayer node];
  layer.map = mapInfo;

    layer.tilesByColumnRow = [NSMutableDictionary dictionary];
    // basic properties from layerInfo
    layer.layerInfo = layerInfo;
    layer.layerInfo.layer = layer;
    layer.mapTileSize = mapInfo.tileSize;
    layer.alpha = layerInfo.opacity;
    layer.position = layerInfo.offset;

    // recalc the offset if we are isometriic
    if (mapInfo.orientation == OrientationStyle_Isometric)
    {
        layer.position = CGPointMake((layer.mapTileSize.width / 2.0) * (layer.position.x - layer.position.y),
                                     (layer.mapTileSize.height / 2.0) * (-layer.position.x - layer.position.y));
    }

    NSMutableDictionary* layerNodes = [NSMutableDictionary dictionaryWithCapacity:tilesets.count];

    //MY CODE
    NSMutableArray *arrayOfSprites = [[NSMutableArray alloc] init];
    SKNode *theSprite;
    //----||----

    // loop through the tiles
    for (NSInteger col = 0; col < layerInfo.layerGridSize.width; col++)
    {
        for (NSInteger row = 0; row < layerInfo.layerGridSize.height; row++)
        {
            // get the gID
            NSInteger gID = layerInfo.tiles[col + (NSInteger)(row * layerInfo.layerGridSize.width)];

            // mask off the flip bits and remember their result.
            bool flipX = (gID & kTileHorizontalFlag) != 0;
            bool flipY = (gID & kTileVerticalFlag) != 0;
            bool flipDiag = (gID & kTileDiagonalFlag) != 0;
            gID = gID & kFlippedMask;

            // skip 0 GIDs
            if (!gID)
                continue;

            // get the tileset for the passed gID.  This will allow us to support multiple tilesets!
            TMXTilesetInfo* tilesetInfo = [mapInfo tilesetInfoForGid:gID];
            [layer.tileInfo addObject:tilesetInfo];

            if (tilesetInfo)    // should never be nil?
            {
                SKTexture* texture = [tilesetInfo textureForGid:gID];
                SKSpriteNode* sprite = [SKSpriteNode spriteNodeWithTexture:texture];

                sprite.name = [NSString stringWithFormat:@"%ld",(long)(col + row * layerInfo.layerGridSize.width)];

                // make sure it's in the right position.
                if (mapInfo.orientation == OrientationStyle_Isometric)
                {
                    sprite.position = CGPointMake((layer.mapTileSize.width / 2.0) * (layerInfo.layerGridSize.width + col - row - 1),
                                                  (layer.mapTileSize.height / 2.0) * ((layerInfo.layerGridSize.height * 2 - col - row) - 2) );
                }
                else
                {
                    sprite.position = CGPointMake(col * layer.mapTileSize.width + layer.mapTileSize.width/2.0,
                                                  (mapInfo.mapSize.height * (tilesetInfo.tileSize.height)) - ((row + 1) * layer.mapTileSize.height) + layer.mapTileSize.height/2.0);
                }

                // flip sprites if necessary
                if(flipDiag)
                {
                    if(flipX)
                        sprite.zRotation = -M_PI_2;
                    else if(flipY)
                        sprite.zRotation = M_PI_2;
                }
                else
                {
                    if(flipY)
                        sprite.yScale *= -1;
                    if(flipX)
                        sprite.xScale *= -1;
                }

                // add sprite to correct node for this tileset
                SKNode* layerNode = layerNodes[tilesetInfo.name];
                if (!layerNode) {
                    layerNode = [[SKNode alloc] init];
                    layerNodes[tilesetInfo.name] = layerNode;
                }

                //adding physicsbody to every tile
                //sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(sprite.frame.size.width, sprite.frame.size.height)];
                //sprite.physicsBody.dynamic = NO;
                //sprite.physicsBody.categoryBitMask = mapCategory;

                //[arrayOfSprites addObject:sprite.physicsBody];

                [layerNode addChild:sprite];
                NSUInteger indexes[] = {col, row};
                NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexes length:2];
                [layer.tilesByColumnRow setObject:sprite forKey:indexPath];

                //MY CODE
                sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
                sprite.physicsBody.dynamic = NO;
                [arrayOfSprites addObject:sprite.physicsBody];
                //-----||------

#ifdef DEBUG
//              CGRect textRect = [texture textureRect];
//              NSLog(@"atlasNum %2d (%2d,%2d), gid (%d,%d), rect (%f, %f, %f, %f) sprite.pos (%3.2f,%3.2f) flipx%2d flipy%2d flipDiag%2d", gID+1, row, col, [tilesetInfo rowFromGid:gID], [tilesetInfo colFromGid:gID], textRect.origin.x, textRect.origin.y, textRect.size.width, textRect.size.height, sprite.position.x, sprite.position.y, flipX, flipY, flipDiag);
#endif

            }
        }
    }

    //MY CODE
    NSArray *array = [arrayOfSprites copy];
    theSprite = [SKNode node];
    theSprite.physicsBody = [SKPhysicsBody bodyWithBodies:array];
    theSprite.physicsBody.dynamic = NO;
    theSprite.position = CGPointMake(layer.position.x+16, layer.position.y+16);
    [layer addChild:theSprite];
    //-----||------

    // add nodes for any tilesets that were used in this layer
    for (SKNode* layerNode in layerNodes.allValues) {
        if (layerNode.children.count > 0) {
            [layer addChild:layerNode];
        }
    }

    [layer calculateAccumulatedFrame];

    return layer;
}

After adding physics bodies to every tile and adding those physics bodies to NSMutableArray I assign a copy of this NSMutableArray to the NSArray and try to create one physics body out of it like this: 在将物理物体添加到每个图块并将这些物理物体添加到NSMutableArray之后,我将此NSMutableArray的副本分配给NSArray并尝试像这样从中创建一个物理物体:

//MY CODE
        NSArray *array = [arrayOfSprites copy];
        theSprite = [SKNode node];
        theSprite.physicsBody = [SKPhysicsBody bodyWithBodies:array];
        theSprite.physicsBody.dynamic = NO;
        theSprite.position = CGPointMake(layer.position.x+16, layer.position.y+16);
        [layer addChild:theSprite];
        //-----||------

In result, one physics body with the height and width of one tile is added. 结果,添加了一个高度和宽度为1个图块的物理物体。

If you want to use [SKPhysicsBody bodyWithBodies:array], then you need to make sure that all the bodies in the array are relative to the parent node. 如果要使用[SKPhysicsBody bodyWithBodies:array],则需要确保数组中的所有主体都相对于父节点。

sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size]; means that your physics body position is relative to the sprite node. 表示您的物理身体位置是相对于Sprite节点的。 You need the body relative to the parent node. 您需要相对于父节点的主体。

The only way I know how to do this is with center: 我知道如何做到这一点的唯一方法是使用center:

sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size center:sprite.position];

This should place the SKPhysicsBody at the location of the sprite when it is added to the parent node. 将其添加到父节点后, SKPhysicsBody在该精灵的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM