Monster direction math problem

Hello I have a problem of making sure the monster is facing the correct direction towards the player locations. The (x,y) coordinates for every object start is in the top left? Right now the code is as simple as:

if player.x \<= game.sMob\_1.x and player.y \>= game.sMob\_1.y then game.sMob\_1.rotation = 90 -- left elseif player.x \>= game.sMob\_1.x and player.y \>= game.sMob\_1.y then game.sMob\_1.rotation = 270 -- right elseif player.x \<= game.sMob\_1.x and player.y \<= game.sMob\_1.y then game.sMob\_1.rotation = 0 -- down elseif player.x \>= game.sMob\_1.x and player.y \<= game.sMob\_1.y then game.sMob\_1.rotation = 180 -- up end

Obviously there are some logical problems for certain angles, could someone direct me to a theory post on this topic, I understand its 100% a math problem and I need to designate specific spots for the up, down, left, right and take into account both the physics bodies on their locations which is needing their center mass.

Thank you for your time.

Just an fyi in case you don’t know, you can also change the way an object is facing (flip the object) using the scale properties.

For instance:

MyObject.xScale = -1

That will flip an object so it is facing from right to left for instance (presuming it was facing right originally)

@azmar here is some simple vector maths which should get your monster rotating in the right direction

local mathdeg = math.deg local mathatan2 = math.atan2 &nbsp; local function findRotation(obj1,obj2) &nbsp;&nbsp; &nbsp;return mathdeg(mathatan2(obj2.y-obj1.y,obj2.x-obj1.x))-90 end &nbsp;

an example

&nbsp; local mathdeg = math.deg local mathatan2 = math.atan2 &nbsp; local boxes = { &nbsp;&nbsp; &nbsp;display.newRect( 25, 50, 25, 25 ), &nbsp;&nbsp; &nbsp;display.newRect( 460, 305, 25, 25 ), } local function findRotation(obj1,obj2) &nbsp;&nbsp; &nbsp;return mathdeg(mathatan2(obj2.y-obj1.y,obj2.x-obj1.x))-90 end transition.to( boxes[1], {x = 400, delta = true,time = 5000} ) Runtime:addEventListener( "enterFrame", function () &nbsp;&nbsp; &nbsp;boxes[2].rotation = findRotation(boxes[1],boxes[2]) end )

Regards, Lemon

Just an fyi in case you don’t know, you can also change the way an object is facing (flip the object) using the scale properties.

For instance:

MyObject.xScale = -1

That will flip an object so it is facing from right to left for instance (presuming it was facing right originally)

@azmar here is some simple vector maths which should get your monster rotating in the right direction

local mathdeg = math.deg local mathatan2 = math.atan2 &nbsp; local function findRotation(obj1,obj2) &nbsp;&nbsp; &nbsp;return mathdeg(mathatan2(obj2.y-obj1.y,obj2.x-obj1.x))-90 end &nbsp;

an example

&nbsp; local mathdeg = math.deg local mathatan2 = math.atan2 &nbsp; local boxes = { &nbsp;&nbsp; &nbsp;display.newRect( 25, 50, 25, 25 ), &nbsp;&nbsp; &nbsp;display.newRect( 460, 305, 25, 25 ), } local function findRotation(obj1,obj2) &nbsp;&nbsp; &nbsp;return mathdeg(mathatan2(obj2.y-obj1.y,obj2.x-obj1.x))-90 end transition.to( boxes[1], {x = 400, delta = true,time = 5000} ) Runtime:addEventListener( "enterFrame", function () &nbsp;&nbsp; &nbsp;boxes[2].rotation = findRotation(boxes[1],boxes[2]) end )

Regards, Lemon