intersecting issue

Hello,

I am writing a 2D Platformer game with the old feel of the Mario games from the NES (you know the classic game) and I was trying to figure out how to do the jumping on an enemy’s head.

So what I am asking is, how could I check if the bottom Y of the player avatar is intersecting the top y of the enemy avatar?

Not asking you to do my work for me, but a point in the right direction would be great, and I won’t turn away an example :wink:

Hi @Glabay,

Welcome to the Java Gaming forums, it’s nice to see you here. Coming to your question, you can find whether the collision happened in which direction by comparing the intersection width of the rectangles.

So to find out the intersection, you can use the built-in method in the Rectangle class.


Rectangle intersection = ball.getBounds().intersection(brick.getBounds());

Now compare the width and height to find out in which way collision happened.

Hope this helps.

You could check like this:
object A
object B

if (A.yPosition + A.height) > B.height AND (A.xPosition >= B.xPosition AND a.xPosition <= b.xPosition + b.width)
then you have a collision.

else, you dont.

Thanks for the reply, helps a lot.

What the previous replies are describing is called AABB collision detection. More info can be found at this tutorial: http://lazyfoo.net/SDL_tutorials/lesson17/index.php