Relative mouse movement
Posted: Thu Apr 08, 2021 12:49 am
Hi there,
I am trying to implement a certain type of mouse movement. Here's how it works
I'm having trouble because I need to change the angle of the player on mouse move, but I get this error:
What I want to do is somehow let this function have access to the instance of player so that it can update the values accordingly.
Can someone help me out with what I should do here?
I am trying to implement a certain type of mouse movement. Here's how it works
- Figure out how much the mouse position has changed only in the x component.
- Take this number, scale it, and then add it to the players "aim angle".
Code: Select all
--! file: player.lua
Player = Object:extend()
function Player:new()
self.x = 300
self.y = 20
self.speed = 500
self.radius = 20
self.resolution = 100
self.sensitivity = 0.1
self.mouseX = 0
self.rotationAngle = 0
self.aimLength = 60
end
function Player:update(dt)
--! key input
x_dir = bool_to_number(love.keyboard.isDown("right")) - bool_to_number(love.keyboard.isDown("left"))
y_dir = bool_to_number(love.keyboard.isDown("down")) - bool_to_number(love.keyboard.isDown("up"))
self.x = self.x + x_dir * self.speed * dt
self.y = self.y + y_dir * self.speed * dt
--! self.rotationAngle = self.rotationAngle + x_change * self.sensitivity
print(self.rotationAngle)
end
function love.mousemoved(X,Y,DX,DY)
x,y,dx,dy = X,Y,DX,DY
self.rotationAngle = self.rotationAngle + dx * self.sensitivity
end
function bool_to_number(value)
return value and 1 or 0
end
function Player:draw()
love.graphics.circle("fill",self.x, self.y, self.radius, self.resolution)
love.graphics.line(self.x, self.y, math.cos(self.rotationAngle) * self.aimLength , math.sin(self.rotationAngle) * self.aimLength)
end
Code: Select all
Error: player.lua:33: attempt to index global 'self' (a nil value)
stack traceback:
[string "boot.lua"]:777: in function '__index'
player.lua:33: in function <player.lua:31>
[string "boot.lua"]:604: in function <[string "boot.lua"]:594>
[C]: in function 'xpcall'
Can someone help me out with what I should do here?