Confused with the descriptions of transformPoint and inverseTransformPoint

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
User avatar
shingoscar
Prole
Posts: 3
Joined: Fri Aug 28, 2020 2:01 pm

Confused with the descriptions of transformPoint and inverseTransformPoint

Post by shingoscar » Mon Oct 26, 2020 2:51 pm

I'm looking for definitions of global / local coordinates and screen-space in the love wiki.

These are the descriptions from the wiki.
love.graphics.transformPoint
Converts the given 2D position from global coordinates into screen-space.
global->screen

love.graphics.inverseTransformPoint
Converts the given 2D position from screen-space into global coordinates.
screen->global

Transform:transformPoint
This effectively converts the given position from global coordinates into the local coordinate space of the Transform.
global->local

Transform:inverseTransformPoint
This effectively converts the given position from the local coordinate space of the Transform into global coordinates.
local->global
The problem is as I learnt the global coordinate also known as world coordinate is an initial untransformed coordinate, the local coordinate is a transformed coordinate. In a 2D world the global coordinate is usually the window coordinate, so the most top left position is 0, 0 in this coordinate.
Orientation-based_coordinates

coord.png
coord.png (6.75 KiB) Viewed 1132 times

So the transformPoint method is converting a point from local to world. Correct me if I'm wrong.

Code: Select all

local t = love.math.newTransform(100, 100, 10)
local globalX, globalY = 0, 0
local localX, localY = t:transformPoint(globalX, globalY)
-- Result: 100, 100

User avatar
pgimeno
Party member
Posts: 2338
Joined: Sun Oct 18, 2015 2:58 pm

Re: Confused with the descriptions of transformPoint and inverseTransformPoint

Post by pgimeno » Mon Oct 26, 2020 4:08 pm

shingoscar wrote:
Mon Oct 26, 2020 2:51 pm
So the transformPoint method is converting a point from local to world. Correct me if I'm wrong.
World coordinates, also called global coordinates, are the coordinates used by your program's world. Think map coordinates in the case of a game that uses a map. Or 3D position coordinates in case of a 3D program.

Screen coordinates are the coordinates used by the screen. They are always 2D, and the upper left corner is always (0, 0) while the lower right corner is always (screenWidth, screenHeight). (ETA: This is not entirely accurate; actually the Z is always 0).

One usually creates a transformation that maps world to screen coordinates. For example:

- Imagine you have a map which is exactly the same width and height of the screen.
- Start with an empty transform: local xform = love.math.newTransform()
- Scale it: xform:scale(2)
- Now, if you apply that transform before drawing your map (love.graphics.replaceTransform(xform)), your map will be drawn zoomed in by a 2x factor, that is, you will see only a quarter of your map.
- And now, if you do: xform:transformPoint(screenWidth/2, screenHeight/2), you will get screenWidth, screenHeight, because screenWidth, screenHeight is the point at which the centre of your map is being drawn.
- If you want to convert screen coordinates (e.g. mouse coordinates are always given in screen coordinates) to your world's coordinates, you can use inverseTransformPoint. For example, xform:inverseTransformPoint(screenWidth, screenHeight) will return (screenWidth/2, screenHeight/2), which is the point within your map where the bottom right corner of the screen is.

I believe the description in the wiki is accurate. Using your analogy, "local" coordinates are actually screen coordinates, while "world" coordinates are map coordinates. So, you should have drawn the window tilted and displaced in your image, to accurately depict what's going on.

ETA: Local coordinates would be coordinates of objects within your world. For example, a player sprite's pixel coordinates could be referred to as the local coordinates of the sprite, and they won't change even if the object is drawn rotated or displaced.

Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests