Creating X and Y axis for a game

115 Views Asked by At

Recently I was thinking about creating my own axis x/y, especially 'x', but in that game in which I want to create it, there are no values below 0, because pointX = 0 is on left screen border.

I want to create function which will smoothly count all values depends on our game resolution X.

For example:

parameters: min value, max value, screenX, cursorPosition

if(cursorPosition == screenWidth/2) then
  return 0
end

When cursor position is below screenWidth/2, function will smoothly count value between -0 and min value (min value will be, when cursor position = 0)

and the same when cursor pos is above screenWidth/2, function will smoothly count value between 0 and max value (max value will be when cursor position = our screenX)

Can anyone explain to me, how can I reach an effect like that? :)

Regards

1

There are 1 best solutions below

0
Spektre On

use linear interpolation to change the dynamic range. Let assume your view has xs,ys resolution and point (0,0) is top left corner. so the dynamic range per each axis is:

x = <0,xs-1>
y = <0,ys-1>

and you want to change it to:

x' = <minx,maxx>
y' = <miny,maxy>

So do this:

x' = minx + x*(maxx-minx)/(xs-1)
y' = miny + y*(maxy-miny)/(ys-1)

and if you need to go back for any reason:

x = (x'-minx)*(xs-1)/(maxx-minx)
y = (y'-miny)*(ys-1)/(maxy-miny)

where (minx,miny) is top left corner and (maxx,maxy) is bottom right corner. If you want to change also the sign of any axis then you can as minx<maxx is not required for this so just swap the initial values so minx>maxx.

Not coding in lua but If your values are floating then beware integer rounding while mixing integers and floats together.