在广场上找到可用空间以产生敌人 [英] Finding free space in square to spawn enemy

查看:67
本文介绍了在广场上找到可用空间以产生敌人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要产生敌人才能看起来不错的区域(它们在栅栏后面),我不希望敌人彼此产生,所以我想知道是否有任何好的方法可以做到这一点?

I have an area where the enemy needs to be spawned to look good (they are behind a fence) and I don't want the enemies to spawn over each other so I was wondering if there is any good way to do this?

http://sv.tinypic .com / r / 2zowwu1 / 8

我创建了一个函数,用于检查下一个方框(下一个敌人)是否适合所有可能的位置(逐像素)像素),但这会减慢速度,因为它是移动应用程序,有时可以很快生成敌人。确定必须有一个不错的公式可以计算下一个敌人可以容纳的不同边界吗?

I created a function that check if the next box (the next enemy) fits in all possible positions (pixel by pixel) but this is to slow as it is a mobile application and the enemies can be spawned rather fast sometimes. Sure there must be a nice formula that can calculate the different boundaries where the next enemy can fit?

推荐答案

如果每个敌人都占据一个表面E,而您所在的区域具有表面S,则E / S是您的区域被一个敌人占领的比例。大概您的E / S非常小,否则您将很难引入很多敌人而不会重叠。因此,如果E / S很小,并且您有N个敌人,而n * E / S仍然很小(例如1/10),那么您很有可能能够随机选择一个位置,然后遍历列表并检查您的新敌人x是否在其他任何敌人的半径R内。如果不是,请尝试新的随机位置。如果您的敌人列表是根据他们的x坐标排序的,则该部分会很快,您平均可以节省一半列表。使用第二张表对y进行排序。通常情况下,性能是以内存为代价的(这是基于可用信息量的,如此处所示)。因此:

If each enemy occupies a surface E, and your area has surface S, then E/S is the fraction of your area occupied by one enemy. Presumably you have E/S very small, otherwise you'll have a hard time putting in many enemies without overlapping. So if E/S is small, and you have N enemies, and n*E/S is still somewhat small (like 1/10th), then you have good chances of being able to randomly select a position, then go over your list of enemies and check whether your new enemy x is within radius R of any other enemy. If not, try new random position. If your list of enemies is ordered according to their x coordinate, then that part will be fast, you'll save half the list on average. Use a second table for sorting along y. As is usually the case, performance comes at the cost of memory (when it is based on amount of information available, as is case here). So:


  1. 通过将它们放置在两个表中来跟踪创建的敌人:一个根据x排序的
    表,另一个根据y排序的表

  2. 选择在哪里生成下一个敌人:x,y

  3. 在表1中找到绑定x:的两个敌人敌方 closer
    比R(敌人的身高)更大(sqrt((x-xi)^ 2 +(y-yi)^ 2)其中
    xi,yi是桌上的敌人吗?检查所需的其他条件。

  4. 如果未通过测试,请选择新的x,y,然后从第4步重复执行

  5. 如果通过,则将索引保存在新敌人的位置将被插入,并使用y表对y重复上述测试:

  6. 在表2中找到绑定y的两个敌人:是新的敌人 closer
    比R(敌人的大小)对它们中的任意一个(sqrt((x-xi)^ 2 +(y-yi)^ 2),其中
    xi,yi是表中的敌人)?

  7. 如果未通过测试,则选择新y,如果仅选择新y,则从步骤6重复。

  8. 如果通过,则将索引插入到要插入新敌人的位置

  9. 在表1的已保存索引1和表2的已保存索引2中插入新敌人

  1. keep track of enemies you create, by putting them in two tables: one table sorted according to x, another table sorted according to y
  2. select where to spawn next enemy: x, y
  3. find in table 1 the two enemies that bound x: is new enemy closer than R (enemy size) to either of them (sqrt((x-xi)^2+(y-yi)^2) where xi,yi is enemy from table)? Check any other conditions required.
  4. if fail test, select new x, y and repeat from step 4
  5. if pass, save index where new enemy will be inserted, and repeat above test for y, using y table:
  6. find in table 2 the two enemies that bound y: is new enemy closer than R (enemy size) to either of them (sqrt((x-xi)^2+(y-yi)^2) where xi,yi is enemy from table)?
  7. if fail test, select new y and repeat from step 6 if only select new y
  8. if pass, save index where new enemy will be inserted
  9. insert new enemy at saved index 1 in table 1 and saved index 2 in table 2

这篇关于在广场上找到可用空间以产生敌人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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