2016-11-10 53 views
0

我使用JSTileMap繪製一個關卡。我改變了一點點,以便將SKPhysicsBody添加到每個瓷磚上,但有時當我將一些衝動應用到主要角色時,他碰到了牆壁/地面/天花板,他表現得很怪異。他從表面反映出與物理學原理相悖的方式。我認爲這是因爲玩家擊中了兩個物理實體(例如地面的兩個物理實體)連接的點。 SKPhysicsBody類提供了一種從不同物理體創建一個物理體的方法+(SKPhysicsBody *)bodyWithBodies:(NSArray *)body;我可以使用這種方法從瓷磚物理實體中創建一個物理實體嗎?如何將physicsBody添加到由JSTileMap生成的貼圖並將其更改爲一個物理體

下面是從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; 
} 

增加物理機構每瓦以及將這些物理機構的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]; 
     //-----||------ 

在結果,在高度和一個瓦片的寬度,在一個物理體中的溶液。

回答

1

如果你想使用[SKPhysicsBody bodyWithBodies:數組],那麼你需要確保該陣列中的所有機構都相對於父節點。

sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];意味着你的物理身體位置是相對於精靈節點。您需要相對於父節點的主體。

我知道如何做到這一點的唯一方法是與中心:

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

這應該放置在SKPhysicsBody精靈的位置時,它被添加到父節點。

+0

謝謝,我可以創建一個由所有瓷磚製成的物理實體,但這種奇怪的行爲並沒有消失。我認爲bodyWithBodies方法會以某種方式連接所有的身體,並刪除它們之間的連接,但它只能幫助你將所有的身體視爲一體(如移動,定位等),我的不好。 – mfker

+0

奇怪的行爲 – Knight0fDragon

+0

當我對主角應用一些衝動,並且他撞到牆壁/地面/天花板時,他表現得很怪異。他從表面反映出與物理學原理相悖的方式。例如,當他向右移動(根據施加的衝動)並且他從天花板反射時,他有時開始向後移動(左)。 – mfker

相關問題