Ignoring PS4 Controllers

Yo @Deltakosh or the other Dave (I dont see his @ sign tag)

Are we purposely ignoring the PS4 controllers in the Gamepad API. I cant seem to find that part.

But for some reason i can see the PS4 controller using regular HTML Gamepad API in a non babylon project and the HTML Gamepad Test site that reports all the same key values as a Xbox One controller… But i don’t get any response for the PS4 controller in my Babylon Projects… Any ideas as to why that is ???

Well this is because I have no PS4 controller:) But if it behaves like the XBox controller it should be dead easy to support it (we are probably filtering by driver name)

@Deltakosh I live near Seattle and would be happy to loan you a PS4 controller!
(sorry if off topic)

Not off topic at all:) I was more looking for someone interested to help by doing the PR :wink:

1 Like

Not quite confident enough in my familiarity with Babylon to make contributions just yet :frowning_face:

I will check it then :slight_smile:

FYI… My PS4 controller id reports as:

Wireless Controller (STANDARD GAMEPAD Vendor: 054c Product: 05c4)

On my Mac

If that helps :slight_smile:

I ordered one . Will work on it asap :smiley:

2 Likes

How do you connect a PS4 controller? Via blutooth? is there something specific to do?

Ok I’ve just connected through USB. Will make it work

@MackeyK24 by default it works in Babylon.js. It is recognize as a regular gamepad. and I can receive all events

I get nothing … I will check a regular game pad usage in my code base then

Can please show a playground using PS/4 controller

Sure:
https://playground.babylonjs.com/#U3XJTB#30

@trevordev helped me come up with the code to handle pretty much any controller…

One second let me dig that up. I gotta clean it out cause its for a specific project I’m not trying to share yet. but the controls work with any controller I believe.


    var gamepadManager = new BABYLON.GamepadManager()
    scene.gamepadManager = gamepadManager
    var gamepad, controls
    controls = null

    gamepadManager.onGamepadConnectedObservable.add((gamepad, state)=>{	
	gamepad = gamepadManager.gamepads[0]	
	if(gamepad){
             controls = new Controller(gamepad, 
             {  
                sensitivity :   0.3,
                deadzone 	: 	0.12,
                LYinvert 	:	-1,
                LXinvert 	: 	1,
                RYinvert 	: 	-1,
                RXinvert 	: 	1,
            }, scene)
        }
    })

    gamepadManager.onGamepadDisconnectedObservable.add((gamepad, state)=>{
        gamepad = false
        controls = null
		console.log('Detached Controller!')        
    })

scene.onBeforeRenderObservable.add(()=>{ 
        if(controls == null || !controls.gamepad.browserGamepad.buttons){
            return
        }
        if(controls !== null){
        controls.update()
        }
    })

class Controller{
	constructor(gamepad, options, scene){
		this._options = options
		this._scene = scene
		this._gamepad = gamepad	
		console.log(gamepad, "- gamepad connected.")
		this._sticksRaw = null
		this._sticksSmoothed = null
	}
	update(){
		//console.log(this.gamepad)
		var sticksRaw = [
		this.gamepad.leftStick.x*this.options.LXinvert,
		this.gamepad.leftStick.y*this.options.LYinvert,
		this.gamepad.rightStick.x*this.options.RXinvert,
		this.gamepad.rightStick.y*this.options.RYinvert,
		]
		//for debuging once in production just overwrite?
		var sticksSmoothed = []
		//Apply Deadzones
		for(let i=0; i<sticksRaw.length; i+=2){
			let sv = new BABYLON.Vector2(sticksRaw[i], sticksRaw[i+1]).scale(this.options.sensitivity)
			if(sv.length() < this.options.deadzone){
				sv = BABYLON.Vector2.Zero()
			}else{           
				sv.scale((sv.length() - this.options.deadzone) / (1 - this.options.deadzone))
			}
			sticksSmoothed.push(sv.x, sv.y)
		}  
		this.sticksRaw = sticksRaw
		this.sticksSmoothed = sticksSmoothed
	}
	get gamepad(){return this._gamepad}
	get options(){return this._options}
	get target(){return this._target}
	get camera(){return this._camera}
	
	get sticksRaw(){return this._sticksRaw}
	set sticksRaw(v){this._sticksRaw = v}
	
	get sticksSmoothed(){return this._sticksSmoothed}
	set sticksSmoothed(v){this._sticksSmoothed = v}
	
	get LX(){return this._sticksSmoothed[0]}
	get LY(){return this._sticksSmoothed[1]}
	get RX(){return this._sticksSmoothed[2]}
	get RY(){return this._sticksSmoothed[3]}
	get button(){return this.gamepad._buttons}
	
}

Prolly needs to be cleaned up some… but this should get you there.

I see now… You added BABYLON.DualShockPad… Sweet :slight_smile:

1 Like

PS4 Controller now work great… Thanks @Deltakosh :slight_smile:

2 Likes