Page 1 of 1
Player look at mouse
Posted: Tue Feb 03, 2015 1:28 pm
by Bindie
Hey, I tried to find how to make the player look at mouse, I found the atan2 function:
Code: Select all
function angle(x1, y1, x2, y2)
return math.atan2(y2-y1, x2-x1)
end
Then I searched some threads and found that love.translate does not translate the mouse coordinates.
So to make it simple, how do I translate mouse coordinates?
What I tried to do was adding the offset that I don't really know where they came from (I just got it from printing my player.x and y and mouse.x and y and looking at the difference) and before putting mouse.x and mouse.y into the new player.angle player.x movement and player.y movement was added to the mouse.x and mouse.y and I thought that would update the mouse.x and mouse.y though still my mouse.x and mouse.y is boxed.
I have attached the code.
Re: Player look at mouse
Posted: Tue Feb 03, 2015 2:45 pm
by micha
Bindie wrote:So to make it simple, how do I translate mouse coordinates?
I like the concept of having two functions screenToWorld(sx,sy) and worldToScreen(wx,wy). Once you have these, you can easily transform any coordinate (including mouse coordinates) between the screen and the in-game world.
The content of the two functions very much depends on the type of camera movement that you allow. If it is only translation, it is the easiest: If you do translate(tx,ty), then the world-point (wx,wy) gets the screen-point (wx+tx,wy+ty) so the functions look like this:
Code: Select all
function screenToWorld(sx,sy)
return sx-tx,xy-ty
end
function worldToScreen(wx,wy)
return wx+tx,wy+ty
end
If you also include scaling, rotating and shearing, this becomes more complex.
Re: Player look at mouse
Posted: Tue Feb 03, 2015 4:36 pm
by Bindie
Lets say:
Code: Select all
function screenToWorld(sx,sy)
return sx-tx,xy-ty
end
function worldToScreen(wx,wy)
return wx+tx,wy+ty
end
So world to screen is what I would put in the love.translate function as wx and wy?
Re: Player look at mouse
Posted: Tue Feb 03, 2015 9:18 pm
by micha
In your draw-function you have:
Code: Select all
love.graphics.translate(-player.x + 400 - 32.5, -player.y + 300)
replace this by these lines:
Code: Select all
tx = -player.x + 400-32.5
ty = -player.y + 300
love.graphics.translate(tx,ty)
To get the angle between player and mouse, you do this:
Code: Select all
mx,my = love.mouse.getPosition() -- mouse coordinates on screen
wx,wy = screenToWorld(mx,my) -- calculate position of the mouse in "world coordinates"
angleBetweenPlayerAndMouse = angle(player.x,player.y,wx,wy) -- calculate angle
In the last line, use the angle function that you wrote in your first post.
Re: Player look at mouse
Posted: Wed Feb 04, 2015 3:36 pm
by Bindie
Now my mouse coordinates are translated. Thanks. Seems my player can look left and right howewer not down, why? I added the code as specified.
Edit: Nevermind, I found it.
In this function:
Code: Select all
function screenToWorld(sx,sy)
return sx-tx,xy-ty -- Replace xy with sy
end
Maybe this thread will be used again, I hope so.