Page 1 of 1

Enemy rotate to player

Posted: Thu Aug 29, 2013 2:58 pm
by IAsep-TrixI
does anyone know how to make a drawn picture (aka an enemy) rotate to the player?

kind of like this
O5621uD.png
O5621uD.png (16.98 KiB) Viewed 292 times

Re: Enemy rotate to player

Posted: Thu Aug 29, 2013 3:46 pm
by raidho36
In enemies code, put computation of an angle towards the player (math.atan2) and use obtained angle for drawing operation.

Re: Enemy rotate to player

Posted: Thu Aug 29, 2013 4:05 pm
by IAsep-TrixI
raidho36 wrote:In enemies code, put computation of an angle towards the player (math.atan2) and use obtained angle for drawing operation.

Code: Select all

enemies.r = math.atan2((enemies.y - player.y), (enemies.x - player.x))
alright, I set math.atan and appended the enemies.r variable into enemies.draw

Code: Select all

love.graphics.draw(enemies.pic,(v.x - 15),v.y,0,enemies.scaleX,enemies.scaleY)
but its rotating somewhere else instead of rotating to the player's direction

Re: Enemy rotate to player

Posted: Thu Aug 29, 2013 4:31 pm
by raidho36
It rotates in opposite direction, starting at 0 degrees (dead right). This is because math.atan2 operates in generic math coordinate grid where Y axis facing up, whereas LÖVE uses computer-graphics-style Y axis that points down. Either use negative of Y argument (just swap player and enemy coordinates), or use negative of function result.

Re: Enemy rotate to player

Posted: Thu Aug 29, 2013 6:43 pm
by IAsep-TrixI
raidho36 wrote:It rotates in opposite direction, starting at 0 degrees (dead right). This is because math.atan2 operates in generic math coordinate grid where Y axis facing up, whereas LÖVE uses computer-graphics-style Y axis that points down. Either use negative of Y argument (just swap player and enemy coordinates), or use negative of function result.
It still doesnt work for some reason.

Re: Enemy rotate to player

Posted: Thu Aug 29, 2013 7:07 pm
by raidho36
I just noticed that you said you used math.atan. You should use math.atan2, although the code snippet uses it. For some reason, drawing snippet doens't have any kind of reference to rotation and it's explicitly set to 0. I suggest you posting the real code you use.

Re: Enemy rotate to player

Posted: Fri Aug 30, 2013 6:17 am
by BlackBulletIV
raidho36 wrote:This is because math.atan2 operates in generic math coordinate grid where Y axis facing up, whereas LÖVE uses computer-graphics-style Y axis that points down.
Oh, so that's why angles move clockwise in LÖVE.

Re: Enemy rotate to player

Posted: Fri Aug 30, 2013 1:02 pm
by IAsep-TrixI
raidho36 wrote:I just noticed that you said you used math.atan. You should use math.atan2,
Ah, it works now. Thanks raidho!