XNA 2D鼠标采摘鼠标、XNA

2023-09-07 14:03:32 作者:幼儿园大哥

我使用XNA上一个简单的2D即时战略游戏的工作。现在我已经达到了,我需要能够点击精灵的单位或建筑能够引用与精灵相关联的对象的点。 从研究,我已经做在过去的三天,我已经找到了如何做很多参考资料鼠标采摘,在3D这似乎并不适用于我的情况。 据我所知,另一种方式做,这是简单地拥有所有可选的对象世界的数组,当玩家点击一个精灵它会针对数组中的所有对象的位置鼠标位置。这个问题我有这种方法的缺点是它会成为如果单位和建筑的数量增长到较大的数字相当缓慢。 (这也似乎不是很优雅),所以有一些什么其他的方法我能做到这一点。 (请注意,我还努力通过使用哈希表与精灵位置的对象相关联,并且使用2维阵列的想法,其中重新presents在世界的一个像素阵列中的每个位置。再次他们似乎喜欢做的事情,而笨重的方式。)

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.)

推荐答案

有关多达数百家单位,它应该是速度不够快,简单地做线性搜索为O(n)在所有单位的世界,如果点击区域是圆形或矩形。尤其是看到它会为每点击一次,每帧一次都没有。

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.

有关更详细的解答,here's我的回答对空间分割过类似的问题。在那里,我提时段电网和四叉树为性能优化的潜在结构。

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!