Page 1 of 1

Using Graphcis Transformation

Posted: Mon Oct 17, 2016 11:22 am
by MasterQ32
Hey!

I'm trying to achieve the conversion of pixel coordinates (mouse) to my local object coordinate system.
Right now the transformation is generated by love.graphics.translate, love.graphics.scale and love.graphics.rotate.

As far as i read in this forums and the documentation, there is no way to get the transformation matrix of love.graphics, isn't it?

I'd like to use something like the following:

Code: Select all

function something()
  -- Converts the origin (0,0) of my transformation to screen coordinates
  local sx,sy = love.graphics.transform(0, 0)
  
  -- Converts the pixel position (100,100) to the local coordinate system
  local lx, ly = love.graphics.transformBack(100, 100)
end
Right now the only two ways i see are monkey patching and doing all the matrix transformations myself with a second library or patching the original love source to get the two functions.

Am i correct with this assumption or is there something i don't see?

Regards
Felix

Re: Using Graphcis Transformation

Posted: Mon Oct 17, 2016 3:27 pm
by pgimeno
MasterQ32 wrote:As far as i read in this forums and the documentation, there is no way to get the transformation matrix of love.graphics, isn't it?
No, but you can track it.

viewtopic.php?p=201884#p201884

Edit: which is what you said, yes.

Edit2: Maybe there's an alternative way using FFI to access the LÖVE functions, but I wouldn't count on that.

Re: Using Graphcis Transformation

Posted: Mon Oct 17, 2016 4:33 pm
by MasterQ32
As a short google doesn't give any useful information to FFI: What exactly is it?

Re: Using Graphcis Transformation

Posted: Mon Oct 17, 2016 5:13 pm
by s-ol
MasterQ32 wrote:As a short google doesn't give any useful information to FFI: What exactly is it?
http://luajit.org/ext_ffi_tutorial.html

Re: Using Graphcis Transformation

Posted: Mon Oct 17, 2016 5:16 pm
by pgimeno
After a closer look at the source, FFI would not help here. It's basically a way to access C functions of libraries from Lua, and that includes LÖVE's functions. But the matrix is private and there's no getter for it. You could only access the method Matrix4.transform() which does it only in one direction, so it's not a solution.

I've implemented love.graphics.getMatrix() and I'm going to prepare a pull request. That will leave the burden of inverting the matrix up to the user, which will hopefully help not abusing backward transforms, or at least to use them rationally because they are going to be much slower unless the inverse matrix is cached in the LÖVE side, which needs a dirty flag and whatnot.

Re: Using Graphcis Transformation

Posted: Mon Oct 17, 2016 8:31 pm
by slime
pgimeno wrote:I've implemented love.graphics.getMatrix() and I'm going to prepare a pull request.
I plan to go about Transform stuff in a different manner, so I probably won't merge that.

Re: Using Graphcis Transformation

Posted: Mon Oct 17, 2016 10:07 pm
by pgimeno
Ok. In case it's useful to someone, here's the patch: https://bitbucket.org/pgimeno/love/comm ... at=default

I'm going to update the post in the other thread to include the backward transform. Will edit this post when done. Even better I'll bump the original thread.

Re: Using Graphcis Transformation

Posted: Tue Oct 18, 2016 3:38 pm
by pgimeno
There's a getter that I missed. Unfortunately, going down the rabbit hole to arrive to the right function, to use it with FFI, seems too difficult. The mangled name of the getter function of the class Matrix is _ZNK4love6Matrix11getElementsEv with g++ in Linux, and it changes depending on the compiler. That needs an implicit instance parameter, which must come from the top of the transformations stack, which has its own complications to be accessed. The plot thickens...

And then there's the difficulties of accessing C++ via FFI, which the author of LuaJIT comments on here: http://lua-users.org/lists/lua-l/2011-07/msg00502.html

If someone dares to try to make an example of using FFI to grab the transform, I'd be curious. I wouldn't discard the possibility of it not being doable at all.

In short, tracking the transform seems the only reasonable way to go for now.