How to get the final position after multiple graphical operation

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.
User avatar
Linkpy
Party member
Posts: 102
Joined: Fri Aug 29, 2014 6:05 pm
Location: France
Contact:

Re: How to get the final position after multiple graphical operation

Post by Linkpy »

Just another question, this time for inverse the matrix.

My matrix is :

Code: Select all

1, 0, 5
0, 1, 5
0, 0, 1
So, a vector (5, 5) become (10, 10).

But, my inverse matrix is :

Code: Select all

 1,  0, 0
 0,  1, 0
-5, -5, 1
Normally, a vector (5, 5) become (0, 0) no ?

Edit : This is my inversion code :

Code: Select all

	---------------------------------------------------------------------------
	--- Get the inversed matrix.
	---
	GetInverse: =>

		-- We calculate the determinant
		det  = @matrix[1] * ( @matrix[5] * @matrix[9] - @matrix[6] * @matrix[8] )
		det -= @matrix[2] * ( @matrix[4] * @matrix[9] - @matrix[6] * @matrix[7] )
		det += @matrix[3] * ( @matrix[4] * @matrix[5] - @matrix[7] * @matrix[8])

		-- If the determinant is not equal to 0
		if det != 0

			-- We calculate the inversed matrix
			a = {
				  ( @matrix[5] * @matrix[9] - @matrix[6] * @matrix[8] ) / det
				 -( @matrix[4] * @matrix[9] - @matrix[6] * @matrix[7] ) / det
				  ( @matrix[4] * @matrix[8] - @matrix[5] * @matrix[7] ) / det
				 -( @matrix[2] * @matrix[9] - @matrix[3] * @matrix[8] ) / det
				  ( @matrix[1] * @matrix[9] - @matrix[3] * @matrix[7] ) / det
				 -( @matrix[1] * @matrix[8] - @matrix[2] * @matrix[7] ) / det
				  ( @matrix[2] * @matrix[6] - @matrix[3] * @matrix[5] ) / det
				 -( @matrix[1] * @matrix[6] - @matrix[3] * @matrix[4] ) / det
				  ( @matrix[1] * @matrix[5] - @matrix[2] * @matrix[4] ) / det
			}

			-- And return it.
			return Matrix a

		-- Else
		else
			-- We return the identity matrix.
			return Matrix!
Founder of NeoShadow Studio. Currently working on the project "Sirami".
github / linkpy
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to get the final position after multiple graphical operation

Post by pgimeno »

I haven't followed your code, but your inverse matrix is wrong. You probably have forgotten to transpose the matrix. The correct inverse matrix in your example is

Code: Select all

 1,  0, -5
 0,  1, -5
 0,  0,  1
User avatar
Linkpy
Party member
Posts: 102
Joined: Fri Aug 29, 2014 6:05 pm
Location: France
Contact:

Re: How to get the final position after multiple graphical operation

Post by Linkpy »

Edit : Oh yes, I didn't see the second video, sorry and thanks !

Edit 2 : Unittest passed, thanks !
Founder of NeoShadow Studio. Currently working on the project "Sirami".
github / linkpy
marco.lizza
Citizen
Posts: 52
Joined: Wed Dec 23, 2015 4:03 pm

Re: How to get the final position after multiple graphical operation

Post by marco.lizza »

@Linkpy, your formula for the determinand is wrong. Should be

Code: Select all

det  = @matrix[1] * (@matrix[5] * @matrix[9] - @matrix[6] * @matrix[8])
det -= @matrix[2] * (@matrix[4] * @matrix[9] - @matrix[6] * @matrix[7])
det += @matrix[3] * (@matrix[4] * @matrix[8] - @matrix[5] * @matrix[7])
Also, when composing the inverse matrix, you have missed to transpose the elements (i.e. convert rows in columns).
User avatar
Linkpy
Party member
Posts: 102
Joined: Fri Aug 29, 2014 6:05 pm
Location: France
Contact:

Re: How to get the final position after multiple graphical operation

Post by Linkpy »

Thanks. I corrected the indexes and the tests passed flawlessly !
Yes, I modified the inverse function to add the transpose. ^^
Founder of NeoShadow Studio. Currently working on the project "Sirami".
github / linkpy
User avatar
idbrii
Prole
Posts: 34
Joined: Sat Jun 12, 2021 5:07 am

Re: How to get the final position after multiple graphical operation

Post by idbrii »

This thread shows up first in search results for finding world position from transform stack, so I thought I'd add:

11.0 added love.graphics.transformPoint and love.graphics.inverseTransformPoint.

That means pgimeno's helpful functions can be reduced to aliases of these functions:

Code: Select all

local function local2scr(x, y)
    return love.graphics.transformPoint(x,y)
end

local function scr2local(x, y)
    return love.graphics.inverseTransformPoint(x,y)
end
Post Reply

Who is online

Users browsing this forum: No registered users and 92 guests