Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller Support #71

Open
44100hertz opened this issue Nov 16, 2022 · 5 comments
Open

Controller Support #71

44100hertz opened this issue Nov 16, 2022 · 5 comments

Comments

@44100hertz
Copy link

Wrap the Web Gamepad API to the Love Joystick API and I would be very happy.

@Davidobot
Copy link
Owner

This should work out the box with SDL? Maybe we're on an old version of it.

@44100hertz
Copy link
Author

🤔 Maybe I should write a gamepad test...

@Saturn91
Copy link

Saturn91 commented Feb 8, 2023

Am I correct that controllers are not supported currently? ;-)

@idbrii
Copy link

idbrii commented Sep 30, 2023

Gamepads have worked for quite a while, but maybe with bugs. My lovejs web builds are playable with gamepad even back on this 2021 project: https://idbrii.itch.io/explorb (It seems I had some web-only problem getting initial gamepad input, but moving and launching the ball with gamepad works.)

@wiredmatt
Copy link

wiredmatt commented Oct 22, 2023

A minimal example doesn't seem to work on the web, it does for desktop build. No console errors or warnings.

local joystick, position, speed

function love.load()
  local joysticks = love.joystick.getJoysticks()
  joystick = joysticks[1]

  position = { x = 400, y = 300 }
  speed = 300
end

function love.update(dt)
  if not joystick then return end

  if joystick:isGamepadDown("dpleft") then
    position.x = position.x - speed * dt
  elseif joystick:isGamepadDown("dpright") then
    position.x = position.x + speed * dt
  end

  if joystick:isGamepadDown("dpup") then
    position.y = position.y - speed * dt
  elseif joystick:isGamepadDown("dpdown") then
    position.y = position.y + speed * dt
  end
end

function love.draw()
  love.graphics.circle("fill", position.x, position.y, 50)
end

doing print(joystick) returns nil to the browser console.

doing like this:

function love.joystickadded(joystick)
  gamepad = joystick
end

will at least output something in the console, however if gamepad:isGamepadDown("...") doesn't seem to get picked up

function love.gamepadpressed(joystick, button)
  print("pressed" .. button)
end

with gamepadpressed, on buttons (x,a,b,y) you'll get output.

However, no dpad or sticks detected.

Final update:

Got it working, the key is to use getGamepadAxis instead, I suggest this gets added to the docs, even if it was easy to figure out some people might get demotivated immediately by not having proper gamepad support

local joystick, position, speed

function love.load()
  position = { x = 400, y = 300 }
  speed = 300
end

function love.joystickadded(_joystick)
  joystick = _joystick
end

function love.update(dt)
  print(joystick)

  if not joystick then return end

  position.x = position.x + speed * dt * joystick:getGamepadAxis("leftx")
  position.y = position.y + speed * dt * joystick:getGamepadAxis("lefty")
end

function love.draw()
  love.graphics.circle("fill", position.x, position.y, 50)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants