B
B
Boris Onofrey2019-01-03 06:53:33
Mathematics
Boris Onofrey, 2019-01-03 06:53:33

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,
  };
}

For values
coordinates = {x: 61, y: 64}
camera = {x: 0, y: 0};
textureSize = 32;

everything is calculated correctly. However, for the values
{x: 1, y: 2}
coordinates = {x: 106, y: 64}
camera = {x: 0, y: 0};
textureSize = 32;

I get the result, although I expect it. I suppose it is all done in a completely different way, but a cursory google did not give any results, because there are big problems with the theory ... =(
{x: 3, y: 2}
{x: 4, y: 2}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2019-01-03
@RedCreepster

because there are big problems with the theory

Math.floor simply discards the fractional part of the argument (in case the argument is positive , deal with the negative for yourself):
Math.floor(3.3125) == 3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question