Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a texture has been touched libgdx WITHOUT scene2d

Tags:

libgdx

I want to know if there is a away to detect a touch on a certain texture without using scene2d. The Gdx.input.isTouched() detects a touch on the WHOLE screen, is there a way i could use something that detects a touch on a texture without scene2d?

like image 979
ilikeyoyo Avatar asked Jul 18 '14 21:07

ilikeyoyo


1 Answers

Of course there is, I also never use screne2d.

in your update method

  if(Gdx.input.isTouched())
{
  Vector3 tmp=new Vector3(Gdx.input.getX(),Gdx.input.getY();
  camera.unproject(tmp);
  Rectangle textureBounds=new Rectangle(textureX,textureY,textureWidth,textureHeight);
  // texture x is the x position of the texture
  // texture y is the y position of the texture
  // texturewidth is the width of the texture (you can get it with texture.getWidth() or textureRegion.getRegionWidth() if you have a texture region
   // textureheight is the height of the texture (you can get it with texture.getHeight() or textureRegion.getRegionhHeight() if you have a texture region
  if(textureBounds.contains(tmp.x,tmp.y))
     {
     // you are touching your texture
     }
}
like image 107
Boldijar Paul Avatar answered Nov 03 '22 04:11

Boldijar Paul