Using Graphcis Transformation

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
MasterQ32
Prole
Posts: 3
Joined: Sun Oct 16, 2016 8:03 am

Using Graphcis Transformation

Post 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
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Using Graphcis Transformation

Post 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.
MasterQ32
Prole
Posts: 3
Joined: Sun Oct 16, 2016 8:03 am

Re: Using Graphcis Transformation

Post by MasterQ32 »

As a short google doesn't give any useful information to FFI: What exactly is it?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Using Graphcis Transformation

Post 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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Using Graphcis Transformation

Post 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.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Using Graphcis Transformation

Post 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.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Using Graphcis Transformation

Post 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.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Using Graphcis Transformation

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 217 guests