Jump to content
  • Advertisement
Sign in to follow this  
Stompy9999

Tile based collision

This topic is 4879 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently making a Platform game in C and Allegro. In my game, tiles are stored in a multidemensional char array. I'm trying to write a collision function. Here is what I'm thinking for the code:
int CheckCollision(int x, int y, const char block_type)
{

    if(map_mem[y/tile_size][x/tile_size] == block_type)
       return TRUE;

    return FALSE;

}

Would this work well, or is there a better method.

Share this post


Link to post
Share on other sites
Advertisement
If I'm not mistaken, your above code snippet will merely check if the player is already inside the tile, correct? (of course, provided you pass the player's coordinates as x and y).

It think it would be better to check for collisions only when you move the player, if possible. For example, when right is pressed, check if the player is going to cross the threshold to another tile. If so, check if that tile is a collidable one. If so, stop the player. If not, let him keep going.

Share this post


Link to post
Share on other sites
Thanks nilkn, I'll consider that.

I have some collision code laid out from another game, and I figure I could just use that code, but replace my old collision code with this one. I'll see how it goes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!