Java 与intersectLinePolygon相交的多边形和直线行为不正常
我试图检测光标周围的一个小矩形和一个“连接器”之间的碰撞,这基本上只是两点之间的一条线 现在,我决定使用Intersector.intersectLinePolygon(p1,p2,polygon)方法来实现这一点,但是当我运行代码时。每当矩形的X点或Y点与直线的边界框在同一范围内时,它就会检测到碰撞,我真的无法把头绕过它。理想的结果是仅当矩形实际接触直线时才报告碰撞Java 与intersectLinePolygon相交的多边形和直线行为不正常,java,math,vector,libgdx,collision,Java,Math,Vector,Libgdx,Collision,我试图检测光标周围的一个小矩形和一个“连接器”之间的碰撞,这基本上只是两点之间的一条线 现在,我决定使用Intersector.intersectLinePolygon(p1,p2,polygon)方法来实现这一点,但是当我运行代码时。每当矩形的X点或Y点与直线的边界框在同一范围内时,它就会检测到碰撞,我真的无法把头绕过它。理想的结果是仅当矩形实际接触直线时才报告碰撞 Vector3 worldPos = cam.unproject(new Vector3(mouseX, mou
Vector3 worldPos = cam.unproject(new Vector3(mouseX, mouseY, 0));
Rectangle rect = new Rectangle(worldPos.x-4, worldPos.y-4, 8, 8);
Boolean connectorIntersected = false;
for (int i = 0; i < nodeConnectorHandler.getAllConnectors().size(); i++) {
//Getting two points that make the connector line
Node n1 = nodeConnectorHandler.getAllConnectors().get(i).getFrom();
Node n2 = nodeConnectorHandler.getAllConnectors().get(i).getTo();
float x1 = n1.getCX();
float y1 = n1.getCY();
float x2 = n2.getCX();
float y2 = n2.getCY();
//Making a polygon out of rect
Polygon p = new Polygon(new float[] {
rect.getX(),
rect.getY(),
(rect.getX()+8f),
rect.getY(),
(rect.getX()+8f),
(rect.getY()+8f),
rect.getX(),
(rect.getY()+8f)
});
//Checking if the line intersects the polygon (representing the rectangle around the cursor)
if (Intersector.intersectLinePolygon(new Vector2(x1,y1), new Vector2(x2,y2), p))
{
selectedIndex = nodeConnectorHandler.getAllConnectors().get(i).getID();
System.out.println("ConnectorIntersected!");
connectorIntersected = true;
}
break
}
vector3worldpos=cam.unproject(新的Vector3(mouseX,mouseY,0));
矩形矩形=新矩形(worldPos.x-4,worldPos.y-4,8,8);
布尔connectorIntersected=false;
对于(int i=0;i
每次矩形位于这些区域时,代码都会报告冲突(以黄色显示,近似框):
这两个点之间的红线是“连接器”
光标在直线的正下方。它报告了一次跨越整个游戏世界的黄色区域的碰撞
我想我不是没有正确使用这个函数,就是犯了一些明显的错误。或者这就是函数应该做出的反应?我真的不知道。谢谢你的帮助:)好的,显然我用错了方法。相交多边形按预期工作。“我的错”_(ツ)_/''