I assume that you have a orthographic camera. (you really can't make a game without it)
You don't need to know its origin to check if is touched, but if you want to, the origin is
float originX = rectangle.x + rectangle.width / 2f;
float originY = rectangle.y + rectangle.height / 2f;
Anyways, you will need to unproject your touch coordinates, this means that you need to translate from the screen position to the camera position (world position).
For this you need a Vector3, is prefered to declare it somewhere outside of the method you are using, because initializing a object in every frame is not recommended.
Vector3 tmpPos=new Vector3();
And now check if your rectangle is touched, anywhere you want.
You have 2 ways to do this.
- Do this in the render / update method, checking the position of the finger / cursor.
public void render(float delta) {
if (Gdx.input.isTouched() { // use .isJustTouched to check if the screen is touched down in this frame
// so you will only check if the rectangle is touched just when you touch your screen.
float x = Gdx.input.getX();
float y = Gdx.input.getY();
tmpPos.set(x, y, 0);
camera.unproject(tmpPos);
// now the world coordinates are tmpPos.x and tmpPos.y
if (rectangle.contains(tmpPos.x, tmpPos.y)) {
// your rectangle is touched
System.out.println("YAAAY I'm TOUCHED");
}
}
}
Options 2, use a input listener, so you only check when a touch event is triggered.
In the show / create method of your screen / game add your input listener
public void show() {
// .....
Gdx.input.setInputProcessor(new InputProcessor() {
// and in the method that you want check if the rectangle is touched
@override
public void touchDown(int screenX, int screenY, int pointer, int button) {
tmpPos.set(screenX, screenY, 0);
camera.unproject(tmpPos);
// now the world coordinates are tmpPos.x and tmpPos.y
if (rectangle.contains(tmpPos.x, tmpPos.y)) {
// your rectangle is touched
System.out.println("YAAAY I'm TOUCHED");
}
}
// and the rest of the methods
// ....
//
});
}
I would use the second way.
let me know if it worked for you.