[英]Java: Iteration to find a coordinate point that hasn't been used?
I am using the following method to try to find a point (coordinate) that hasn't been previously used, and isn't within the bounds of items that have previously used and coordinates. 我正在使用以下方法尝试查找以前未使用过的,不在先前使用过的项目和坐标范围内的点(坐标)。
The way it works is I am rendering "bases" (RTS top-down game), and I am creating two random variable locations for x and y. 它的工作方式是渲染“基础”(RTS自上而下的游戏),并为x和y创建两个随机变量位置。 I pass these, along with the bases texture, into the following method.
我将这些以及基础纹理传递到以下方法中。 The method loops through a list of rectangles that are the rectangles of each previously rendered base.
该方法遍历矩形列表,这些矩形是每个先前渲染的基的矩形。 If the point is within any of the rectangles, the method is called again using a different set of coordinates.
如果该点在任何矩形内,则使用另一组坐标再次调用该方法。 It does this until it finds a set that isn't within a rectangle.
这样做直到找到不在矩形内的集合。 It then adds a new rectangle to the list at these coordinates, and returns them so the game can render a new base.
然后,在这些坐标处将一个新矩形添加到列表中,并返回它们,以便游戏可以渲染新的基准。
However, the bases still overlap. 但是,基础仍然重叠。
Here is the method: 方法如下:
private Point getCoords(int x, int y, Texture t){
for (int i=bases.size()-1; i> -1; i--) {
if (bases.get(i).contains(new Point(x,y))){
x = new Random().nextInt(map.getWidth() * map.getTileWidth());
y = new Random().nextInt(map.getHeight() * map.getTileHeight());
getCoords(x, y, t);
}
}
bases.add(new Rectangle(x,y,t.getImage().getWidth(), t.getImage().getHeight()));
return new Point(x, y);
}
And here is where it is being called: 在这里被称为:
switch(ran){
default:
int x = new Random().nextInt(map.getWidth() * map.getTileWidth());
int y = new Random().nextInt(map.getHeight() * map.getTileHeight());
Point p = getCoords(x, y, temp);
map.generateBase("air", p.x, p.y);
break;
}
Any ideas what is wrong here? 有什么主意在这里吗?
Thanks 谢谢
int x = new Random().nextInt(map.getWidth() * map.getTileHeight());
Maybe a bad copy paste. 也许是不良的复制粘贴。 It may be :
可能是:
int x = new Random().nextInt(map.getWidth() * map.getTileWidth());
In both codes :-D 在两个代码中:-D
There are several problems: 有几个问题:
Your algorithm might be overwritting good coordinates (free ones) with wrong coordinates, you dont have any condition to exit the loop/recursion if you find a good place 您的算法可能会用错误的坐标覆盖正确的坐标(自由坐标),如果找到合适的位置,您没有任何条件退出循环/递归
You are checking for if rectangle contains the point, but later you are adding a rectanble, so it may not contain the point, but the rectangle created later may collide 您正在检查矩形是否包含该点,但是稍后要添加可重构对象,因此它可能不包含该点,但是稍后创建的矩形可能会碰撞
try this 尝试这个
private Point getCoords(int x, int y, Texture t){
boolean found = false;
final int width = map.getTileWidth();
final int height = map.getTileHeight();
while(!found) {
x = new Random().nextInt(map.getWidth() * width);
y = new Random().nextInt(map.getHeight() * height);
for (int i=bases.size()-1; i> -1; i--) {
if (!bases.get(i).intersects(new Rectanble(x,y, width, height))){
found = true;
} else found = false;
}
}
bases.add(new Rectangle(x,y,t.getImage().getWidth(), t.getImage().getHeight()));
return new Point(x, y);
}
*** EDIT: Im not sure if I had to use TileWidth and TileHeight or image width and image height for width
and height
:D ***编辑:我不确定是否必须使用TileWidth和TileHeight或图像宽度和图像高度作为
width
和height
:D
Okay so after some playing around, I found the issue is the rectangles that are saved are saved with a fixed location which means as the map moves, the rectangles don't. 好的,所以在玩了一段时间之后,我发现问题是保存的矩形保存在固定的位置,这意味着随着地图的移动,矩形不会移动。 The fix is to loop through each bases and get the base's map position, rather than screen position, and check against this.
解决方法是遍历每个基地并获取基地的地图位置,而不是屏幕位置,然后进行检查。 Also, I found i was checking for a point in a rectangle, which may be outside the rectangle but leaves my bases overlapping still.
另外,我发现我正在检查矩形中的一个点,该点可能在矩形外部,但我的底部仍然重叠。 So i now check for rectangle-rectangle collision instead
所以我现在检查矩形-矩形碰撞
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.