For my roleplaying game, I want to detect if an astronaut is on a floor panel in the world. Since the floor panel image is 50x50 pixels and the world cell size is 1 pixel, I am using a heap of complicated code to detect the astronaut. This is what I'm using:
It's probably not the best way to do it, but it's the only way I know how. However, with this code, the astronaut is sensed at places where he shouldn't be able to be sensed. I only want to check the floor panel in front of the player, but the player can be up to 100 pixels away. The sensing offset appears to be wrong, as does the amount of pixels to check. Does anyone know how to fix this?
public Actor checkSquareInFront(Class cls) { int xPos = getX(); int yPos = getY(); Actor actorToCheck = null; if (direction == 0) // rotation 0 { for (int currentPixel = ((yPos + 25) / 50 * 50) - 50; currentPixel < ((yPos + 25) / 50 * 50); currentPixel++) { actorToCheck = checkRow(cls, currentPixel, ((xPos + 25) / 50 * 50), 50); if (actorToCheck != null) { break; } setLocation(xPos, yPos); } } else if (direction == 1) // rotation 90 { for (int currentPixel = ((yPos + 25) / 50 * 50) + 50; currentPixel < ((yPos + 25) / 50 * 50) + 100; currentPixel++) { actorToCheck = checkRow(cls, currentPixel, ((xPos + 25) / 50 * 50) - 50, 50); if (actorToCheck != null) { break; } setLocation(xPos, yPos); } } else if (direction == 2) // rotation 180 { for (int currentPixel = ((yPos + 25) / 50 * 50) - 50; currentPixel < ((yPos + 25) / 50 * 50); currentPixel++) { actorToCheck = checkRow(cls, currentPixel, ((xPos + 25) / 50 * 50) - 100, 50); if (actorToCheck != null) { break; } setLocation(xPos, yPos); } } else if (direction == 3) // rotation 270 { for (int currentPixel = ((yPos + 25) / 50 * 50) - 100; currentPixel < ((yPos + 25) / 50 * 50) - 50; currentPixel++) { actorToCheck = checkRow(cls, currentPixel, ((xPos + 25) / 50 * 50) - 50, 50); if (actorToCheck != null) { break; } setLocation(xPos, yPos); } } setLocation(xPos, yPos); return actorToCheck; } public Actor checkRow(Class cls, int y, int x, int range) { Actor actorToCheck = null; for (int currentPixel = x; currentPixel < x + range; currentPixel++) { if (currentPixel < getWorld().getWidth() && y < getWorld().getHeight()) { setLocation(currentPixel, y); actorToCheck = getOneObjectAtOffset(0, 0, cls); if (actorToCheck != null) { break; } } } return actorToCheck; }