aframe-extras icon indicating copy to clipboard operation
aframe-extras copied to clipboard

Cannot override custom-controls method

Open kylebakerio opened this issue 4 years ago • 2 comments

As per the example on the readme, I'm trying to override some methods.

I'd prefer to just override a method not shown in the example (getDpad), which I assume is ignored (I don't see any reference to custom-controls in the gamepad-controls.js, so I'm not sure where the override happens--in reality, it seems this feature doesn't exist anymore, so probably nowhere), but I could potentially do it via isVelocityActive or getVelocityDelta (I don't see a getPositionDelta as mentioned in the readme example, btw... ?).

For all I can tell this all gets completely ignored. Is this feature no longer here, and customization now requires being manually recompiled?

AFRAME.registerComponent('custom-controls', {
    
    getDpad(target) {
        // this isn't mentioned as overridable in docs, but if it can be, I'd like to
        // I want to use the dpad myself without it being used for movement
        // (I'll use joysticks for that). 
        debugger; // this never gets hit
        return target.set(0, 0);
    },

    // alternatively, I _think_ modifying the following two functions (listed in README as overridable) as follows could also work:
    isVelocityActive: function () {
         // changes that I imagine would work
        if (!this.data.enabled || !this.isConnected()) return false;

        // const dpad = this._dpadVector;
        const joystick = this._moveVector;

        // this.getDpad(dpad);
        this.getJoystick(Joystick.MOVEMENT, joystick);

        const inputX = /*dpad.x ||*/ joystick.x; // change that I imagine would work
        const inputY = /*dpad.y ||*/ joystick.y; // change that I imagine would work

        return Math.abs(inputX) > JOYSTICK_EPS || Math.abs(inputY) > JOYSTICK_EPS;
    },

    getVelocityDelta() {
         // changes that I imagine would work
        debugger // <--this never fires
        
        // const dpad = this._dpadVector;
        const joystick = this._moveVector;

        // this.getDpad(dpad);
        this.getJoystick(Joystick.MOVEMENT, joystick);

        const inputX = /*dpad.x ||*/ joystick.x;
        const inputY = /*dpad.y ||*/ joystick.y;
        const dVelocity = new THREE.Vector3();

        if (Math.abs(inputX) > JOYSTICK_EPS) {
            dVelocity.x += inputX;
        }
        if (Math.abs(inputY) > JOYSTICK_EPS) {
            dVelocity.z += inputY;
        }

        return dVelocity;
    },
    
    // lots of other personal code is also here (e.g., an init() function, and other helper methods, that I use separately),
    // which I mention for completeness--that's fine, right?

I do use the gamepad controls as-is and they work; I add them via rig.setAttribute('movement-controls',"enabled:true; controls:gamepad; speed:.1"); if mobile/VR, or avatar.setAttribute('look-controls',"pointerLockEnabled: true"); rig.setAttribute('movement-controls',"enabled:true;"); if desktop.

The only problem is that the dpad can't be used properly, because it causes movement...

(relevant portion of the readme:)

Customizing movement-controls

To implement your custom controls, define a component and override one or more methods:

Method Type Required
isVelocityActive() : boolean Movement Yes
getVelocityDelta(deltaMS : number) : THREE.Vector3 Movement No
getPositionDelta(deltaMS : number) : THREE.Vector3 Movement No

Example:

AFRAME.registerComponent('custom-controls', {
  isVelocityActive: function () {
    return Math.random() < 0.25;
  },
  getPositionDelta: function () {
    return new THREE.Vector3(1, 0, 0);
  }
});

https://github.com/n5ro/aframe-extras/blob/master/src/controls/README.md

kylebakerio avatar Dec 23 '20 13:12 kylebakerio

(Glad to do a pull request if I can do something to help.)

kylebakerio avatar Dec 23 '20 13:12 kylebakerio

slightly hacky fix I'm going to implement for now:

document
.querySelector('[gamepad-controls]')
.components['gamepad-controls']
.getDpad = function(target) {
    return target.set(0,0)
 } 

This seems to work as desired. However, at a minimum, the README should be updated. (I'm glad to do that, let me know if desired.)

kylebakerio avatar Dec 23 '20 13:12 kylebakerio

The custom-controls described in the README is just an example to create your own controls, you don't inherit anything from an existing implementation, you need to implement the required methods yourself. If the documentation is not clear on this point, PR welcome.

vincentfretin avatar Dec 04 '22 09:12 vincentfretin

I see. Since no one else seems to have this misunderstanding, I think we can leave it. It was 'override' that confused me probably. This was a while ago, so I don't remember honestly.

kylebakerio avatar Dec 05 '22 15:12 kylebakerio