LibGDX overlaps problem

Started by
2 comments, last by KenWill 8 years, 10 months ago

Hey,

So i wanna check if my mouse pointer is over an area or a sprite. So i used the following


if (sprite.getBoundingRectangle().overlaps(Gdx.input.getX(), Gdx.input.getY()) {
    // do something
}

But i can't use the mouse X and Y with overlaps it seems. My first solution for this was to create a rectangle at the mouse X and Y and whenever that overlaps with another rectangle it'll do what i want, but then i found this solution and it seems easier and faster. So which one to use and how can i fix this? .-.

Thanks:)

Advertisement

I'm pretty sure you're not reading the docs. 'overlaps' receives a Rectangle instance. Now what you do have is 'contains(float,float)' which returns true if the rectangle contains the point.

Read the docs: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Rectangle.html

Or better yet, download them and link them to your IDE so they pop up automatically.

Now why LibGDX doesn't has an overloaded 'intersects' method instead of those two is beyond me.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator


if (sprite.getBoundingRectangle().contains(Gdx.input.getX(), Gdx.input.getY()) {
    // do something
}

Maybe use the contains method instead of the overlaps method. Remember if your using a camera u need to unproject the mouse coordinates first before passing them to the contains method.

Unprojection version:


Vector3 mousePosition = camera.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0f));

if (sprite.getBoundingRectangle().contains(mousePosition.x, mousePosition.y) {
    // do something
}

Follow my hobby projects:

Ognarion Commander (Java/LIBGDX): https://github.com/OlafVanSchlacht/ognarion-commander


if (sprite.getBoundingRectangle().contains(Gdx.input.getX(), Gdx.input.getY()) {
    // do something
}

Maybe use the contains method instead of the overlaps method. Remember if your using a camera u need to unproject the mouse coordinates first before passing them to the contains method.

Unprojection version:


Vector3 mousePosition = camera.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0f));

if (sprite.getBoundingRectangle().contains(mousePosition.x, mousePosition.y) {
    // do something
}

Yes i did unprojected the camera and i asked this because well i ask on the libgdx forums and someone told me that i have to use overlaps and that's why i was confused but i figured it out not thanks a bunch.

This topic is closed to new replies.

Advertisement