繁体   English   中英

使用libgdx从矩形确定触摸/单击4个多边形

[英]Determine touch/click of 4 Polygons from a Rectangle using libgdx

我目前正在使用libgdx ,并且正在尝试从Rectangle获取4个相等的Polygons

Rectangle myRect = Rectangle(0, 0, 171, 171);

我正在寻找确定代表Rectangle每一侧的4个Polygons 在此处输入图片说明

这是我使用该引擎的第一天,我对几何形状有些不了解,因此我正在寻找可以得到的任何帮助。 本质上,我将使用这些Polygons来确定其中是否存在指定的X,Y对。

谢谢您的帮助。

您可以很容易地找到矩形的中点,只需将高度和宽度平均即可。 从那里您可以手动构造一个多边形,从一个角到另一个角跳到中点。 由于四舍五入,您将失去一些精度,但是如果需要双精度,则可以使用getX()getWidth()

public Polygon[] findTris(Rectangle rectangle){
    //Creating a list of the x points of the rectangle, ordered clockwise.
    new int[] xpoints = new int[5];
    xpoints[0] = rectangle.x;
    xpoints[1] = rectangle.x+rectangle.width;
    xpoints[2] = rectangle.x+rectangle.width;
    xpoints[3] = rectangle.x;
    xpoints[4] = rectangle.x;

    //Doing the same for y points.
    int[] ypoints = new int[5];
    ypoints[0] = rectangle.y;
    ypoints[1] = rectangle.y;
    ypoints[2] = rectangle.y+rectangle.height;
    ypoints[3] = rectangle.y+rectangle.height;
    ypoints[4] = rectangle.y;

    //Finding the midpoint.
    int midx = (rectangle.x+rectangle.width)/2;
    int midy = (rectangle.y+rectangle.height)/2;

    //Creating an array to hold the polygons.
    Polygon[] polys = new Polygon[4];

    //Creating the polygons.
    for(int i = 0; i < 4; i++){
        int[] triXPoints = {xpoints[i], xpoints[i+1], midx};
        int[] triYPoints = {ypoints[i], ypoints[i+1], midy};
        polys[i] = Polygon(xpoints,ypoints,3);
    }
    return polys;
}

现在可以正常工作,但是如果您要做的就是在正方形中找到鼠标位置,则可以使用鼠标地图。 鼠标地图是指您希望能够在其中识别鼠标的每个区域中具有明显不同颜色的图像。您可以将地图存储为BufferedImage并且每当需要查找鼠标所在的区域时,都可以BufferedImage上适当位置的缓冲图像的颜色。
这是想法:
http://i.stack.imgur.com/iFPsl.png

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM