简体   繁体   English

libgdx中的三角形碰撞检测

[英]Collision detection in libgdx for triangles

I am making a simple game with libgdx and wanted to add some simple collision detection. 我正在用libgdx制作一个简单的游戏,想添加一些简单的碰撞检测。 I already managed to express my player by using a simple rectangle: 我已经设法通过使用一个简单的矩形来表达我的球员:

boundingBox = new Rectangle(x + 10, y + 10, 13, 21); boundingBox = new Rectangle(x + 10,y + 10,13,21);

but my obstacles seem to be much more complicated. 但我的障碍似乎要复杂得多。 They are supposed to be spikes over which the player can jump and have a triangle shape. 它们应该是玩家可以跳跃并具有三角形形状的峰值。 They pretty much look like this: http://kayin.pyoko.org/iwbtg/forums/Smileys/iwbtg/spikes.gif 它们几乎像这样: http : //kayin.pyoko.org/iwbtg/forums/Smileys/iwbtg/spikes.gif

As far as I noticed there is no triangle shape in libgdx. 据我所知,libgdx中没有三角形。 I already tried using polygons but they seem far too complicated for my purposes. 我已经尝试过使用多边形,但是对于我来说,它们似乎过于复杂。

Is there an easy way to implement an accurate hitbox for them? 有没有一种简单的方法可以为他们实现准确的匹配?

Thanks in advance for reading my post : ) 在此先感谢您阅读我的文章:)

EDIT: Thanks everyone for your responses, everything works fine now, besides drawing my polygons for testing purposes. 编辑:谢谢大家的回应,除了为测试目的绘制我的多边形外,现在一切正常。 When I call 当我打电话

shapeRenderer.polygon(kid.getVertices()); shapeRenderer.polygon(kid.getVertices());

it only draws my polygon in the top left corner, since it's defined as 它只在左上角绘制多边形,因为它的定义是

boundingBox2.setVertices(new float[] { 10, 10, 10, 31, 23, 31, 23, 10 }); boundingBox2.setVertices(new float [] {10,10,10,31,23,31,23,10});

But I move it around in the update method of my kid class by using 但是我通过使用我的孩子类的更新方法来移动它

boundingBox2.setPosition(position.x, position.y); boundingBox2.setPosition(position.x,position.y);

Is there a way to use that position change inside 有没有办法使用内部的位置变化

shapeRenderer.polygon(kid.getVertices()); shapeRenderer.polygon(kid.getVertices()); ?

Anyways I really appreciate your help and after sorting out this problem I will close this thread : ) 无论如何,我非常感谢您的帮助,在解决了这个问题之后,我将关闭此线程:)

Create a Polygon of your rectangle and your triangle. 创建矩形和三角形的多边形。 You can even create a custom polygon if you want to add more advanced shapes. 如果要添加更多高级形状,甚至可以创建自定义多边形。

To convert from rectangle to polygon is very easy, i made a method some months ago 从矩形转换为多边形非常容易,几个月前我提出了一种方法

public static float[] rectangleToVertices(float x, float y, float width,
        float height) {
    float[] result = new float[8];
    result[0] = x;
    result[1] = y;

    result[2] = x + width;
    result[3] = y;

    result[4] = x + width;
    result[5] = y + height;
    result[6] = x;
    result[7] = y + height;

    return result;
}

The good thing about libGDX polygon class is that you can move your polygon or even rotate it, and get the transformed vertices! libGDX多边形类的好处是,您可以移动多边形甚至旋转多边形,并获得变换后的顶点!

Now you can use the Intersector class 现在您可以使用Intersector类

public static boolean overlapConvexPolygons(Polygon p1,
                            Polygon p2)
Check whether specified convex polygons overlap.
Parameters:
p1 - The first polygon.
p2 - The second polygon.
Returns:
Whether polygons overlap.

For testing purposes, after you end your sprite batch do like this 出于测试目的,在结束精灵批处理后,请执行以下操作

batch.end(); // you end your spritebatch
renderer.setProjectionMatrix(camera.combined);
renderer.begin(ShapeType.Line)
renderer.polygon(polygonname.getVertices());
renderer.end();

Now you will be able to see your polygon. 现在,您将可以看到多边形。

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

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