Skip to content

Commit

Permalink
Button to GamepadButton
Browse files Browse the repository at this point in the history
  • Loading branch information
signorpipo committed Dec 2, 2022
1 parent 7f8a246 commit baf8242
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ PP.Direction2DTo3DConverter = class Direction2DTo3DConverter {
this._myLastValidFlatRight = PP.vec3_create();

//Setup

this._myMinAngleToBeValid = 5;
}

// directionUp is needed when u want to understand when the direction is going to fly or not
// if you don't want the direction to be flat (so like it's always flying) you can avoid specifying it
convert(direction2D, referenceTransformQuat, directionUp = null, outDirection3D = PP.vec3_create()) {
// implemented outside class definition
// direction3DUp can be used to flat the direction if the conversionTransform is not aligned with it
// it's also needed to specify the fly axis, if different from the conversionTransform up
// if direction3DUp is null, conversionTransform up is used
convert(direction2D, conversionTransform, direction3DUp = null, outDirection3D = PP.vec3_create()) {
return this.convertTransform(direction2D, conversionTransform, direction3DUp, outDirection3D);
}

isFlying() {
Expand Down Expand Up @@ -99,15 +101,67 @@ PP.Direction2DTo3DConverter = class Direction2DTo3DConverter {

this._myLastValidFlatRight.vec3_zero();
}

// Convert Alternatives

// if direction3DUp is null, [0, 1, 0] is used
// does not work properly if forward is aligned with direction3DUp
convertForward(direction2D, forward, direction3DUp = null, outDirection3D = PP.vec3_create()) {
// implemented outside class definition
}

// direction3DUp can be used to flat the direction if the conversionTransform is not aligned with it
// it's also needed to specify the fly axis, if different from the conversionTransform up
// if direction3DUp is null, conversionTransform up is used
convertTransform(direction2D, conversionTransform, direction3DUp = null, outDirection3D = PP.vec3_create()) {
return this.convertTransformMatrix(direction2D, conversionTransform, direction3DUp = null, outDirection3D);
}

convertTransformMatrix(direction2D, conversionTransformMatrix, direction3DUp = null, outDirection3D = PP.vec3_create()) {
// implemented outside class definition
}

convertTransformQuat(direction2D, conversionTransformQuat, direction3DUp = null, outDirection3D = PP.vec3_create()) {
// implemented outside class definition
}

convertRotationQuat(direction2D, conversionRotationQuat, direction3DUp = null, outDirection3D = PP.vec3_create()) {
// implemented outside class definition
}
};

PP.Direction2DTo3DConverter.prototype.convert = function () {
PP.Direction2DTo3DConverter.prototype.convertForward = function () {
let rotationQuat = PP.quat_create();
return function convertForward(direction2D, forward, direction3DUp = null, outDirection3D = PP.vec3_create()) {
rotationQuat.quat_identity();
rotationQuat.quat_setForward(forward, direction3DUp);
return this.convertRotationQuat(direction2D, rotationQuat, direction3DUp, outDirection3D);
}
}();

PP.Direction2DTo3DConverter.prototype.convertTransformMatrix = function () {
let rotationQuat = PP.quat_create();
return function convertTransformMatrix(direction2D, conversionTransformMatrix, direction3DUp = null, outDirection3D = PP.vec3_create()) {
rotationQuat = conversionTransformMatrix.mat4_getRotationQuat(rotationQuat);
return this.convertRotationQuat(direction2D, rotationQuat, direction3DUp, outDirection3D);
}
}();

PP.Direction2DTo3DConverter.prototype.convertTransformQuat = function () {
let rotationQuat = PP.quat_create();
return function convertTransformQuat(direction2D, conversionTransformQuat, direction3DUp = null, outDirection3D = PP.vec3_create()) {
rotationQuat = conversionTransformQuat.quat2_getRotationQuat(rotationQuat);
return this.convertRotationQuat(direction2D, rotationQuat, direction3DUp, outDirection3D);
}
}();

PP.Direction2DTo3DConverter.prototype.convertRotationQuat = function () {
let forward = PP.vec3_create();
let right = PP.vec3_create();
let directionUpNegate = PP.vec3_create();
let direction3DUpNegate = PP.vec3_create();
let forwardScaled = PP.vec3_create();
let rightScaled = PP.vec3_create();
return function convert(direction2D, referenceTransformQuat, directionUp = null, outDirection3D = PP.vec3_create()) {
return function convertRotationQuat(direction2D, conversionRotationQuat, direction3DUp = null, outDirection3D = PP.vec3_create()) {
if (direction2D.vec2_isZero()) {
let resetFlyForward = this._myParams.myAutoUpdateFlyForward && this._myParams.myResetFlyForwardWhenZero;
if (resetFlyForward) {
Expand All @@ -130,73 +184,73 @@ PP.Direction2DTo3DConverter.prototype.convert = function () {
}
}

forward = referenceTransformQuat.quat2_getForward(forward);
right = referenceTransformQuat.quat2_getRight(right);
forward = conversionRotationQuat.quat_getForward(forward);
right = conversionRotationQuat.quat_getRight(right);

if (directionUp != null) {
directionUpNegate = directionUp.vec3_negate(directionUpNegate);
if (direction3DUp != null) {
direction3DUpNegate = direction3DUp.vec3_negate(direction3DUpNegate);

// check if it is flying based on the convert transform orientation
if (this._myParams.myAutoUpdateFlyForward) {
let angleForwardWithDirectionUp = forward.vec3_angle(directionUp);
let angleForwardWithDirectionUp = forward.vec3_angle(direction3DUp);
this._myIsFlyingForward = this._myIsFlyingForward ||
(angleForwardWithDirectionUp < 90 - this._myParams.myMinAngleToFlyForwardUp || angleForwardWithDirectionUp > 90 + this._myParams.myMinAngleToFlyForwardDown);
}

if (this._myParams.myAutoUpdateFlyRight) {
let angleRightWithDirectionUp = right.vec3_angle(directionUp);
let angleRightWithDirectionUp = right.vec3_angle(direction3DUp);
this._myIsFlyingRight = this._myIsFlyingRight ||
(angleRightWithDirectionUp < 90 - this._myParams.myMinAngleToFlyRightUp || angleRightWithDirectionUp > 90 + this._myParams.myMinAngleToFlyRightDown);
}

// remove the component to prevent flying, if needed
if (!this._myIsFlyingForward) {
// if the forward is too similar to the up (or down) take the last valid forward
if (!this._myLastValidFlatForward.vec3_isZero(0.000001) && (forward.vec3_angle(directionUp) < this._myMinAngleToBeValid || forward.vec3_angle(directionUpNegate) < this._myMinAngleToBeValid)) {
if (!this._myLastValidFlatForward.vec3_isZero(0.000001) && (forward.vec3_angle(direction3DUp) < this._myMinAngleToBeValid || forward.vec3_angle(direction3DUpNegate) < this._myMinAngleToBeValid)) {
if (forward.vec3_isConcordant(this._myLastValidFlatForward)) {
forward.pp_copy(this._myLastValidFlatForward);
} else {
forward = this._myLastValidFlatForward.vec3_negate(forward);
}
}

forward = forward.vec3_removeComponentAlongAxis(directionUp, forward);
forward = forward.vec3_removeComponentAlongAxis(direction3DUp, forward);
forward.vec3_normalize(forward);
}

if (!this._myIsFlyingRight) {
// if the right is too similar to the up (or down) take the last valid right
if (!this._myLastValidFlatRight.vec3_isZero(0.000001) && (right.vec3_angle(directionUp) < this._myMinAngleToBeValid || right.vec3_angle(directionUpNegate) < this._myMinAngleToBeValid)) {
if (!this._myLastValidFlatRight.vec3_isZero(0.000001) && (right.vec3_angle(direction3DUp) < this._myMinAngleToBeValid || right.vec3_angle(direction3DUpNegate) < this._myMinAngleToBeValid)) {
if (right.vec3_isConcordant(this._myLastValidFlatRight)) {
right.pp_copy(this._myLastValidFlatRight);
} else {
right = this._myLastValidFlatRight.vec3_negate(right);
}
}

right = right.vec3_removeComponentAlongAxis(directionUp, right);
right = right.vec3_removeComponentAlongAxis(direction3DUp, right);
right.vec3_normalize(right);
}

// update last valid
if ((forward.vec3_angle(directionUp) > this._myMinAngleToBeValid && forward.vec3_angle(directionUpNegate) > this._myMinAngleToBeValid) ||
if ((forward.vec3_angle(direction3DUp) > this._myMinAngleToBeValid && forward.vec3_angle(direction3DUpNegate) > this._myMinAngleToBeValid) ||
(direction2D[1] != 0 && this._myLastValidFlatForward.vec3_isZero(0.000001))) {
this._myLastValidFlatForward = forward.vec3_removeComponentAlongAxis(directionUp, this._myLastValidFlatForward);
this._myLastValidFlatForward = forward.vec3_removeComponentAlongAxis(direction3DUp, this._myLastValidFlatForward);
this._myLastValidFlatForward.vec3_normalize(this._myLastValidFlatForward);
}

if ((right.vec3_angle(directionUp) > this._myMinAngleToBeValid && right.vec3_angle(directionUpNegate) > this._myMinAngleToBeValid) ||
if ((right.vec3_angle(direction3DUp) > this._myMinAngleToBeValid && right.vec3_angle(direction3DUpNegate) > this._myMinAngleToBeValid) ||
(direction2D[0] != 0 && this._myLastValidFlatRight.vec3_isZero(0.000001))) {
this._myLastValidFlatRight = right.vec3_removeComponentAlongAxis(directionUp, this._myLastValidFlatRight);
this._myLastValidFlatRight = right.vec3_removeComponentAlongAxis(direction3DUp, this._myLastValidFlatRight);
this._myLastValidFlatRight.vec3_normalize(this._myLastValidFlatRight);
}
}

// compute direction 3D
outDirection3D = right.vec3_scale(direction2D[0], rightScaled).vec3_add(forward.vec3_scale(direction2D[1], forwardScaled), outDirection3D);

if (directionUp != null && !this._myIsFlyingForward && !this._myIsFlyingRight) {
outDirection3D = outDirection3D.vec3_removeComponentAlongAxis(directionUp, outDirection3D);
if (direction3DUp != null && !this._myIsFlyingForward && !this._myIsFlyingRight) {
outDirection3D = outDirection3D.vec3_removeComponentAlongAxis(direction3DUp, outDirection3D);
}

outDirection3D.vec3_normalize(outDirection3D);
Expand Down
32 changes: 16 additions & 16 deletions wle_pp/wle_pp_bundler/js/pp/gameplay/grab_throw/grabber_hand.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ WL.registerComponent('pp-grabber-hand', {
}

if (this._myGrabButton == 0) {
this._myGamepad.registerButtonEventListener(PP.ButtonType.SELECT, PP.ButtonEvent.PRESS_START, this, this._grab.bind(this, PP.ButtonType.SELECT));
this._myGamepad.registerButtonEventListener(PP.ButtonType.SELECT, PP.ButtonEvent.PRESS_END, this, this._throw.bind(this, PP.ButtonType.SELECT));
this._myGamepad.registerButtonEventListener(PP.GamepadButtonType.SELECT, PP.GamepadButtonEvent.PRESS_START, this, this._grab.bind(this, PP.GamepadButtonType.SELECT));
this._myGamepad.registerButtonEventListener(PP.GamepadButtonType.SELECT, PP.GamepadButtonEvent.PRESS_END, this, this._throw.bind(this, PP.GamepadButtonType.SELECT));
} else if (this._myGrabButton == 1) {
this._myGamepad.registerButtonEventListener(PP.ButtonType.SQUEEZE, PP.ButtonEvent.PRESS_START, this, this._grab.bind(this, PP.ButtonType.SQUEEZE));
this._myGamepad.registerButtonEventListener(PP.ButtonType.SQUEEZE, PP.ButtonEvent.PRESS_END, this, this._throw.bind(this, PP.ButtonType.SQUEEZE));
this._myGamepad.registerButtonEventListener(PP.GamepadButtonType.SQUEEZE, PP.GamepadButtonEvent.PRESS_START, this, this._grab.bind(this, PP.GamepadButtonType.SQUEEZE));
this._myGamepad.registerButtonEventListener(PP.GamepadButtonType.SQUEEZE, PP.GamepadButtonEvent.PRESS_END, this, this._throw.bind(this, PP.GamepadButtonType.SQUEEZE));
} else {
this._myGamepad.registerButtonEventListener(PP.ButtonType.SQUEEZE, PP.ButtonEvent.PRESS_START, this, this._grab.bind(this, PP.ButtonType.SQUEEZE));
this._myGamepad.registerButtonEventListener(PP.ButtonType.SQUEEZE, PP.ButtonEvent.PRESS_END, this, this._throw.bind(this, PP.ButtonType.SQUEEZE));
this._myGamepad.registerButtonEventListener(PP.GamepadButtonType.SQUEEZE, PP.GamepadButtonEvent.PRESS_START, this, this._grab.bind(this, PP.GamepadButtonType.SQUEEZE));
this._myGamepad.registerButtonEventListener(PP.GamepadButtonType.SQUEEZE, PP.GamepadButtonEvent.PRESS_END, this, this._throw.bind(this, PP.GamepadButtonType.SQUEEZE));

this._myGamepad.registerButtonEventListener(PP.ButtonType.SELECT, PP.ButtonEvent.PRESS_START, this, this._grab.bind(this, PP.ButtonType.SELECT));
this._myGamepad.registerButtonEventListener(PP.ButtonType.SELECT, PP.ButtonEvent.PRESS_END, this, this._throw.bind(this, PP.ButtonType.SELECT));
this._myGamepad.registerButtonEventListener(PP.GamepadButtonType.SELECT, PP.GamepadButtonEvent.PRESS_START, this, this._grab.bind(this, PP.GamepadButtonType.SELECT));
this._myGamepad.registerButtonEventListener(PP.GamepadButtonType.SELECT, PP.GamepadButtonEvent.PRESS_END, this, this._throw.bind(this, PP.GamepadButtonType.SELECT));
}
},
onDeactivate() {
Expand All @@ -100,17 +100,17 @@ WL.registerComponent('pp-grabber-hand', {
}

if (this._myGrabButton == 0) {
this._myGamepad.unregisterButtonEventListener(PP.ButtonType.SELECT, PP.ButtonEvent.PRESS_START, this);
this._myGamepad.unregisterButtonEventListener(PP.ButtonType.SELECT, PP.ButtonEvent.PRESS_END, this);
this._myGamepad.unregisterButtonEventListener(PP.GamepadButtonType.SELECT, PP.GamepadButtonEvent.PRESS_START, this);
this._myGamepad.unregisterButtonEventListener(PP.GamepadButtonType.SELECT, PP.GamepadButtonEvent.PRESS_END, this);
} else if (this._myGrabButton == 1) {
this._myGamepad.unregisterButtonEventListener(PP.ButtonType.SQUEEZE, PP.ButtonEvent.PRESS_START, this);
this._myGamepad.unregisterButtonEventListener(PP.ButtonType.SQUEEZE, PP.ButtonEvent.PRESS_END, this);
this._myGamepad.unregisterButtonEventListener(PP.GamepadButtonType.SQUEEZE, PP.GamepadButtonEvent.PRESS_START, this);
this._myGamepad.unregisterButtonEventListener(PP.GamepadButtonType.SQUEEZE, PP.GamepadButtonEvent.PRESS_END, this);
} else {
this._myGamepad.unregisterButtonEventListener(PP.ButtonType.SQUEEZE, PP.ButtonEvent.PRESS_START, this);
this._myGamepad.unregisterButtonEventListener(PP.ButtonType.SQUEEZE, PP.ButtonEvent.PRESS_END, this);
this._myGamepad.unregisterButtonEventListener(PP.GamepadButtonType.SQUEEZE, PP.GamepadButtonEvent.PRESS_START, this);
this._myGamepad.unregisterButtonEventListener(PP.GamepadButtonType.SQUEEZE, PP.GamepadButtonEvent.PRESS_END, this);

this._myGamepad.unregisterButtonEventListener(PP.ButtonType.SELECT, PP.ButtonEvent.PRESS_START, this);
this._myGamepad.unregisterButtonEventListener(PP.ButtonType.SELECT, PP.ButtonEvent.PRESS_END, this);
this._myGamepad.unregisterButtonEventListener(PP.GamepadButtonType.SELECT, PP.GamepadButtonEvent.PRESS_START, this);
this._myGamepad.unregisterButtonEventListener(PP.GamepadButtonType.SELECT, PP.GamepadButtonEvent.PRESS_END, this);
}
},
_grab: function (grabButton) {
Expand Down
1 change: 0 additions & 1 deletion wle_pp/wle_pp_bundler/js/pp/input/cauldron/mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ PP.MouseButtonType = {
PP.Mouse = class Mouse {
constructor() {
// #TODO refactor Mouse/Keyboard/Gamepad and create a sort of parent ButtonHandler that have the base ButtonInfo and all of them inherit
// ButtonType could also become GamepadButtonID or directly GamepadButton like in Unity

this._myButtonInfos = new Map();
for (let typeKey in PP.MouseButtonType) {
Expand Down
Loading

0 comments on commit baf8242

Please sign in to comment.