Page 1 of 2

Transformation Matrices

Posted: Fri Aug 23, 2013 9:16 am
by Mikepicker
Hi everyone,

firstly I wish to thank the LOVE creators, I'm learning to use this game engine but I feel already quite comfortable with it.

Here's my problem:
I'm developing a little fighting style game (a "street fighter" cartoon remake). Every player is composed of 5 separated images (I've used a spritesheet of course): body, two legs and two hands.
When a player goes to the right side of the screen (facing the enemy to his left) I have to reverse the player, so, in my love.draw() I have:

Code: Select all

love.graphics.push()
	
	if (self.reversed) then

		love.graphics.translate(pl.body.x + pl.body.width/2, pl.body.y + pl.body.height/2)
		love.graphics.scale(-1,1)
		love.graphics.translate(-(pl.body.x + pl.body.width/2), -(pl.body.y + pl.body.height/2))
	
	end

--Draw Body parts--

love.graphics.pop()
So I can see the player correctly reversed. But the problem is that the original hand coordinates are not modified! So I can't check the collisions properly (since a player is "hit" when the enemy hand hit his body)!

Could you suggest me a nice solution to this?

Thank you in advance

Re: Transformation Matrices

Posted: Fri Aug 23, 2013 9:27 am
by Robin
When reversing, can't you simply change the positions of the body parts?

Then you can leave push, pop, translate and scale out of it, and draw the body parts simply like this:

Code: Select all

love.graphics.draw(self.left_arm.img, self.left_arm.x, self.left_arm.y, 0, self.reversed and -1 or 1)
(The fifth argument to love.graphics.draw is the scale on the x-axis.)

Re: Transformation Matrices

Posted: Fri Aug 23, 2013 9:34 am
by Mikepicker
Thank you for your answer!

Yes, I could do that, but since I am writing many animations I should write a "double" code: one for the normal animations, and one for the reversed one. I would like to write code once, then apply something (transformation matrix?) to automatically reverse it. Do you think is it possible?

Thanks

Re: Transformation Matrices

Posted: Fri Aug 23, 2013 9:37 am
by Robin
It really depends on what you mean and how you've implemented. We can't be more specific than that until you upload a .love of your game. (Which is, incidentally, one of the rules for asking for help here.)

Re: Transformation Matrices

Posted: Fri Aug 23, 2013 9:52 am
by Mikepicker
I can't upload my love file due to this error: "Sorry, the board attachment quota has been reached."

Re: Transformation Matrices

Posted: Fri Aug 23, 2013 10:05 am
by Robin
In that case, you can upload it somewhere else and link it here.

Re: Transformation Matrices

Posted: Fri Aug 23, 2013 10:19 am
by Mikepicker
https://www.dropbox.com/s/3hnlgrz7cp75i ... umble.love

Here it is :)
As you can see, there are two animations, which are totally equals, but only reversed.

Re: Transformation Matrices

Posted: Fri Aug 23, 2013 11:07 am
by Robin
Okay... I have no suggestion right now. (Maybe someone else would like to jump in here?)

Re: Transformation Matrices

Posted: Fri Aug 23, 2013 12:06 pm
by vrld
Mikepicker wrote:So I can see the player correctly reversed. But the problem is that the original hand coordinates are not modified! So I can't check the collisions properly (since a player is "hit" when the enemy hand hit his body)!
This is expected (and in fact wanted): love.graphics.translate and friends only change how things are drawn on the screen, but they don't dare to touch the underlying "world".
Mikepicker wrote:Could you suggest me a nice solution to this?
I guess you can go at least two routes:
  1. Don't flip in the animation, but the actual coordinates of the body parts. I.e. instead of doing (in player_states.lua)

    Code: Select all

    if self.player.lookAt == "left" then
        self.anim:setReverse(true)
    else
       self.anim:setReverse(false)
    end
    do something like this:

    Code: Select all

    if self.player.lookAt ~= self.player.lookAtLast then
        self:flip() -- flips all coordinates
        self.player.lookAtLast = self.player.lookAt
    end
  2. Have symmetric collision shapes, i.e. if one player punches, look for collisions to the left and to the right of the player. Of course you will run into problems when you want players to be able to punch each other in the back, so you should probably go with (1) in the long run.

Re: Transformation Matrices

Posted: Fri Aug 23, 2013 12:26 pm
by Mikepicker
Perfect! Thank you so much for the suggestion! One last question:
is there something like Duff-Porter (http://ssp.impulsetrain.com/2013-03-17_ ... Modes.html) in order to handle image overlay? (This because legs have to stay behind the body, but at the same time when player kicks the opponent, they have to be in front of the opponent body)