Moving sprite toward mouse
Posted: Sun Jan 02, 2011 12:20 am
I'm not quite sure how to do this. I've tried all I can and nothing seems to work. It must be some kind of math that I am not familiar with?
If you split it into X and Y then it becomes very easy algebra. Just test and move accordingly.danlthemanl wrote:I'm not quite sure how to do this. I've tried all I can and nothing seems to work. It must be some kind of math that I am not familiar with?
Code: Select all
image.x = image.x + difference between mouse_x and image.x times a factor < 1
image.y = image.y + difference between mouse_y and image.y times a factor < 1
Code: Select all
-- In load
PlayerX = 400
PlayerY = 300
PlayerVel = -0.2
if love.mouse.isDown("l") then
mouseX = love.mouse.getPosition(x)
mouseY = love.mouse.getPosition(y)
diffX = PlayerX / mouseX - 1
diffY = PlayerY / mouseY - 1
PlayerX = PlayerX + diffX * PlayerVel
PlayerY = PlayerY + diffY * PlayerVel
end
Code: Select all
mouseX = love.mouse.getPosition(x)
mouseY = love.mouse.getPosition(y)
Code: Select all
mouseX,mouseY = love.mouse.getPosition()