Relative mouse movement

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
cuppajoeman
Prole
Posts: 3
Joined: Thu Apr 08, 2021 12:40 am

Relative mouse movement

Post by cuppajoeman »

Hi there,

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".
I think my downfall is to do with callback functions, I don't understand them too well. In my head I understand it as a function that gets run when some event occurs ...

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
I'm having trouble because I need to change the angle of the player on mouse move, but I get this error:

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'
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?
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Relative mouse movement

Post by darkfrei »

The

Code: Select all

function Player:new()

end
Is same as

Code: Select all

function Player.new(self) -- the self is just first argument

end
Does self exist?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
togFox
Party member
Posts: 764
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Relative mouse movement

Post by togFox »

As above - seems like you are using your classes incorrectly. Should

self.rotationAngle

be

Player.rotationAngle or something like that?
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: Relative mouse movement

Post by pgimeno »

The concept is sound. The problem is that love.mousemoved has no idea of who is 'self'. You mean it to refer to the player, but you don't let the function know that.

In the other functions, you're using the colon syntax, therefore there's an implicit parameter called 'self', as darkfrei noted, which will be replaced by the instance when executed. In the Löve callback, you are not using colon syntax (and you CAN'T use it if you want it to work). You need to let it know what object you intend it to mean, because the callbacks are not executed in the context of an object. You're defining it in the same file as other player functions, but that's not a good idea, and it does not make it recognize the meaning of 'self'.

To solve it, you could do the same as with other callbacks: define a Player:mousemoved(x, y, dx, dy) instead, and call it with the right object in main. Don't use the class as togFox has suggested, use the object.

Something like this:
In player.lua:

Code: Select all

function Player:mousemoved(x, y, dx, dy)
  self.rotationAngle = self.rotationAngle + dx * self.sensitivity
end
You haven't shown main.lua, therefore I don't know how you've named your objects, but assuming the instance of the Player class is called playerObject, you could do something like this in main.lua:

Code: Select all

function love.mousemoved(x, y, dx, dy)
  playerObject:mousemoved(x, y, dx, dy)
end
cuppajoeman
Prole
Posts: 3
Joined: Thu Apr 08, 2021 12:40 am

Re: Relative mouse movement

Post by cuppajoeman »

Thanks for everyone's input.

I think the part that made me understand it the most was
because the callbacks are not executed in the context of an object
I added the mousemoved method to the player class, and then call that in the main.lua inside of the love.mousemoved callback.

It works great.

Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests