Player look at mouse

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Player look at mouse

Post 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.
Attachments
main.lua
(24.78 KiB) Downloaded 80 times
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Player look at mouse

Post 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.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: Player look at mouse

Post 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?
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Player look at mouse

Post 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.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: Player look at mouse

Post 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.
Attachments
main.lua
(25.3 KiB) Downloaded 81 times
Post Reply

Who is online

Users browsing this forum: No registered users and 215 guests