简体   繁体   English

如何绘制没有三角形切口的矩形多边形?

[英]How do I draw a rectangular polygon without a triangular cutout?

I am creating a rectangular polygon that represents the box bounding the player sprite in a simple 2D game.我正在创建一个矩形多边形,代表在一个简单的 2D 游戏中限制玩家精灵的框。 This is the implementation I use:这是我使用的实现:

float[] vertices = new float[] {player.getSprite().getX(), player.getSprite().getY(),
                player.getSprite().getX() + player.getSprite().getWidth(),
                player.getSprite().getY(), player.getSprite().getX(),
                player.getSprite().getY() + player.getSprite().getHeight(),
                player.getSprite().getX() + player.getSprite().getWidth(),
                player.getSprite().getY() + player.getSprite().getHeight()};

        Polygon rPoly = new Polygon(vertices);

        polyBatch.draw(
                new PolygonRegion(new TextureRegion(healthImage), rPoly.getTransformedVertices(),
                new EarClippingTriangulator().computeTriangles(vertices).toArray()),
                player.getSprite().getScaleX(), player.getSprite().getScaleY());

Where the "Sprite" is an actual LibGDX sprite object. When I try to use this implementation I get this as a result:其中“Sprite”是实际的 LibGDX 精灵 object。当我尝试使用此实现时,结果如下:

在此处输入图像描述

How do I get this polygon to be drawn without that triangular cut in it?如何在没有三角形切割的情况下绘制这个多边形?

I will start this answer with a disclaimer: I have never used LibGDX before.我将以免责声明开始这个答案:我以前从未使用过 LibGDX。 N.netheless I can see a potential problem with your code. N.netheless 我可以看到您的代码存在潜在问题。

Let's number the corners of your rectangle as follows:让我们按如下方式对矩形的角进行编号:

   1         2

   3         4

Your array of vertex coordinates includes these corners in the order 1, 2, 3, 4.您的顶点坐标数组包括这些角,顺序为 1、2、3、4。

You are using a polygon object to represent this rectangle.您正在使用多边形 object 来表示此矩形。 Polygon objects will typically expect the vertex coordinates that they are given to go around the polygon in one direction or the other.多边形对象通常会期望它们在一个方向或另一个方向上围绕多边形提供给 go 的顶点坐标。 For example, if you had a polygon with 10 points, how would the Polygon class know in which order to connect the points?例如,如果您有一个包含 10 个点的多边形,那么Polygon class 如何知道这些点的连接顺序? Of course, order 1, 2, 3, 4 is not going around your rectangle in either direction.当然,顺序 1、2、3、4 不会沿任一方向绕过矩形。

Try swapping the order of the last two pairs of coordinates, so that your array of vertices includes the corners in the order 1, 2, 4, 3.尝试交换最后两对坐标的顺序,以便您的顶点数组包含顺序为 1、2、4、3 的角。


Bonus hint for readability: try to format your array of vertices so that it contains one pair of coordinates per line, perhaps something like the following:可读性的额外提示:尝试格式化顶点数组,使其每行包含一对坐标,可能类似于以下内容:

Sprite sprite = player.getSprite();
float[] vertices = new float[] {
    sprite.getX(),                     sprite.getY(),
    sprite.getX() + sprite.getWidth(), sprite.getY(),
    sprite.getX() + sprite.getWidth(), sprite.getY() + sprite.getHeight(),
    sprite.getX(),                     sprite.getY() + sprite.getHeight()
};

To reduce the line length, I've created a local variable for the value of player.getSprite() .为了减少行的长度,我为player.getSprite()的值创建了一个局部变量。 I've guessed the name of the class as Sprite : feel free to adjust this if necessary.我猜 class 的名称是Sprite :如有必要,请随意调整。 You could potentially create further local variables for the values of sprite.getX() , sprite.getY() and so on.您可能会为sprite.getX()sprite.getY()等的值创建更多局部变量。

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

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