简体   繁体   English

如何在LIBGDX中用曲线检查碰撞检测?

[英]How to check collision detection with curve in LIBGDX?

I'm still a newbie in LIBGDX :( I'm trying to search google but I can't find tutorials. In my application i'm trying not to use textures, but ShapeRenderer. I'm making a curve with this code: 我仍然是LIBGDX的新手:(我正在尝试搜索谷歌,但我找不到教程。在我的应用程序中,我试图不使用纹理,但ShapeRenderer。我用这段代码制作曲线:

        Gdx.gl10.glLineWidth(10);
        render_shape.begin(ShapeType.Line);
        render_shape.setColor(1, 1, 0, 1);
        render_shape.curve(10, 500, 30, 40, 50, 60, 300, 100, 30);
        render_shape.end();

And my curve looks like that: 我的曲线看起来像这样:

我的曲线

For example I want to see Toast message when user click on this Curve. 例如,我想在用户单击此曲线时看到Toast消息。 How can I do it? 我该怎么做?

I was thinking about getting all points from the curve and compare it with touch X and Y, but I don't know how to get points from this curve. 我正在考虑从曲线中获取所有点并将其与触摸X和Y进行比较,但我不知道如何从该曲线获得点。

Thanks for helping! 谢谢你的帮助!

If you want to go through with determining the clicked point color you could do something like this: 如果您想确定点击的颜色点,可以执行以下操作:

Pixmap pixmap;

    void getScreenShot(){
        Gdx.gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 1);
        pixmap = new Pixmap(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), Pixmap.Format.RGBA8888);
        Gdx.gl.glReadPixels(0,0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixmap.getPixels());
    }

    /**
 * Gets the RGB values of the clicked pixel
 * 
 * @param screenX
 *            X clicked position
 * @param screenY
 *            Y clicked position
 * @return Vector3f of the RGB values.
 */
private Vector3 getRGBValues(int screenX, int screenY) {
    float newY = Gdx.graphics.getHeight() - screenY; //if using y up, you need to convert it to y up from a y down since by default all of the clicked cordinates are in a y down system
    int value = colorMap.getPixel((int) screenX, (int) newY);
    int R = ((value & 0xff000000) >>> 24);
    int G = ((value & 0x00ff0000) >>> 16);
    int B = ((value & 0x0000ff00) >>> 8);

    return new Vector3(R, G, B);
}

This code assumes that the camera has not been zoomed and has not been moved. 此代码假定相机尚未缩放且尚未移动。 If either of these conditions are not met some code must be added to account for camera movement and zooming. 如果不满足这些条件中的任何一个,则必须添加一些代码以考虑相机移动和缩放。

So you would first have to call the getScreenShot() method and then the getRGBValues with the clicked coordinates as the parameters. 因此,您首先必须调用getScreenShot()方法,然后使用单击的坐标作为参数调用getRGBValues。 From these RGB values if it is the same as the line you are drawing you know that the user did in fact click the line. 从这些RGB值中,如果它与您绘制的线相同,则您知道用户确实单击了该行。 If not, then the user did not click the line. 如果没有,则用户没有单击该行。 I am not sure how performance friendly this method is... but it should be a method 我不确定这种方法的性能如何......但它应该是一种方法

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

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