## Rotate a picture of a Zombie

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Pa0DeF0rma
Prole
Posts: 5
Joined: Fri Nov 15, 2019 8:48 pm

### Rotate a picture of a Zombie

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

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Rotate a picture of a Zombie

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.

Pa0DeF0rma
Prole
Posts: 5
Joined: Fri Nov 15, 2019 8:48 pm

### Re: Rotate a picture of a Zombie

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

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Rotate a picture of a Zombie

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).

pgimeno
Party member
Posts: 2310
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Rotate a picture of a Zombie

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.

### Who is online

Users browsing this forum: No registered users and 21 guests