Slight modification to @Chrisirs code
if (pX >= rectX && pX < rectX + rectwidth
&& pY >= rectY && pY < rectY + rectHeight) {
// …
}
It is probable that the tile’s position and size are stored as integers but the players position [pX, pY]
are stored in floats. If that is the case then there are values of pX
and pY
which will not ‘hit’ any tile. Genrally it is better to use a half-open range to avoid this problem.