## Enemy rotate to player

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
IAsep-TrixI
Citizen
Posts: 89
Joined: Mon Aug 12, 2013 4:22 am
Location: Philippines,Asia

### Enemy rotate to player

does anyone know how to make a drawn picture (aka an enemy) rotate to the player?

kind of like this

An agent of the free

check out the game I'm doing with Jkash!

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

### Re: Enemy rotate to player

In enemies code, put computation of an angle towards the player (math.atan2) and use obtained angle for drawing operation.

IAsep-TrixI
Citizen
Posts: 89
Joined: Mon Aug 12, 2013 4:22 am
Location: Philippines,Asia

### Re: Enemy rotate to player

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
An agent of the free

check out the game I'm doing with Jkash!

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

### Re: Enemy rotate to player

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.

IAsep-TrixI
Citizen
Posts: 89
Joined: Mon Aug 12, 2013 4:22 am
Location: Philippines,Asia

### Re: Enemy rotate to player

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.
An agent of the free

check out the game I'm doing with Jkash!

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

### Re: Enemy rotate to player

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.

BlackBulletIV
Inner party member
Posts: 1260
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

### Re: Enemy rotate to player

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.

IAsep-TrixI
Citizen
Posts: 89
Joined: Mon Aug 12, 2013 4:22 am
Location: Philippines,Asia

### Re: Enemy rotate to player

raidho36 wrote:I just noticed that you said you used math.atan. You should use math.atan2,
Ah, it works now. Thanks raidho!
An agent of the free

check out the game I'm doing with Jkash!

### Who is online

Users browsing this forum: Bing [Bot] and 6 guests