Problem:
I have 32x32 world tiles, and a 64x64 object. I am only drawing visible tiles around the player. The object has its x and y coordinates in the tile world. I am drawing the object after I've drawn the visible area.
I'm drawing the object like this :
for (int y = firstY; y < lastY; y++) {
for (int x = firstX; x < lastX; x++) {
if (object.visible(x, y))
object.draw();
}
}
}
The visible(x,y) method :
public boolean visible(int x, int y) {
if (this.x == x && this.y == y) {
return true;
}
return false;
}
Now what happens is this :
Image 1. Character stands alongside the object, everything is ok.
Image 2. Character triggers screen move left, half of the object gets drawn outside the map.
Image 3. Character triggers screen move right, half of the object should be visible but its not.
I know that this happens because I'm checking only if the upper left corner of the object is visible. I'm stuck. What should I do to remedy this?
Thank you for any ideas.
for
loops seem pretty unnecessary. Can't you just haveif (object.visible(firstX, lastX, firstY, lastY))
which then just doesif (x >= firstX && x <= lastX && y >= firstY && y <= lastY)
? In addition, yourvisible
function could just bereturn <condition here>;
. \$\endgroup\$