gpt4 book ai didi

java - Libgdx 碰撞问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:27 25 4
gpt4 key购买 nike

我正在制作一款自上而下的角色扮演游戏。我几乎已经完成了移动系统,但我似乎无法弄清楚如何检测玩家是否应该阻止玩家。我的 tmx 文件中有两个对象层,玩家应该在任何 map 上与之交互:NPC 层和碰撞层。我尝试过 NPC 是图 block 对象(以 RectangleMapObjects 形式出现),通常为 1.5 个图 block 高,碰撞对象是多边形,而且我也有一些图 block ,我只想从一侧访问,因此我需要知道取决于方向的三个独立点是否在碰撞内。几个小时后,这就是我所得到的:

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;
}

它总是返回 true

解决了我自己的问题,愚蠢的错误,每张支票都有保证返回,而不是仅在必要时返回

最佳答案

我发现这里是任何想要它的人的功能代码

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;
}

只是我回答此问题以关闭它的注释

关于java - Libgdx 碰撞问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56923000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com