XNA 2D 鼠标拾取 [英] XNA 2D mouse picking

查看:46
本文介绍了XNA 2D 鼠标拾取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XNA 开发一个简单的 2D 实时战略游戏.现在我已经到了需要能够点击一个单位或建筑物的精灵并能够引用与该精灵关联的对象的地步.从我过去三天所做的研究中,我发现了许多关于如何在 3D 中进行鼠标拾取"的参考资料,这些参考资料似乎不适用于我的情况.我知道另一种方法是简单地拥有世界上所有可选"对象的数组,当玩家单击精灵时,它会根据数组中所有对象的位置检查鼠标位置.我用这种方法遇到的问题是,如果单元和建筑物的数量增加到更大的数量,它会变得相当慢.(它似乎也不是很优雅)那么我还有哪些其他方法可以做到这一点.(请注意,我还研究了使用哈希表将对象与精灵位置相关联的想法,并使用二维数组,其中数组中的每个位置代表世界上的一个像素.再一次,它们看起来更像是笨拙的做事方式.)

I'm working on a simple 2D Real time strategy game using XNA. Right now I have reached the point where I need to be able to click on the sprite for a unit or building and be able to reference the object associated with that sprite. From the research I have done over the last three days I have found many references on how to do "Mouse picking" in 3D which does not seem to apply to my situation. I understand that another way to do this is to simply have an array of all "selectable" objects in the world and when the player clicks on a sprite it checks the mouse location against the locations of all the objects in the array. the problem I have with this approach is that it would become rather slow if the number of units and buildings grows to larger numbers. (it also does not seem very elegant) so what are some other ways I could do this. (Please note that I have also worked over the ideas of using a Hash table to associate the object with the sprite location, and using a 2 dimensional array where each location in the array represents one pixel in the world. once again they seem like rather clunky ways of doing things.)

推荐答案

对于多达数百个单位,如果单击区域是圆形或矩形.尤其是看到它每次点击一次,而不是每帧一次.

For up to hundreds of units, it should be fast enough to simply do a linear search O(n) over all the units in the world if the click regions are circles or rectangles. Especially seeing as it will be once per click, not once per frame.

如果您的单位不是圆形或矩形,请先检查边界圆或矩形,如果通过检查更复杂的边界形状.

If your units are not circular or rectangular, check against a bounding circle or rectangle first, and if that passes check against the more complicated bounding shape.

有关更详细的答案,这是我对类似问题的回答 关于空间分区.在那里我提到了桶状网格和四叉树作为性能优化的潜在结构.

For a more detailed answer, here's my answer to a similar question about space partitioning. There I mention bucketed grids and quadtrees as potential structures for performance optimisation.

但是在您进行测试并且确实遇到性能问题之前,您永远不应该进行性能优化!

But you should never do performance optimisation until you have tested and actually do have a performance problem!

这篇关于XNA 2D 鼠标拾取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆