Search code examples
luamouselove2dpong

Making object locked to a circle "follow" mouse


I'm making a circle pong game (where there's only one paddle and you move in a circle with the ball spawning in the middle of the circle)

Currently, I've almost everything down but I feel like using the keyboard to move the paddle is too slow and I cannot find any "middle" value where it's not too fast or slow

I saw some other examples of this game using the mouse to control the paddle but I have no idea how to do such a thing.

This is my update function for the paddle (sorry if the way I handle updating is ugly):

pad:update(

function(dt,self)
    local mouseX,mouseY=love.mouse.getPosition()
    self.rot=math.atan2((400 - self.x), -(300 - self.y))
    --self.rot=math.atan2((mouseX - self.x), -(mouseY - self.y))
    self.x = circleRadius*math.cos(self.r) + self.orgX;
    self.y = circleRadius*math.sin(self.r) + self.orgY;
    if love.keyboard.isDown("a") then
        self.r=self.r+4*dt
    end
    if love.keyboard.isDown("d") then
        self.r=self.r-4*dt
    end
end,
dt
)

The above code is inside love.update and sends a function as an argument to pads update function, which then calls that function, giving it the correct arguments like self and dt.

r is basically the position of the paddle on the circle


Solution

  • Got it by setting the current position on the circle (r) to the angle between mouse and the centre of the circle (which in my case is the centre of the window which is 800x600)

    self.r=math.atan2((400-mouseX), -(300-mouseY))+math.rad(90)