[Collision Detection] Thinks it's colliding even though it isn't

Hey,

I’ve been trying to solve this little bug in my code for a while now, but I can’t seem to figure it out yet. When running my program the player entity correctly falls to the ground due to the gravitational pull, but if he walks off of the ground then he doesn’t fall.

As far as I can tell, the two collision checking if-else statements or something regarding them is probably messing up. The game seems to think that the player is colliding with something and therefore won’t allow it to fall anymore.

If someone cares to check out the code and see if you can figure out a possible solution, thanks.

Link to the two classes I believe are causing/are related to this issue:

Here is the compiled game:
https://mega.co.nz/#!wg11xJgb!PAbhBae2qjtM_N8Ka036_Me4f0p686n4AmV6nAv9vsM

It looks like you are only checking collisions if you are moving?

I think that you need to separately check collisions. Check the y axis and x axis collisions separately from the movement code. The only reference I see to y is when you jump. The only time I see it for x is when you move.

Also, there is no code that alters the y value. I’ll have to go digging around, but it seems like it’ll always be 0.

You can just use JBox2D and chill… 8)

Some people like learning how these things work, recommending a library isn’t solving anything. I’m sure OP knows about all the libraries he could use, he just doesn’t want to which is fine.

Yeah you are right. Its cool to know that kind of stuff and its a good programming habit as well.

Found it! It’s one of the issues with using copy paste!

If you look at your willcollide calls you’re defining your rectangle incorrectly in both cases. The first two parameters are the corners of the rectangle. In this case? You’re setting them as your delta not your position. So your always checking against your bottom left tile. And you’ll never collide on the left edges. :3

So this:


		// Check X-Axis collision:
		if(!super.willCollide(tiles, new Rectangle((int)(tempDX + super.getX()), (int)(tempDY), super.getCurrentSprite().getWidth(), super.getCurrentSprite().getHeight()))) {
			super.setDX(tempDX);
		}

		// Check Y-Axis collision:
		if(!super.willCollide(tiles, new Rectangle((int)(tempDX), (int)(tempDY + super.getY()), super.getCurrentSprite().getWidth(), super.getCurrentSprite().getHeight()))) {
			super.setDY(tempDY);
		} else {
			super.setDY(0.0);
		}

Should be (I changed the second one slightly so that you’re testing at the end X instead of at the previous):


		// Check X-Axis collision:
		if(!super.willCollide(tiles, new Rectangle((int)(tempDX + super.getX()), (int)(super.getY()), super.getCurrentSprite().getWidth(), super.getCurrentSprite().getHeight()))) {
			super.setDX(tempDX);
		}

		// Check Y-Axis collision:
		if(!super.willCollide(tiles, new Rectangle((int)(super.getDX() + super.getX()), (int)(tempDY + super.getY()), super.getCurrentSprite().getWidth(), super.getCurrentSprite().getHeight()))) {
			super.setDY(tempDY);
		} else {
			super.setDY(0.0);
		}

=O I’ve been going over it for hours and that was my only problem… Wow. Thanks for the information, now I’ll know for next time and it works perfectly now!

I never use a library unless I either can’t code what I need or it’s necessary. Collision is simple, when I don’t mess up a piece of code as UprightPath has shown me, so I don’t use a library for it. =P

Glad to be of help!

Also, just as a bit of extra advice: Your collision detection will likely act oddly at times the way it’s written. If the player is less than the delta away from the object that’s going to be collided with it’s going to stop without hitting the object. By that, I mean that its DX will be set to 0 at such a point that its edge will not touch the other object. This will probably be more noticeable when the player is falling from a higher point (e.g. has a higher DX before impact) or if you increase the update time. It will have a sort of falling stutter: It’ll have DX = 0 while above the ground, then have gravity applied until it’s DX is greater than the distance between the player and floor, stop again, then continue. In the end, if the positional information were different then you might even see the player “float” above the ground due to this.

Hmm… Now that you mention it, I do think that it could do that. I’ll rework things if I do come across that issue in the future, but at the moment I should be able to prevent it with carefully chosen values for the gravitational pull, movement speeds, etc…