¿Manejo de colisión de sprites 2D de arriba hacia abajo con sprites en movimiento?

I am making a simple top down 2d game in c# with xna 4.0, and I'm using the AABB collision detection method. I'm definitely a beginner with programming so I apologize if this question is very basic.

I want to have multiple enemies move toward the player to attack him, but whenever I add more than one I am having a very difficult time managing collision and handling between them all. How should I manage the simple rectangle intersect collision I am using? I tried setting the enemies to their previous position if a collision returned true, but that did lots of funky stuff.

Gracias por cualquier ayuda!

preguntado el 01 de febrero de 12 a las 04:02

2 Respuestas

Previous position generally won't work the best when handling multiple because even the "previous position" can start becoming invalid.

Instead you could consider PUSHING an object out of the colliding object's way.

This article is in OpenTK which is not XNA-based but is pretty darn similar, should be easy to adapt:


It takes the minimum translation necessary to push an AABB out of another AABB's way. This must be done in multiple passes to make sure no two objects are colliding. However, technically if you already did the Player, for example, you don't need to check the enemies vs. the player. Instead you could handle this more generically, using an algorithm that goes over all your AABB's that need to be compared and knows which ones have already been compared. This part can come in the optimization stage though.

Be conscious of the ordering too; this is where your control for priority of collisions comes into play. When your player's AABB collides with another, would you rather your player get "pushed" FIRST or the enemy? Those details can be up to you in your implementation.

Respondido 01 Feb 12, 09:02

Okay, so I tried to implement this collision handling thing... And it seems to work, but sometimes I get random jumping about by my sprites... I think it has to do with when I'm actually calling the collision handler. When should I be? Also, thanks for your help :) - Ryan Endacott

you need to be calling it in the Update every frame, once per every two AABB's you want collided. - user155407

I did that... And I have it where no objects collide after it is called.. But it ends up fixing in weird ways.. Sprites often switch places. - Ryan Endacott

Aaaah this is such a pain. Nothing seems to be working and I've tried so much! - Ryan Endacott

Why dont use Farseer Physics Engine that handles collision (and other physics) automatically? Farseer Physics Engine is a collision detection system with realistic physics responses.

Farseer can handle every collision and you can concentrate on your game design! (Don't reinvent the wheel :)

It's an open source project and widely used! There are lot of samples out there

Puede descargar desde http://farseerphysics.codeplex.com/

Respondido 02 Feb 12, 00:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.