Jump to content
  • Advertisement
Sign in to follow this  
MadMax1992

2d collision detection problems

This topic is 3890 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

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
Advertisement
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
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!