Page 1 of 1

Joystick module doesn't work?

Posted: Tue Oct 10, 2017 7:07 pm
by nikneym
I got issues about love joystick module. It seems i have connected joystick on pc but button-push checks doesn't work. Can you guys try this code out for me? Joystick works on other stuff but love.

Code: Select all

joysticks=love.joystick.getJoysticks();
joystick=joysticks[1];

player={
	x=100,
	y=100,
	speed=200,
};

function joystickCalistir(dt)
	if joystick:isGamepadDown("dpleft") then
		player.x=player.x-player.speed*dt;
	elseif joystick:isGamepadDown("dpright") then
		player.x=player.x+player.speed*dt;
	end
	
	if joystick:isGamepadDown("dpup") then
		player.y=player.y-player.speed*dt;
	elseif joystick:isGamepadDown("dpdown") then
		player.y=player.y-player.speed*dt;
	end
end

function love.update(dt)
	joystickCalistir(dt);
	connected=joystick:isConnected();
end

function love.draw()
	love.graphics.setBackgroundColor(255, 255, 255);

	love.graphics.setColor(255, 40, 40);

	if connected==true then
		love.graphics.print("Connected.", 100, 10);
	elseif connected==false then
		love.graphics.print("Couldn't connected.", 100, 10);
	end

	love.graphics.print(joystick:getName(), 10, 10);
	love.graphics.rectangle("fill", player.x, player.y, 32, 32);
end

Re: Joystick module doesn't work?

Posted: Tue Oct 10, 2017 7:37 pm
by zorg
Did you call Joystick:isGamepad to see if it's recognized as an xbox360-like (probably means xinput-compatible, but i'm not sure) gamepad? If that returns false, but Joystick:isConnected is true, then you might have to resort to using the non-gamepad functions (and/or callbacks) instead; or in other words, most functions on the Joystick page that don't have "gamepad" in their names.

Re: Joystick module doesn't work?

Posted: Wed Oct 11, 2017 5:50 pm
by nikneym
thanks for the reply i'm gonna try it :)

Re: Joystick module doesn't work?

Posted: Wed Oct 11, 2017 6:26 pm
by nikneym
works well with this code. but i'm not sure im doing it right. :p @zorg

Edit: there is a wrong write about horizontal and vertical but it doesn't matter.

Code: Select all

joysticks=love.joystick.getJoysticks();
joystick=joysticks[1];

player={
	x=100,
	y=100,
	speed=200,
};

function joystickCalistir(dt)
	local horizontal=joystick:getAxis(2);
	local vertical=joystick:getAxis(1);
	
	if horizontal==1 then
		player.y=player.y+player.speed*dt;
	elseif horizontal==-1 then
		player.y=player.y-player.speed*dt;
	end
	
	if vertical==1 then
		player.x=player.x+player.speed*dt;
	elseif vertical==-1 then
		player.x=player.x-player.speed*dt;
	end
end

function love.update(dt)
	joystickCalistir(dt);
	connected=joystick:isConnected();
end

function love.draw()
	love.graphics.setBackgroundColor(255, 255, 255);

	love.graphics.setColor(255, 40, 40);	
	
	if connected==true then
		love.graphics.print("Connected.", 100, 10);
	elseif connected==false then
		love.graphics.print("Couldn't connected.", 100, 10);
	end
	
	if joystick:isVibrationSupported()==false then
		love.graphics.print("Your joystick doesn't support vibration.", 200, 10);
	end
	
	--love.graphics.print(joystick:getGUID(), 10, 30);
	love.graphics.print(joystick:getAxis(2), 10, 30);
	
	love.graphics.print(joystick:getName(), 10, 10);
	love.graphics.rectangle("fill", player.x, player.y, 32, 32);
end

Re: Joystick module doesn't work?

Posted: Thu Oct 12, 2017 8:03 am
by zorg
Looks like it could work like that :3
Though if you decide to support analog stick movement (as in movement ranging from slow to fast) then you could remove the checks in your joystickCalistir function, and just multiply player.speed with them (assuming the axes' range is between -1 and 1).