Per Tile Collision
Posted: Wed Sep 30, 2015 8:36 pm
How do i go about having collision for certain parts of a tilemap? Say the player can't pass in a direction, if a wall tile is there.
Thanks.
Thanks.
Sega Megadrive/Genesis development
http://gendev.spritesmind.net/forum/
Code: Select all
int tilepositionx = playerx / 8;
int tilepositiony = playery / 8;
Code: Select all
tileset_properties[0] = 0; // this tile the player can walk through
tileset_properties[1] = 1; // this tile is solid and the player can't walk through
tileset_properties[n] = 0; // and so on and so forth
Code: Select all
int playerx = 10; // x position of the player
int playery = 20; // y position of the player
extern tile tileset[2]; // some tileset stored somewhere. has 2 tiles.
int tileset_properties[2]; // solidness properties of the two tiles
#define TILEMAP_WIDTH 2
#define TILEMAP_HEIGHT 2
int tilemap[TILEMAP_WIDTH * TILEMAP_HEIGHT]; // a 2x2 tilemap
#define TILEPROP_NOTSOLID 0
#define TILEPROP_SOLID 1
int main(void)
{
tileset_properties[0] = TILEPROP_NOTSOLID; // this tile the player can walk through
tileset_properties[1] = TILEPROP_SOLID; // this tile is solid and the player can't walk through
// get the position of the tile that the player is on.
int tilepositionx = playerx / 8;
int tilepositiony = playery / 8;
int tile = tilemap[tilepositionx + (TILEMAP_WIDTH * tilepositiony)];
int tileproperty = tileset_properties[tile];
if (tileproperty == TILEPROP_NOTSOLID)
{
// non-solid tile and can walk through
}
else if (tile property == TILEPROP_SOLID)
{
// solid tile and can't move through
}
}
Hmm, trying to get this to work with the way i did the tilemap drawing, by using VDP_SetMapEX() to draw part of the map on screen.djcouchycouch wrote:Simplest way is to take the tile map you've used to fill the background plane and check the position of your character against it.
To find the corresponding tile of a pixel XY position, divide the x and y by the width and height of the tile. The tiles are most likely 8x8 so to check an object position like the player against the tile map, do this:
That will give you the position of the tile in the tilemap that the player is landing on.Code: Select all
int tilepositionx = playerx / 8; int tilepositiony = playery / 8;
You'll need a way to know if the tile the player lands on is solid or not. You'd need to look at each of the tiles of the tileset and have a table containing their "solidness" property.
To put those things together.Code: Select all
tileset_properties[0] = 0; // this tile the player can walk through tileset_properties[1] = 1; // this tile is solid and the player can't walk through tileset_properties[n] = 0; // and so on and so forth
That's one way to do it. There are many ways.Code: Select all
int playerx = 10; // x position of the player int playery = 20; // y position of the player extern tile tileset[2]; // some tileset stored somewhere. has 2 tiles. int tileset_properties[2]; // solidness properties of the two tiles #define TILEMAP_WIDTH 2 #define TILEMAP_HEIGHT 2 int tilemap[TILEMAP_WIDTH * TILEMAP_HEIGHT]; // a 2x2 tilemap #define TILEPROP_NOTSOLID 0 #define TILEPROP_SOLID 1 int main(void) { tileset_properties[0] = TILEPROP_NOTSOLID; // this tile the player can walk through tileset_properties[1] = TILEPROP_SOLID; // this tile is solid and the player can't walk through // get the position of the tile that the player is on. int tilepositionx = playerx / 8; int tilepositiony = playery / 8; int tile = tilemap[tilepositionx + (TILEMAP_WIDTH * tilepositiony)]; int tileproperty = tileset_properties[tile]; if (tileproperty == TILEPROP_NOTSOLID) { // non-solid tile and can walk through } else if (tile property == TILEPROP_SOLID) { // solid tile and can't move through } }