简体   繁体   中英

Top down 2d sprite collision handling with moving sprites?

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.

Thanks for any help!

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:

http://www.opentk.com/node/869

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.

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

You can download from http://farseerphysics.codeplex.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM