Page 1 of 1

Transforming coordinates to screen coordinates

Posted: Mon Dec 14, 2015 8:11 am
by ElmuKelmuZ
I have my own Vector2-pseudoclass I use for positions and sizes of objects (https://gist.github.com/Elmuti/a26cdf288752698a58d0).

I should obviously have this coordinate system separate from rendering,so i need to transform these v2(x, y) -coordinates to screen coordinates that I can use

Code: Select all

love.graphics.draw
with.

I'm new to games, so if anyone could explain to me how I could achieve this transformation I'd really appreciate it :P

Re: Transforming coordinates to screen coordinates

Posted: Mon Dec 14, 2015 8:38 am
by micha
The simplest solution is to go for a constant scaling factor. Let's call the coordinates you handle with your vectors world coordinates and the coordinates on screen screen coordinates. You can transform between the two by simple multiplication:

Code: Select all

function sx,sy = worldToScreen(wx,wy)
  sx = wx * scaleX
  sy = wy * scaleY
end

function wx,wy = screenToWorld(sx,sy)
  wx = sx / scaleX
  wy = sy / scaleY
end
The variables scaleX and scaleY tell you how many pixels one world-coordinate-unit is. For example if you are using 32-by-32 pixel tiles, then scaleX and scaleY would be 32.

The next step would be scrolling. You could incorporate this into the two functions above by adding an offset. However, I suggest you use one of the camera libraries available (for example gamera)