Joystick problems

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
Praetor3k
Prole
Posts: 7
Joined: Sun Jul 18, 2010 7:30 pm

Joystick problems

Post by Praetor3k »

Hi there,
I'm having problems with the joystick api. I'm using a set of drivers for connecting my PS3 pad to Windows xp (http://www.motioninjoy.com/) and all buttons/axes are detected in windows control panel.

My love.load function initialise joystick 1 like so

Code: Select all

p1_joystick_data = {};
p1_joystick_data.id = 1;
p1_joystick_data.num_axes = nil;
function love.load()
	print("Init love.load()");
	joysticks = love.joystick.getNumJoysticks();
	print("There are " .. tostring(joysticks) .. " available");
	if(joysticks < 1) then return end;
	print("Name:" .. tostring(love.joystick.getName(p1_joystick_data.id)));
	print("Open? " .. tostring(love.joystick.isOpen( p1_joystick_data.id )));
	while(love.joystick.isOpen( p1_joystick_data.id ) == false)do
		local result = love.joystick.open( 1 );
		print("result " .. tostring(result));
	end
	print("Open? " .. tostring(love.joystick.isOpen( p1_joystick_data.id )));
	p1_joystick_data.num_axes = love.joystick.getNumAxes( p1_joystick_data.id );
	print("num_axes: " .. tostring(p1_joystick_data.num_axes))
end

however it fails to open the joystick and loops in while loop. 

Can anyone offer help please?

thanks.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Joystick problems

Post by bartbes »

Indexes start at 0, maybe that's the problem?
Praetor3k
Prole
Posts: 7
Joined: Sun Jul 18, 2010 7:30 pm

Re: Joystick problems

Post by Praetor3k »

LOL, well that's sorted it! Sorry, so used to Lua tables with start index at 1.

Many thanks!
User avatar
Luiji
Party member
Posts: 396
Joined: Mon May 17, 2010 6:59 pm

Re: Joystick problems

Post by Luiji »

LOVE should provide 1-based indexes in order to be more friendly to Lua programmers.
Good bye.
Praetor3k
Prole
Posts: 7
Joined: Sun Jul 18, 2010 7:30 pm

Re: Joystick problems

Post by Praetor3k »

Yeah there's a few confusing bits about this api, I noticed
love.joystick.getName()
is documented as taking no argument, but you need to supply the id (in this case 0). :)

One more question, which api hook am I to use to find the analog extent of the other axes?
I used

Code: Select all

p1_joystick_data.axes_values[1] = love.joystick.getAxes();
but this threw an error "Incorrect number of arguments, got [0], expected [1-1]". I figured it's the same issue as love.joystick.getName() and supplied a value of 0 and this gave me the analog x axis extent.
However, when I try getting extent of axis values 2,3,4 upto 6 (which is how many axes it's reporting), I get nil.
My update function is

Code: Select all

function love.update(dt)
	for i=1, p1_joystick_data.num_axes do
		p1_joystick_data.axes_values[i] = love.joystick.getAxes(i-1);
	end
	HandleControls();
end
many thanks for the quick reponses.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Joystick problems

Post by bartbes »

getAxes returns the number of axes a joystick has.
Praetor3k
Prole
Posts: 7
Joined: Sun Jul 18, 2010 7:30 pm

Re: Joystick problems

Post by Praetor3k »

It can't be, otherwise axis 1 wouldn't be correct (reporting values of -1 to 1 when analog stick moves in x direction).
Also:
http://love2d.org/wiki/love.joystick
love.joystick.getAxes Returns the position of each axis.
love.joystick.getAxis Returns the direction of the axis.
love.joystick.getNumAxes Returns the number of axes on the joystick.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Joystick problems

Post by bartbes »

Oh, heh, you're right, it returns the axis position of the joystick, so you'd want something like:

Code: Select all

p1_joystick_data.axes_values = {love.joystick.getAxes(0)}
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], Semrush [Bot], snibo and 6 guests