Answer the question
In order to leave comments, you need to log in
How to convert coordinates correctly?
There is a function to convert mouse coordinates to map coordinates
export function canvasCoordinatesToMapCoordinates(
coordinates: ICoordinates,
camera: Camera,
textureSize: number
): ICoordinates {
const coordinateX = Math.floor((coordinates.x + camera.x) / textureSize);
const coordinateY = Math.floor((coordinates.y + camera.y) / textureSize);
return {
x: coordinateX,
y: coordinateY,
};
}
coordinates = {x: 61, y: 64}
camera = {x: 0, y: 0};
textureSize = 32;
{x: 1, y: 2}
coordinates = {x: 106, y: 64}
camera = {x: 0, y: 0};
textureSize = 32;
{x: 3, y: 2}
{x: 4, y: 2}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question