UQdeco2800 / 2022-studio-1

MIT License
4 stars 0 forks source link

World Position to Tile Position Inaccuracy #252

Closed SamBehm closed 2 years ago

SamBehm commented 2 years ago

Description

The worldToTilePosition function in TerrainComponent is producing incorrect values.

How To Reproduce

Steps to reproduce the behaviour:

  1. Log the world position and mouse position values in StructureService's handleClicks
  2. In game, place a building on each tile on the bottom left diagonal of the map
  3. In the terminal, view the tile positions.

Expected Behaviour

Converting positions down the bottom left diagonal of the map should produce values:

(0,0), (1,0), (2,0) ... (n, 0)

Screenshots

image

image

SamBehm commented 2 years ago

What could work as a fix for this is to use a transformation matrix to transform the mouse position as a vector to tile space. Possible fix:

  1. Add height/2 to y value to account for origin;
  2. Multiply vector by transformation matrix: |cos(-45) -sin(-45)| ( x ) |sin(-45) cos(-45)| (y + h/2)
  3. Multiply by scaling vector to account for perspective: |1 0 | |0 65/33|

[Note; This calculation is for tileSize 1f]