alriano

How to handle collision between sprite and tilemap?

Recommended Posts

What is commonly done is that a 2nd tilemap is created whose sole purpose is to deal with collisions. This collision tilemap just holds information about where collisions will occur and what type of tile the collision is. Then when you move the player, you just check where the player is going against the collision tilemap and if the tile the player is moving into has a tile, you can check what type of tile and respond accordingly.
 
So for example, let's say you have a room surrounded by walls (represented by tile #1) with a river of lava through it (represented by tile #2) and any tile that doesn't have a collision will be empty (represented by tile #0). The collision tilemap could look like the following:
 
 <code>
collisionTilemap = [
   [1, 1, 1, 1, 2, 1, 1, 1, 1],
   [1, 0, 0, 0, 2, 0, 0, 0, 1],
   [1, 0, 0, 0, 0, 0, 0, 0, 1],
   [1, 0, 0, 0, 2, 0, 0, 0, 1],
   [1, 1, 1, 1, 2, 1, 1, 1, 1]
]

</code>

 

When the player tries to move into a tile with the #1, he is just prevented from moving into the square. But if the player tries to move into a tile with the #2, the player dies because he touched lava.

Share this post


Link to post
Share on other sites

Hi,
I just want to ask something. I have made a game where I have used canvas to make a string. Basically the canvas is a line which behaves  as a string. So what I want to do is detect when the string(i.e. canvas) collides with coin tiles on the tilemap. how can I use game physics and collide functions between canvas and a tilemap layer?  (Not a sprite and tilemap).

 

Please help.

Share this post


Link to post
Share on other sites

Sorry to revive this but I'm having some problems with this example here.

http://phaser.io/examples/v2/tilemaps/tile-callbacks

My main issue is that on collision the tile becomes passable. How do I make it impassable? 

In the example, the once the arrow connects with the coin, then the coin changes it alpha value AND becomes passable at the same time. Why? 

How do I make it so that I can get the collide callback but not change any physics?

 

EDIT:

 

//  This will set Tile ID 26 (the coin) to call the hitCoin function when collided with
    map.setTileIndexCallback(26, hitCoin, this);

    //  This will set the map location 2, 0 to call the function
    map.setTileLocationCallback(2, 0, 1, 1, hitCoin, this);

These 2 functions here for example. the .setTileLocationCallback works as how I imagine it. It sets the tiles.alpha to 0.2 but it's not passable by the sprite body. Meanwhile the setTileIndexCallback makes it both passable and adds alpha to it. Why does one change the physics and the other one does not? I would prefer if neither changed the physics, the way setTileLocationCallback is set up.

 

EDIT 2:

http://examples.phaser.io/_site/view_full.html?d=tilemaps&f=tile+callbacks.js&t=tile callbacks

And in this example both functions make the tile passable.

Share this post


Link to post
Share on other sites
2 hours ago, anthkris said:

Wanted to make sure I posted this @symof. I submitted an issue and got some answers. You need to return true in the callback function to keep the tile impassable.

Thanks for your reply. I did change the return value to true in the hitCoin function from the example and it's still passable.

http://phaser.io/sandbox/edit/WDmKTvNO

You can see from the example that the coin is not impassable even with hitCoin returning true.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.