Use built-in FreeCameraGamepadInput

This commit is contained in:
Tony Garnock-Jones 2023-01-05 10:10:50 +01:00
parent 8bda527684
commit a36f696389
2 changed files with 4 additions and 81 deletions

View File

@ -3,15 +3,13 @@ import {
DualShockPad,
Engine,
FreeCamera,
FreeCameraGamepadInput,
Gamepad as b_Gamepad,
Mesh,
Quaternion,
Scene,
Vector3,
} from '@babylonjs/core/Legacy/legacy';
import { GamepadInput } from './gamepad.js';
let buttonDown : { [button: number]: boolean } = {};
function latch(gp: Gamepad, button: number): boolean {
@ -88,7 +86,9 @@ export async function startEngine(
}
const sm = xrAvailable ? xr.baseExperience.sessionManager : null;
const gamepadInput = new GamepadInput();
const gamepadInput = new FreeCameraGamepadInput();
gamepadInput.gamepadMoveSensibility = 320;
gamepadInput.gamepadAngularSensibility = 100;
camera.inputs.add(gamepadInput);
gamepadInput.attachControl();

View File

@ -1,77 +0,0 @@
import { Observer } from "@babylonjs/core/Misc/observable";
import { Nullable } from "@babylonjs/core/types";
import { ICameraInput } from "@babylonjs/core/Cameras/cameraInputsManager";
import { FreeCamera } from "@babylonjs/core/Cameras/freeCamera";
import { Matrix, Vector3, Vector2 } from "@babylonjs/core/Maths/math.vector";
import { Gamepad } from "@babylonjs/core/Gamepads/gamepad";
import "@babylonjs/core/Gamepads/gamepadSceneComponent";
export class GamepadInput implements ICameraInput<FreeCamera> {
public camera: Nullable<FreeCamera> = null;
public gamepad: Nullable<Gamepad> = null;
public gamepadAngularSensibility = 200;
public gamepadMoveSensibility = 40;
private _vector3: Vector3 = Vector3.Zero();
private _vector2: Vector2 = Vector2.Zero();
private _onGamepadConnectedObserver: Nullable<Observer<Gamepad>> = null;
private _onGamepadDisconnectedObserver: Nullable<Observer<Gamepad>> = null;
public attachControl(): void {
if (this.camera === null) return;
let manager = this.camera.getScene().gamepadManager;
this._onGamepadConnectedObserver = manager.onGamepadConnectedObservable.add((gamepad) => {
this.gamepad = this.gamepad ?? gamepad;
});
this._onGamepadDisconnectedObserver = manager.onGamepadDisconnectedObservable.add((gamepad) => {
if (this.gamepad === gamepad) this.gamepad = null;
});
this.gamepad = manager.gamepads[0];
}
public detachControl(): void {
if (this.camera !== null) {
let manager = this.camera.getScene().gamepadManager;
manager.onGamepadConnectedObservable.remove(this._onGamepadConnectedObserver);
manager.onGamepadDisconnectedObservable.remove(this._onGamepadDisconnectedObserver);
}
this.gamepad = null;
}
public checkInputs(): void {
if (this.gamepad && this.gamepad.leftStick) {
const camera = this.camera!;
const LSValues = this.gamepad.leftStick;
const normalizedLX = LSValues.x / this.gamepadMoveSensibility;
const normalizedLY = LSValues.y / this.gamepadMoveSensibility;
LSValues.x = Math.abs(normalizedLX) > 0.005 ? 0 + normalizedLX : 0;
LSValues.y = Math.abs(normalizedLY) > 0.005 ? 0 + normalizedLY : 0;
const RSValues = this.gamepad.rightStick ?? { x: 0, y: 0 };
var normalizedRX = RSValues.x / this.gamepadAngularSensibility;
var normalizedRY = RSValues.y / this.gamepadAngularSensibility;
RSValues.x = Math.abs(normalizedRX) > 0.001 ? 0 + normalizedRX : 0;
RSValues.y = Math.abs(normalizedRY) > 0.001 ? 0 + normalizedRY : 0;
const _cameraTransform = Matrix.Identity();
Matrix.RotationYawPitchRollToRef(camera.rotation.y, 0, 0, _cameraTransform);
var speed = camera._computeLocalCameraSpeed() * 10.0;
this._vector3.copyFromFloats(LSValues.x * speed, 0, -LSValues.y * speed);
const _deltaTransform = Vector3.Zero();
Vector3.TransformCoordinatesToRef(this._vector3, _cameraTransform, _deltaTransform);
camera.cameraDirection.addInPlace(_deltaTransform);
this._vector2.copyFromFloats(RSValues.y, RSValues.x);
camera.cameraRotation.addInPlace(this._vector2);
}
}
public getClassName(): string {
return "GamepadInput";
}
public getSimpleName(): string {
return "gamepad";
}
}