General discussion about LÖVE, Lua, game development, puns, and unicorns.
5 posts • Page 1 of 1
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
There's mathematical function that can compute aiming angle based on X and Y coordinates of the target:
It's an alternative variant of arctangent trigonometric function that's customized to work specifically with 2d coordinates.
Code: Select all
local direction = math.atan2 ( self.x - player.x, self.y - player.y )
Hi, raidho3. I tried what you say, but that don't resolved my problem. Here is part of my code:
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
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
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).
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.
Users browsing this forum: No registered users and 21 guests