Accelerometer

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
User avatar
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Accelerometer

Post by CanadianGamer »

I've recently been trying to do some android stuff and am now trying to read input from the accelerometer.
The changelog says that you can do the same way as a joystick. Can someone tell me what I'm doing wrong?

Here is my code

Code: Select all

function love.load()
	joystick = "Android Accelerometer"
end

function love.update()
	axes = joystick:getAxisCount( )
end

function love.draw()
    local joysticks = love.joystick.getJoysticks()
    for i, joystick in ipairs(joysticks) do
        love.graphics.print(joystick:getName(), 10, i * 20)
    end
end
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Accelerometer

Post by HugoBDesigner »

You have to do love.joystick.getJoysticks() to get the joysticks. Right now, you set the joystick as a string "Android Accelerometer".
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Re: Accelerometer

Post by CanadianGamer »

HugoBDesigner wrote:You have to do love.joystick.getJoysticks() to get the joysticks. Right now, you set the joystick as a string "Android Accelerometer".
I made a small love program to output all active joysticks here is a screenshot
Attachments
screenshot
screenshot
Screenshot_2016-01-06-14-12-50.png (4.35 KiB) Viewed 3326 times
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Accelerometer

Post by zorg »

Using a similar example to the one on the Joystick object wiki page:

Code: Select all

local joystick, position, speed
function love.load()
   joystick = love.joystick.getJoysticks()[1]
   position = {x = 0, y = 0}
   speed = 10

end

function love.update(dt)
   if joystick then
     local axis1, axis2 = joystick:getAxes()
     position.x, position.y = position.x + axis1 * speed * dt, position.y + axis2 * speed * dt
  end
end

function love.draw()
   love.graphics.circle("fill", position.x, position.y, 50)
end
Code is untested though, and uses plain Axes instead of the gamepad api, because i was uncertain whether the accelerometers would be detected by it or not.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Re: Accelerometer

Post by CanadianGamer »

Thank you for your help. I had to make a couple adjustments for it to work but it helped out a lot.

here is the revised code:

Code: Select all

    
local joystick, position, speed
    function love.load()
        joysticks = love.joystick.getJoysticks()
        looped = 0
        for i in ipairs(joysticks) do
          looped = looped + 1
        end
        joystick = joysticks[looped]
        position = {x = 0, y = 0}
        speed = 10

    end

    function love.update(dt)
       if joystick then
          axis1, axis2 = joystick:getAxes()
          position.x, position.y = position.x + axis1 * speed * dt, position.y + axis2 * speed * dt
      end
    end

    function love.draw()
       love.graphics.circle("fill", position.x, position.y, 50)
    end
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Accelerometer

Post by zorg »

You're quite welcome.
Also, just for completeness' sake, if you wanted to use the last joystick entry, then it could be made simpler:

Code: Select all

-- in love.load:
-- ...
joysticks = love.joystick.getJoysticks()
joystick = joysticks[#joysticks] -- no need for the for loop
-- ...
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Re: Accelerometer

Post by CanadianGamer »

Thank you for showing me that. for the longest time I've thought that the loop was the only way to get the last entry.
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 29 guests