Archived

This topic is now archived and is closed to further replies.

rm3

Pixel by Pixel Collision detection

Recommended Posts

I''m trying to make a tile-based engine like the one used in Tales of Phantasia and Star Ocean (two great old 16-bit games). Everything is great except for collision detection. The collision detection I''m using now works fine, but only if I move the character 16 pixels (the tile width/height) every time movement is performed. I want to be able to just move the character by 1 or 2 when I press the arrow keys, however when I do so the collision detection becomes oddly inaccurate. Here is some of code in question:

inline void Hero_t::CalcTilePos (void) 
{
	tx = (px) >> 4;
	ty = ((py) >> 4) + 1;
}

bool Hero_t::CanMove (int nx, int ny) {
	if (MAP[ny][nx] == 1)
		return false;

	return true;
}

/* from Hero_t::CheckMovement() */
#define PIXMOVSPD  1  // if PIXMOVSPD == 16, detection is
                      // accurate. But at 1 things get messy

if (key[KEY_LEFT]) {
	direc = WEST;
	if (CanMove(tx - 1, ty))
	{
		px -= PIXMOVSPD;
	}
	movement = 1;
}
 
Any help on making this function I would greatly appreciate. thank you

Share this post


Link to post
Share on other sites
I can tell you what''s happening -- I don''t know if I''ll make any more sense to you than your own code though . When your player is 15 pixels away from a wall (the wall being to the left of the player), your player (according to your code) will be calculated to be in the tile right next to the wall (because I believe the >>4 will truncate the 15/16 fraction of a tile that represents the player''s pixel position). So when you check to see if the player can move left, your code will say no, because the tile to the left is solid.

"All you need to do to learn circular logic is learn circular logic"

Share this post


Link to post
Share on other sites