I need simple lookAt() function which I found here in forums, but it works arseways for me ):
I have two actors. First actor1 should look at actor2. I also added two white lines: line1 actors r0 position and line2 where it should look at. Code :
Code: Select all
actor1 = { x = 200, y = 200}
actor2 = { x = 500, y = 50}
function love.load()
actorImg = love.graphics.newImage("actor.png") --64x64
ox = actorImg:getWidth() / 2
oy = actorImg:getHeight() / 2
end
function love.mousepressed( x, y, button )
if button == 'l' then
angle = lookAt(actor1, actor2)
print(angle..' Deg: '..math.deg(angle))
end
end
function lookAt(obj, target)
local a = math.atan2(target.y - obj.y, target.x - obj.x)
return a < 0 and a + math.pi * 2 or a
end
function love.draw()
-- actor1
love.graphics.draw(actorImg, actor1.x, actor1.y, angle, 1,1, ox, oy )
-- actor2
love.graphics.draw(actorImg, actor2.x, actor2.y, 0, 1,1, ox, oy)
-- line1 r0 position
love.graphics.line(actor1.x, actor1.y, actor2.x, actor2.y)
-- line2 lookAt
love.graphics.line(actor1.x, actor1.y, actor1.x, 0)
end
Not sure what I'm doing wrong, so any help more then welcome.