Page 1 of 1

Rotate a picture of a Zombie

Posted: Sun Nov 17, 2019 2:38 pm
by Pa0DeF0rma
Hi guys, i've got a game where in the center of the screen have a person who shoots the enemies(zombies) that are randomly spawned around the screen. So, the problem is that these pictures are not aiming the center, where the person is. Anybody have a clue how to do that? Thanks for reading

Re: Rotate a picture of a Zombie

Posted: Sun Nov 17, 2019 3:18 pm
by raidho36
There's mathematical function that can compute aiming angle based on X and Y coordinates of the target:

Code: Select all

local direction = math.atan2 ( self.x - player.x, self.y - player.y )
It's an alternative variant of arctangent trigonometric function that's customized to work specifically with 2d coordinates.

Re: Rotate a picture of a Zombie

Posted: Sun Nov 17, 2019 4:11 pm
by Pa0DeF0rma
Hi, raidho3. I tried what you say, but that don't resolved my problem. Here is part of my code:

Code: Select all

	for i, inimigo in ipairs(inimigos) do
		local direcao = math.atan(inimigo.x-hitman.x,inimigo.y-hitman.y)
		love.graphics.draw(inimigo.img, inimigo.x, inimigo.y, direcao)
	end
This FOR are in love.draw(), the variable "inimigo" is the zombie, and the variable "Hitman" is the person in the center of the screen. For each enemy that I draw in the screen i use the formule that you mentioned. Do I doing something wrong? Maybe the parameters in the love.graphics.draw() or anything else? Thanks for your help :awesome:

Re: Rotate a picture of a Zombie

Posted: Sun Nov 17, 2019 5:26 pm
by raidho36
Well what is the nature of your problem? If zombies face the player sideways, just rotate the sprite. If zombies rotate in the opposite direction, try swapping zombie and player coordinates in the formula (subtract zombie position from player position). And I assume that the sprites rotate against its edge - you need to specify sprite center point in the draw function, 6th and 7th arguments (right after the scaling arguments and before skewing arguments).

Re: Rotate a picture of a Zombie

Posted: Mon Nov 18, 2019 2:35 am
by pgimeno
You wrote math.atan. It's math.atan2. To make it easy to remember, just note that atan2 is the one that uses 2 parameters, while atan only uses 1. Lua ignores the others, if you pass them.