Question

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:

        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:

My curve

For example I want to see Toast message when user click on this Curve. 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.

Thanks for helping!

Was it helpful?

Solution

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. 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. 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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top