problem with the camera, player not being able to look at the mouse

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Yani354
Prole
Posts: 13
Joined: Wed May 24, 2023 5:21 pm

problem with the camera, player not being able to look at the mouse

Post by Yani354 »

there is probably an easy solution of that, but i can't find it.
The problem is that i have set the camera to look at the player and the player to look at the mouse, but
mouse X and the mouse Y are not updating properly


DEMO:
game.love
(31.87 KiB) Downloaded 47 times
User avatar
marclurr
Party member
Posts: 111
Joined: Fri Apr 22, 2022 9:25 am

Re: problem with the camera, player not being able to look at the mouse

Post by marclurr »

The main problem is you weren't taking the player position and camera translation into account. The below in the draw method your player class draws the player facing left or right and a small square from the centre of the player in the direction of the mouse.

Code: Select all

    
local mx = (love.mouse.getX() + self.x - (love.graphics.getWidth()/2))
local my  = (love.mouse.getY() + self.y - (love.graphics.getHeight()/2))

local dx = mx - self.x
local dy = my - self.y
local dist = math.sqrt(dx*dx + dy*dy)
local normdx = dx/dist
local normdy = dy/dist

if dx >= 0  then 
    love.graphics.draw(self.playerRight,self.x,self.y,0,1,1,32,60)
else
    love.graphics.draw(self.playerLeft,self.x,self.y,0,1,1,32,60)
end

love.graphics.rectangle("fill", self.x + normdx * 96 - 2, self.y + normdy * 96 - 2, 4, 4)

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests