简体   繁体   English

自上而下的2D精灵与移动精灵的碰撞处理?

[英]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. 我正在使用xna 4.0在C#中制作一个简单的自上而下的2d游戏,并且使用的是AABB碰撞检测方法。 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: 本文是基于OpenTK的,它不是基于XNA的,但非常相似,应该易于适应:

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

It takes the minimum translation necessary to push an AABB out of another AABB's way. 它需要最少的翻译才能将AABB推开另一个AABB的方式。 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. 相反,您可以使用一种遍历所有需要比较的AABB并知道已比较哪些AABB的算法来更通用地处理此问题。 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? 当您的玩家的AABB与另一个玩家相撞时,您是希望玩家被“推”到第一个还是敌人? Those details can be up to you in your implementation. 这些细节可以由您自己决定。

Why dont use Farseer Physics Engine that handles collision (and other physics) automatically? 为什么不使用自动处理碰撞(和其他物理学)的Farseer物理引擎? Farseer Physics Engine is a collision detection system with realistic physics responses. Farseer Physics Engine是具有真实物理响应的碰撞检测系统。

Farseer can handle every collision and you can concentrate on your game design! Farseer可以处理所有碰撞,您可以专注于游戏设计! (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/ 您可以从http://farseerphysics.codeplex.com/下载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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