2D collision detection - best approach?

Hello, everyone! I’ve come to a point that I’m stuck on, and I’m not sure how to continue from here. Currently I’m making a 2D side-scroller with large-ish sprites (64x64 up to 128x128) and I need a few forms of collision detection. I need at least one for collision with projectiles, and one for collision with environments. I know that pixel-perfect is time-consuming, so what would be the next-best thing?

Java2D? Or some other library? And why would you need two systems of detection?

I used regular Rectangle’s for collisions with projectiles. Then some points around the character for environment-collisions. I wrote up an article about it here.

In my game, each sprite/entity then has an array of inner Rectangle’s. So if, a projectile’s Rectangle collides with the outer Rectangle of an entity, I check which of the inner Rectangles (if any) it is colliding with, and deal damage and so forth accordingly. Then I check for projectile-collisions with the terrain.

i have implemented a side scroller in 2D with Java2D look this is my piece of code

each entity extends an abstract class that has this methods implemented


public Rectangle[] getEntityRectangle(){
		Rectangle rect = new Rectangle((int)(position.getPosX() - size.getHalfWidth()), (int)(position.getPosY() - size.getHalfHeigh()), size.getWidth(), size.getHeigh());
		return new Rectangle[]{rect}; 
	}

	public boolean collidedWith(Entity other) {
		Rectangle[] ownRect = getEntityRectangle();
		Rectangle[] otherRect = other.getEntityRectangle();		
		
		for (int i = 0; i < ownRect.length; i++) {
			for (int j = 0; j < otherRect.length; j++) {
				if(ownRect[i].intersects(otherRect[j])){
					return true;
				}
			}
		}
		return false; 	
	}

if u look the implementation of the intersects method of the rect class you will see that i just a check between the basic variables of the two rect to chek,
for my there are not difference between proiectiles and environments, both are Entity so use this method.

Just for optimize and control the collision i have one list that contain all the shot of the enemy versus the player
and i verify the collision hire between player and enemy shot
and i have another list that contain all the environment and ther i verify the collision between player and environment elements
finally i have one for the enemy and i use it to verify player proiectiles versus enemy and enemy to player collisions

I’m using Slick2D, and I need one system for collision with the world (which uses tiles) and one for getting hit by other player’s attacks. I was thinking of using rectangles overlapping certain parts of the body for attacks, but for collision with walls that doesn’t make much sense… also, just to give you an idea of what the character looks like:

http://imageshack.us/a/img571/5690/airs.png

and when he’s running:

http://imageshack.us/a/img201/2518/airanimation.png

(arms are rendered in-game)

The only problem with this is the actual size of the current character(s). Their legs will clip through walls if I make the hitbox too small, and will be able to stand on air if it’s too big. How should I approach it in this case?

As I’ve also described in the article, I used just 1 point for the bottom, to get around the issue of standing in the air. I also wrote that I’d recommend that you use 2, like the Sonic-drawing. The spread between these two points is quite up to you, and leaves you with complete control of how much overlap you want to allow. So you won’t have the problem you’re describing.

You shouldn’t factor in the legs when choosing a width for the collision rectangle. It won’t matter much either way, though. People don’t care whether 3 frames of an animation cut through the tiles of the map. And if you choose to factor in the entire width of the animation, it might leave the player being stopped unnaturally far from walls. Choose some median, or forget about the legs. People won’t care either way. Even though you might :slight_smile: