Transforming coordinates to screen coordinates

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Posts: 14
Joined: Sat Sep 01, 2012 7:43 pm

Transforming coordinates to screen coordinates

Post by ElmuKelmuZ » Mon Dec 14, 2015 8:11 am

I have my own Vector2-pseudoclass I use for positions and sizes of objects (

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

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

User avatar
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Transforming coordinates to screen coordinates

Post by micha » Mon Dec 14, 2015 8:38 am

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

function wx,wy = screenToWorld(sx,sy)
  wx = sx / scaleX
  wy = sy / scaleY
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)

Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests