Sign in to follow this  
MadMax1992

2d collision detection problems

Recommended Posts

This should be an easy one, I've got some very basic collision detection (no per-pixel stuff yet), it works as following:
for (int spriteCounter = 0; spriteCounter < numSprites; spriteCounter++) {
		if (sprites[spriteCounter].visible == TRUE && sprites[spriteCounter].collision == true) {
			//collision detection

			collisionRect.bottom = (long)(sprites[spriteCounter].posY);
			collisionRect.top = (long)(sprites[spriteCounter].posY + sprites[spriteCounter].height);
			collisionRect.left = (long)(sprites[spriteCounter].posX);
			collisionRect.right = (long)(sprites[spriteCounter].posX + sprites[spriteCounter].width);

			//building under player
			if ((shtrGameClass.collisionPlayer.bottom < collisionRect.top) && (collisionRect.top + worldYOffset < shtrGameClass.collisionPlayer.top)) {
				if (shtrGameClass.collisionPlayer.left < collisionRect.right && shtrGameClass.collisionPlayer.right > collisionRect.left) {
				}
			}

			//building above player
			else if ((shtrGameClass.collisionPlayer.top > collisionRect.bottom) && ((collisionRect.bottom + worldYOffset) > shtrGameClass.collisionPlayer.bottom)) {
				if (shtrGameClass.collisionPlayer.left < collisionRect.right && shtrGameClass.collisionPlayer.right > collisionRect.left) {
					worldYOffset = -(collisionRect.bottom - (shtrGameClass.collisionPlayer.top));
				}
			}
			//building left of player
			if ((shtrGameClass.collisionPlayer.left > collisionRect.right) && (collisionRect.right + worldXOffset > shtrGameClass.collisionPlayer.right)) {
				if (shtrGameClass.collisionPlayer.top > collisionRect.bottom && shtrGameClass.collisionPlayer.bottom < collisionRect.top) {
					worldXOffset = -(collisionRect.right - (shtrGameClass.collisionPlayer.right));
				}
			}

			//building right of player
			else if ((shtrGameClass.collisionPlayer.right < collisionRect.left) && (collisionRect.left + worldXOffset < shtrGameClass.collisionPlayer.left)) {
				if (shtrGameClass.collisionPlayer.top > collisionRect.bottom && shtrGameClass.collisionPlayer.bottom < collisionRect.top) {
					worldXOffset = -(collisionRect.left - (shtrGameClass.collisionPlayer.left));
				}
			}

		}
	}
The problem is, I'm testing it with a building (made from a .txt file) consisting out of 5 vertical sprites, 6 horizontal sprites, and 12 textures. The left and right side collision detection works perfectly. However, when I try to use the collision detection for the top side of a building or the bottom side of a building. I can go 'between' the sprites when I position the building sprites in the middle of the screen, so I thought: why not turn this:
if (shtrGameClass.collisionPlayer.left < collisionRect.right && shtrGameClass.collisionPlayer.right > collisionRect.left)
into this:
if (shtrGameClass.collisionPlayer.left < collisionRect.right + 2 && shtrGameClass.collisionPlayer.right > collisionRect.left - 2)
but this means I can no long move smoothly along the bottom and top side of the building. It stops at the edge of a sprite. Help!?

Share this post


Link to post
Share on other sites
There's nothing DirectX related here, so I'm moving this out to 'For Beginners' - strikes me that you need to trace through the logic and see just how your data flows through all the conditionals.

Cheers,
Jack

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this