Libgdx碰撞麻烦
Libgdx collision trouble
我正在制作自上而下的角色扮演游戏。我几乎已经完成了移动系统,但我似乎无法弄清楚如何检测玩家是否应该阻挡玩家。我的 tmx 文件中有两个对象层,玩家应该在任何地图上与之交互:NPC 层和碰撞层。我试过 NPC 是瓦片对象(作为 RectangleMapObjects 出现)并且通常有 1.5 个瓦片高,碰撞对象是多边形,我还有一些瓦片我只想从一侧访问,因此我需要知道是否取决于方向的三个独立点在碰撞内。几个小时后,这就是我所拥有的:
public boolean canWalk(float xc, float yc, String dir)
{
int i = 0;
while (true)
{
try
{
RectangleMapObject temp = (RectangleMapObject) ents.get(i);
if (!temp.equals(player))
{
if (dir.equals("s"))
{
float cy = ((y) * 16) - 2;
float cx = ((x) * 16) + 2;
return !(temp.getRectangle().contains(cx + 6, cy + 8));
}
else if (dir.equals("n"))
{
float cy = ((y + 1) * 16) + 2;
float cx = ((x) * 16) + 2;
return !(temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx + 6, cy) && temp.getRectangle().contains(cx + 12, cy));
}
else if (dir.equals("e"))
{
float cy = ((y) * 16) + 2;
float cx = ((x + 1) * 16) + 2;
return !(temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx, cy + 6) && temp.getRectangle().contains(cx, cy + 12));
}
else if (dir.equals("w"))
{
float cy = ((y) * 16) + 2;
float cx = ((x) * 16) - 2;
return !(temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx, cy + 6) && temp.getRectangle().contains(cx, cy + 12));
}
}
}
catch (IndexOutOfBoundsException e)
{
break;
}
i++;
}
versionid = "" + i + ",";
i = 0;
while (true)
{
try
{
PolygonMapObject temp = (PolygonMapObject) cols.get(i);
if (dir.equals("s"))
{
float cy = ((y) * 16) - 2;
float cx = ((x) * 16) + 2;
return !(temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx + 6, cy) && temp.getPolygon().contains(cx + 12, cy));
}
else if (dir.equals("n"))
{
float cy = ((y + 1) * 16) + 2;
float cx = ((x) * 16) + 2;
return !(temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx + 6, cy) && temp.getPolygon().contains(cx + 12, cy));
}
else if (dir.equals("e"))
{
float cy = ((y) * 16) + 2;
float cx = ((x + 1) * 16) + 2;
return !(temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx, cy + 6) && temp.getPolygon().contains(cx, cy + 12));
}
else if (dir.equals("w"))
{
float cy = ((y) * 16) + 2;
float cx = ((x) * 16) - 2;
return !(temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx, cy + 6) && temp.getPolygon().contains(cx, cy + 12));
}
}
catch (IndexOutOfBoundsException e)
{
break;
}
i++;
}
versionid = versionid + i;
return true;
}
它总是return是真的
解决了我自己的问题愚蠢错误每张支票都有保证 return 而不是仅在必要时 returning
我发现这里是功能代码,供任何需要的人使用
public boolean canWalk(String dir)
{
int i = 0;
while (true)
{
try
{
RectangleMapObject temp = (RectangleMapObject) ents.get(i);
if (!temp.equals(player))
{
if (dir.equals("s"))
{
float cy = ((y) * 16) - 2;
float cx = ((x) * 16) + 2;
if (temp.getRectangle().contains(cx + 6, cy + 8))
{
return false;
}
}
else if (dir.equals("n"))
{
float cy = ((y + 1) * 16) + 2;
float cx = ((x) * 16) + 2;
if (temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx + 6, cy) && temp.getRectangle().contains(cx + 12, cy))
{
return false;
}
}
else if (dir.equals("e"))
{
float cy = ((y) * 16) + 2;
float cx = ((x + 1) * 16) + 2;
if (temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx, cy + 6) && temp.getRectangle().contains(cx, cy + 12))
{
return false;
}
}
else if (dir.equals("w"))
{
float cy = ((y) * 16) + 2;
float cx = ((x) * 16) - 2;
if (temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx, cy + 6) && temp.getRectangle().contains(cx, cy + 12))
{
return false;
}
}
}
}
catch (IndexOutOfBoundsException e)
{
break;
}
i++;
}
i = 0;
while (true)
{
try
{
PolygonMapObject temp = (PolygonMapObject) cols.get(i);
if (dir.equals("s"))
{
float cy = ((y) * 16) - 2;
float cx = ((x) * 16) + 2;
if (temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx + 6, cy) && temp.getPolygon().contains(cx + 12, cy))
{
return false;
}
}
else if (dir.equals("n"))
{
float cy = ((y + 1) * 16) + 2;
float cx = ((x) * 16) + 2;
if (temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx + 6, cy) && temp.getPolygon().contains(cx + 12, cy))
{
return false;
}
}
else if (dir.equals("e"))
{
float cy = ((y) * 16) + 2;
float cx = ((x + 1) * 16) + 2;
if (temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx, cy + 6) && temp.getPolygon().contains(cx, cy + 12))
{
return false;
}
}
else if (dir.equals("w"))
{
float cy = ((y) * 16) + 2;
float cx = ((x) * 16) - 2;
if (temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx, cy + 6) && temp.getPolygon().contains(cx, cy + 12))
{
return false;
}
}
}
catch (IndexOutOfBoundsException e)
{
break;
}
i++;
}
return true;
}
请注意,我回答这个问题是为了关闭它
我正在制作自上而下的角色扮演游戏。我几乎已经完成了移动系统,但我似乎无法弄清楚如何检测玩家是否应该阻挡玩家。我的 tmx 文件中有两个对象层,玩家应该在任何地图上与之交互:NPC 层和碰撞层。我试过 NPC 是瓦片对象(作为 RectangleMapObjects 出现)并且通常有 1.5 个瓦片高,碰撞对象是多边形,我还有一些瓦片我只想从一侧访问,因此我需要知道是否取决于方向的三个独立点在碰撞内。几个小时后,这就是我所拥有的:
public boolean canWalk(float xc, float yc, String dir)
{
int i = 0;
while (true)
{
try
{
RectangleMapObject temp = (RectangleMapObject) ents.get(i);
if (!temp.equals(player))
{
if (dir.equals("s"))
{
float cy = ((y) * 16) - 2;
float cx = ((x) * 16) + 2;
return !(temp.getRectangle().contains(cx + 6, cy + 8));
}
else if (dir.equals("n"))
{
float cy = ((y + 1) * 16) + 2;
float cx = ((x) * 16) + 2;
return !(temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx + 6, cy) && temp.getRectangle().contains(cx + 12, cy));
}
else if (dir.equals("e"))
{
float cy = ((y) * 16) + 2;
float cx = ((x + 1) * 16) + 2;
return !(temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx, cy + 6) && temp.getRectangle().contains(cx, cy + 12));
}
else if (dir.equals("w"))
{
float cy = ((y) * 16) + 2;
float cx = ((x) * 16) - 2;
return !(temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx, cy + 6) && temp.getRectangle().contains(cx, cy + 12));
}
}
}
catch (IndexOutOfBoundsException e)
{
break;
}
i++;
}
versionid = "" + i + ",";
i = 0;
while (true)
{
try
{
PolygonMapObject temp = (PolygonMapObject) cols.get(i);
if (dir.equals("s"))
{
float cy = ((y) * 16) - 2;
float cx = ((x) * 16) + 2;
return !(temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx + 6, cy) && temp.getPolygon().contains(cx + 12, cy));
}
else if (dir.equals("n"))
{
float cy = ((y + 1) * 16) + 2;
float cx = ((x) * 16) + 2;
return !(temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx + 6, cy) && temp.getPolygon().contains(cx + 12, cy));
}
else if (dir.equals("e"))
{
float cy = ((y) * 16) + 2;
float cx = ((x + 1) * 16) + 2;
return !(temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx, cy + 6) && temp.getPolygon().contains(cx, cy + 12));
}
else if (dir.equals("w"))
{
float cy = ((y) * 16) + 2;
float cx = ((x) * 16) - 2;
return !(temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx, cy + 6) && temp.getPolygon().contains(cx, cy + 12));
}
}
catch (IndexOutOfBoundsException e)
{
break;
}
i++;
}
versionid = versionid + i;
return true;
}
它总是return是真的
解决了我自己的问题愚蠢错误每张支票都有保证 return 而不是仅在必要时 returning
我发现这里是功能代码,供任何需要的人使用
public boolean canWalk(String dir)
{
int i = 0;
while (true)
{
try
{
RectangleMapObject temp = (RectangleMapObject) ents.get(i);
if (!temp.equals(player))
{
if (dir.equals("s"))
{
float cy = ((y) * 16) - 2;
float cx = ((x) * 16) + 2;
if (temp.getRectangle().contains(cx + 6, cy + 8))
{
return false;
}
}
else if (dir.equals("n"))
{
float cy = ((y + 1) * 16) + 2;
float cx = ((x) * 16) + 2;
if (temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx + 6, cy) && temp.getRectangle().contains(cx + 12, cy))
{
return false;
}
}
else if (dir.equals("e"))
{
float cy = ((y) * 16) + 2;
float cx = ((x + 1) * 16) + 2;
if (temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx, cy + 6) && temp.getRectangle().contains(cx, cy + 12))
{
return false;
}
}
else if (dir.equals("w"))
{
float cy = ((y) * 16) + 2;
float cx = ((x) * 16) - 2;
if (temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx, cy + 6) && temp.getRectangle().contains(cx, cy + 12))
{
return false;
}
}
}
}
catch (IndexOutOfBoundsException e)
{
break;
}
i++;
}
i = 0;
while (true)
{
try
{
PolygonMapObject temp = (PolygonMapObject) cols.get(i);
if (dir.equals("s"))
{
float cy = ((y) * 16) - 2;
float cx = ((x) * 16) + 2;
if (temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx + 6, cy) && temp.getPolygon().contains(cx + 12, cy))
{
return false;
}
}
else if (dir.equals("n"))
{
float cy = ((y + 1) * 16) + 2;
float cx = ((x) * 16) + 2;
if (temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx + 6, cy) && temp.getPolygon().contains(cx + 12, cy))
{
return false;
}
}
else if (dir.equals("e"))
{
float cy = ((y) * 16) + 2;
float cx = ((x + 1) * 16) + 2;
if (temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx, cy + 6) && temp.getPolygon().contains(cx, cy + 12))
{
return false;
}
}
else if (dir.equals("w"))
{
float cy = ((y) * 16) + 2;
float cx = ((x) * 16) - 2;
if (temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx, cy + 6) && temp.getPolygon().contains(cx, cy + 12))
{
return false;
}
}
}
catch (IndexOutOfBoundsException e)
{
break;
}
i++;
}
return true;
}
请注意,我回答这个问题是为了关闭它