(将对此给予奖励 - 另外,我不能 100% 确定哪些标签与此相关)
我在这里感到非常困惑。我正在尝试使用这个(简化的)模型让我的弓箭手发生碰撞:
然而,正如你所看到的,我的弓箭手似乎在半空中相撞!如果它们掉下来了,我会理解(例如,我没有在碰撞模型中放置足够的“点”),但实际上看起来没有与任何物体发生碰撞,这绝对让我感到困惑。
我正在使用与在客户端中显示模型相同的代码在服务器上加载模型,因此这不会成为问题。我有pastebinned it here无论如何。
然后,我将其添加到三个 int[]
数组中,如下所示:
coordsx = new int[80 * 10];
coordsy = new int[80 * 10];
coordsz = new int[80 * 10];
for (javax.vecmath.Vector3f vec : m.getVertices()){ //Quick note: M is a model. As you can see, I'm just going through all the vertex positions.
coordsx[DELTA+(int) vec.x] = 1;
coordsy[DELTA+(int) vec.y] = 1;
coordsz[DELTA+(int) vec.z] = 1;
}
简单说明:DELTA 是 ((80 * 10)/2)
的值,或者为了节省数学运算,400
。另外,我使用了三个 int[]
,而不是 int[][][]
,因为 int[][][]
导致了我无法修复的 OutOfMemory
。
现在我已经获得了这些坐标数组,我正在使用以下代码来检查它:
for (int x = (int) (location.x + 1); x > location.x - 1; x--){
for (int y = (int) (location.y + 1); y > location.y - 1; y--){
for (int z = (int) (location.z + 1); z > location.z - 1; z--){
distancex = x;
distancez = z;
distancey = y;
try{
int i = 0;
if (owner.type == 0){
if (GameServer.DELTA + distancex > 0 && GameServer.DELTA + distancex < 800 && GameServer.coordsx[(int) (GameServer.DELTA + distancex)] == 1){
if (GameServer.DELTA + distancey > 0 && GameServer.DELTA + distancey < 800 && GameServer.coordsy[(int) (GameServer.DELTA + distancey)] == 1){
if (GameServer.DELTA + distancez > 0 && GameServer.DELTA + distancez < 800 && GameServer.coordsz[(int) (GameServer.DELTA + distancez)] == 1){
i = 1;
}
}
}
}
if (i == 1){
collision = true;
YDown = 0;
}
}catch (ArrayIndexOutOfBoundsException e1){
e1.printStackTrace();
}
}
}
}
if (collision){
System.out.println("Collision!");
}else{
System.out.println("No Collision!");
location.y = location.y-=YDown;
}
位置
是弓箭手X、Y和Z的Vector3f
相对于船舶位置 - 我已经使用调试消息对此进行了检查,并且位置确实返回正确。
如您所见,只有在被检查点的 X、Y 和 Z 位置都有坐标时,变量 i
才会被设置为 1。显然,我也在迭代所有附近的坐标,因为我的玩家不仅仅是一个点。
既然玩家似乎与空气发生碰撞,那么显然有问题。但我找不到什么。
我是否走在正确的轨道上,还是我做的一切都完全错误?如果我走在正确的轨道上,那么这里出了什么问题以及如何修复它?
最佳答案
您的模型有问题。使用三个数组可能会节省内存,但它也会改变模型,创建弓箭手可以碰撞的“阴影”。
假设您在 (1,1,1) 和 (2,2,2) 中有顶点。 使用您的模型,还会有一个位于 (1,2,2) 的顶点以及所有坐标均为 1 或 2 的任何其他组合。
所以,回到绘图板。
也许您可以通过为每个坐标使用一位而不是 32 位 int
来节省内存?
或者您可以更改存储模型的方式。如果您使用 int
的二维数组并存储地板的 z 坐标会怎样?这会将您的世界限制为每个 x,y 坐标的一层(或几层),但会节省大量内存。
关于java - 玩家似乎正在与空气碰撞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28431996/