Lets jump right in,
I am trying to get a character in my game to move towards the closest of a few possible positions.
I figured I could create a table and store in each index the calculation for distance. It would look something like this
local closestSpot = { [1] = math.sqrt(( deltaX1 \* deltaX1 ) + ( deltaY1 \* deltaY1 )), [2] = math.sqrt(( deltaX2 \* deltaX2 ) + ( deltaY2 \* deltaY2 )), [3] = math.sqrt(( deltaX3 \* deltaX3 ) + ( deltaY3 \* deltaY3 )), }
(all the delta variable values are calculated outside the table).
Then, I was searching through the API library and found the table.sort() function.
this seems to indicate that I can then sort the values of the above mentioned table in ascending order which would put the lowest value (closest position) into closestSpot[1]
then I could use my transition function to indicate that the player moves to the coordinates indicated by closestSpot[1]
Just wanted to run this by the community to see if this is a valid way of achieving this, or if someone has come up with a better solution.
Thanks