Advanced Sprite Rotation

Hello Coronians,

I have a problem and i cant figure out any solution for it.
In my game, enemys are spawned fron random co-ordinates and the hero is at the center… I need to rotate tge randomly spawned enemies towards the player each time its spawned so that it looks towards the player… So how do i do that?

Help is really appreciated
Thank you

A search yielded several results. If you do more thorough search of the Corona forums you’ll find more resources:

https://forums.coronalabs.com/topic/35087-orbiting-object-around-point-using-touch-non-physics/

https://forums.coronalabs.com/topic/21824-rotate-around-point/

https://forums.coronalabs.com/topic/33905-how-do-i-rotate-an-object-around-a-specific-axis/

https://coronalabs.com/blog/coronageek/corona-geek-119/

@Alex i dont need to rotate around…i just need the enemy to face towards the hero each time its spawned at random coordinates and my hero is at the center…

Something like this (example assumes enemy and player are display objects in your game): 

local math2d = require "plugin.math2d" local vec = math2d.diff( enemy, player ) enemy.rotation = math2d.vector2Angle( vec ) 

@roaminggamer is too quick for me, and already answered, but alternatively:

 enemy.rotation = math.deg(math.atan2(player.y-enemy.y,player.x-enemy.x))

if using images/sprites you’ll need to make sure that they’re aligned to face zero rotation along positive x (otherwise you’ll have to compensate the rotation by whatever built-in non-zero rotation is implied by the image’s facing)

A search yielded several results. If you do more thorough search of the Corona forums you’ll find more resources:

https://forums.coronalabs.com/topic/35087-orbiting-object-around-point-using-touch-non-physics/

https://forums.coronalabs.com/topic/21824-rotate-around-point/

https://forums.coronalabs.com/topic/33905-how-do-i-rotate-an-object-around-a-specific-axis/

https://coronalabs.com/blog/coronageek/corona-geek-119/

@Alex i dont need to rotate around…i just need the enemy to face towards the hero each time its spawned at random coordinates and my hero is at the center…

Something like this (example assumes enemy and player are display objects in your game): 

local math2d = require "plugin.math2d" local vec = math2d.diff( enemy, player ) enemy.rotation = math2d.vector2Angle( vec ) 

@roaminggamer is too quick for me, and already answered, but alternatively:

 enemy.rotation = math.deg(math.atan2(player.y-enemy.y,player.x-enemy.x))

if using images/sprites you’ll need to make sure that they’re aligned to face zero rotation along positive x (otherwise you’ll have to compensate the rotation by whatever built-in non-zero rotation is implied by the image’s facing)