Enemy rotate to player

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
IAsep-TrixI
Citizen
Posts: 89
Joined: Mon Aug 12, 2013 4:22 am
Location: Philippines,Asia

Enemy rotate to player

Post by IAsep-TrixI » Thu Aug 29, 2013 2:58 pm

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

kind of like this

Image
An agent of the free

check out the game I'm doing with Jkash!

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

Re: Enemy rotate to player

Post by raidho36 » Thu Aug 29, 2013 3:46 pm

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

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

Re: Enemy rotate to player

Post by IAsep-TrixI » Thu Aug 29, 2013 4:05 pm

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!

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

Re: Enemy rotate to player

Post by raidho36 » Thu Aug 29, 2013 4:31 pm

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.

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

Re: Enemy rotate to player

Post by IAsep-TrixI » Thu Aug 29, 2013 6:43 pm

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!

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

Re: Enemy rotate to player

Post by raidho36 » Thu Aug 29, 2013 7:07 pm

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.

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

Re: Enemy rotate to player

Post by BlackBulletIV » Fri Aug 30, 2013 6:17 am

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.

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

Re: Enemy rotate to player

Post by IAsep-TrixI » Fri Aug 30, 2013 1:02 pm

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!

Post Reply

Who is online

Users browsing this forum: pgimeno and 8 guests