at the moment I develop a game in LibGDX and use rectangle for the collisions.
I need help to detect collisions
with a rectangle. A had already done this, but i will put the rectangle
on its center of texture
. So the rectangle is smaller than the texture
and will be in the middle of this.
I know already, I can set the size
with: rectangle.setSize
and put it with rectangle.setPosition
, but how I can this put in the middle of my texture?
I would not set the Hitrectangle
at the position of the Texture
, but the Texture
at the position of the Hiterectangle
. Why? The Texture
is only an optical presentation of your game. But it has no logic it needs to execute. The Hitrectangle
instead has only logic, and no optical presentation. Collision detection
is (a verry important) part of the logic. So i would set the position of the Hitrectangle
where it should be, and then set the Texture
depending on the position of that.
Lets say the Hitrectangle
is 0.5 unit
s (use the unit you want, maybe meters?) wide and high and the Texture
is 1 unit
wide and high. The position, given by the left, lower corner of the Hitrectangle
is P(5,6), so you have to draw the Texture
at: P(5 - 0.25, 6 - 0.25)
.
Out of this you have the following formulas:
Texture.x = Hitrect.x - ((Texture.width - Hitrect.width) / 2);
Texture.y = Hitrect.y - ((Texture.height - Hitrect.height) / 2);
Hope it helps.