Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To get the bounding box of a tile at a given position of the tilemap:</p> <pre><code>- (void) setupTilemap { NSString * aFilename = @"tilemap.tmx"; theTilemap = [CCTMXTiledMap tiledMapWithTMXFile:aFilename]; [self addChild:theTilemap]; } - (CGRect) tileBoundingBoxAt:(CGPoint)mapCoordinate { CGPoint tilepos; tilepos.x = abs(mapCoordinate.x / theTilemap.tileSize.width); tilepos.y = abs(mapCoordinate.y / theTilemap.tileSize.height); CGRect boundingBox; boundingBox.origin.x = tilepos.x * theTilemap.tileSize.width; boundingBox.origin.y = tilepos.y * theTilemap.tileSize.height; boundingBox.size.width = theTilemap.tileSize.width; boundingBox.size.height = theTilemap.tileSize.height; return boundingBox; } </code></pre> <p>But, as already said in the comments, this is probably not the most effective way of doing it.</p> <p>For instance if you want to check if a sprite's bounding box is (or is about to) collide with a certain type of tile (say a wall or water) then you can check for the type of tiles at a several points around your sprite.</p> <p>So, instead of just testing the tile at your anchor point (i.e. middle of your sprite)</p> <pre><code>------- | | | X | | | ------- </code></pre> <p>You can instead test all 4 corners of your sprites bounding box.</p> <pre><code>X-----X | | | | | | X-----X </code></pre> <p>You can test as many points as you need for your games requirements. Depending on what you need other offsets might make even more sense for you.</p> <p>For instance only horizontally left and right, but with an offset ahead of the sprite:</p> <pre><code> ------- | | X | | X | | ------- ------- | | X | | X | | ------- X X X </code></pre> <p>The benefit is that you only do a test for each of your "sensor" points. If you use the bounding box you have to test for each tile that is relevant. As soon as you test against more than a handful of tiles it is less effective.</p> <p><strong>Update. Example for a bouncing ball:</strong></p> <p>Assuming the ball collides with only 1 wall (a floor) which is always below the ball Say your ball has its anchor point in the center. you calculate (X) using the radius of the ball. you can calculate the radius by dividing the sprite's bounding box height / 2.</p> <pre><code> ------- | | | . | | | ---X--- </code></pre> <p>TTTTTTTTTTTTTTTTTTTTTTT</p> <p>. = anchor point X = location to test for collision with floor T = tiles</p> <p>get the coordinate of your sprite which is the anchor point (.) calculate coordinate of point to test (X) testpoint xpos = the same as your ball sprite testpoint ypos = ball sprite y pos + radius (if you want + a little offset to test ahead)</p> <p>each frame you get the tile at coordinate (X) and check if it is a tile with collision property. if it is then you have a collision and react accordingly (change of motion direction in this case).</p> <p>if you want the ball to be able to collide with the ceiling and the side walls as well then check at all 4 edges like that:</p> <pre><code> X ------- | | X | . | X | | ------- X </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload