Reputation: 2058
I started using this solution, but then I noticed it doesn't map correctly when the character is at a coordinate with remainder.
First I get the visible range from the camera. Then I get the mouse position and divide by the block size, here's the problem: I'm assuming there's a whole block at the start of the screen, but in reality there could be 1/4, 2/4 and 3/4 of a block, the same goes for the end of the screen.
How can I correctly perform this conversion in the most efficient manner?
typedef struct {
float x, y;
} Point;
Point camera_screen_coord_to_game(Camera *camera, int x, int y)
{
Range range = camera_get_visible(camera, game_clock_ticks());
int block_size = settings_get_block_size();
Point game_coord;
game_coord.x = range.x0 + x / block_size;
game_coord.y = range.y1 - 1 - y / block_size;
return game_coord;
}
Upvotes: 1
Views: 496
Reputation: 513
Could it be because x, y and block_size are all ints? cast them to float before dividing, and you get the fractional part.
Upvotes: 1