Making a sprite face the mouse
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Making a sprite face the mouse
I want to make one sprite (this is for a top down game) that has a clear front and back, I then want the front of the sprite to always be facing the mouse pointer. Basically the sprite rotating to the mouse, does anyone know how I would go about this?
Re: Making a sprite face the mouse
Basically you need to get the angle between the position of the image and the mouse, you can do it with the function `math.atan2(dy,dx)` where dx and dy are the difference between the position of the mouse and image, here is an example:
But what do you mean by:
Code: Select all
local lg = love.graphics
local GW, GH = lg.getDimensions()
local image = lg.newImage("spaceship.png")
local image_w, image_h = image:getDimensions()
local x, y = GW/2, GH/2
local ox, oy = image_w/2, image_h/2
local angle = 0
function love.update(dt)
local mx, my = love.mouse.getPosition()
-- angle = math.atan2(my-y, mx-x)+math.pi/2 -- get angle + 90° in radian
angle = -math.atan2(mx-x, my-y)+math.pi -- get -angle + 180° in radian
end
function love.draw()
lg.draw(image,x,y,angle,1,1,ox,oy)
end
that has a clear front and back
- Attachments
-
- rotate-to-mouse.love
- (1.07 KiB) Downloaded 47 times
Re: Making a sprite face the mouse
Appreciate the help this should work great! I meant front and back like the sprite clearly has a back and front rather than being kinda ambiguous.Bigfoot71 wrote: ↑Wed Mar 01, 2023 2:13 pm Basically you need to get the angle between the position of the image and the mouse, you can do it with the function `math.atan2(dy,dx)` where dx and dy are the difference between the position of the mouse and image, here is an example:
But what do you mean by:Code: Select all
local lg = love.graphics local GW, GH = lg.getDimensions() local image = lg.newImage("spaceship.png") local image_w, image_h = image:getDimensions() local x, y = GW/2, GH/2 local ox, oy = image_w/2, image_h/2 local angle = 0 function love.update(dt) local mx, my = love.mouse.getPosition() -- angle = math.atan2(my-y, mx-x)+math.pi/2 -- get angle + 90° in radian angle = -math.atan2(mx-x, my-y)+math.pi -- get -angle + 180° in radian end function love.draw() lg.draw(image,x,y,angle,1,1,ox,oy) end
that has a clear front and back
-
- Party member
- Posts: 559
- Joined: Wed Oct 05, 2016 11:53 am
Re: Making a sprite face the mouse
Clearly signaling which way the sprite is facing is more of a visual design dilemma, than anything löve specific. The usual go-to for perfectly top-down games (like Hotline Miami) tends to be that the character's arms clearly point one way or the other, particularly if they're holding a weapon of some sort. The alternative is to have the character's head lean forward in the direction they're facing, as seen in game like GTA2. Basically take an element of your character's design, and put some emphasis on it for clarity.
If in doubt, or you fear that player's might get confused in the heat of the moment, you could even add a toggle setting in the options that adds an arrow or triangle to signify character's facing direction.
If in doubt, or you fear that player's might get confused in the heat of the moment, you could even add a toggle setting in the options that adds an arrow or triangle to signify character's facing direction.
Re: Making a sprite face the mouse
Didnt even consider adding a arrow but that would add to the experience 10 fold, thanks for the hint!MrFariator wrote: ↑Wed Mar 01, 2023 7:31 pm Clearly signaling which way the sprite is facing is more of a visual design dilemma, than anything löve specific. The usual go-to for perfectly top-down games (like Hotline Miami) tends to be that the character's arms clearly point one way or the other, particularly if they're holding a weapon of some sort. The alternative is to have the character's head lean forward in the direction they're facing, as seen in game like GTA2. Basically take an element of your character's design, and put some emphasis on it for clarity.
If in doubt, or you fear that player's might get confused in the heat of the moment, you could even add a toggle setting in the options that adds an arrow or triangle to signify character's facing direction.
Who is online
Users browsing this forum: Ahrefs [Bot] and 12 guests