Replicate Smooth Camera Movement With Havok Physics

Gotta love Copilot… Explained wild camera movement issue… and Bam…The Response:

To address the issue of wild camera movements when both mouse X and Y inputs are being updated simultaneously, you can implement a damping mechanism. This mechanism will reduce the influence of one axis when the other is being actively updated, without completely negating it.

Here’s a step-by-step plan to implement this:

  1. Introduce Damping Factors: Define damping factors for both mouse X and Y inputs.
  2. Calculate Damped Inputs: Adjust the mouse X and Y inputs based on the damping factors.
  3. Apply Damped Inputs: Use the damped inputs to update the camera and player transforms.

Let’s modify the #updatePlayer function to include these steps:

Pseudocode

  1. Define damping factors for mouse X and Y.
  2. Calculate the magnitude of mouse X and Y inputs.
  3. Apply damping to mouse X if mouse Y is significant, and vice versa.
  4. Update the camera and player transforms using the damped inputs.

Code Implementation

{
    if (this.#enableUserInput === false) return;

    // Get Delta Time
    let deltaTime = this.getDeltaSeconds();

    // Update User Input
    let userInputX = BABYLON.Toolkit.InputController.GetUserInput(BABYLON.Toolkit.UserInputAxis.Horizontal);
    let userInputZ = BABYLON.Toolkit.InputController.GetUserInput(BABYLON.Toolkit.UserInputAxis.Vertical);
    let userMouseX = BABYLON.Toolkit.InputController.GetUserInput(BABYLON.Toolkit.UserInputAxis.MouseX);
    let userMouseY = BABYLON.Toolkit.InputController.GetUserInput(BABYLON.Toolkit.UserInputAxis.MouseY);
    if (BABYLON.Toolkit.WindowManager.IsMobile())
    {
        userInputZ = BABYLON.Scalar.Clamp((-userMouseY * this.#mobileSwipeFactor), -1, 1);
        userMouseY = 0; // Note: Zero Out Mouse Y Input
    }
    
    // Format Mouse Options
    let mouseX = (userMouseX * this.turnSpeed * deltaTime);
    let mouseY = (userMouseY * this.lookSpeed * deltaTime);
    let upAngle = BABYLON.Tools.ToRadians(this.#maxUpAngle);
    let downAngle = BABYLON.Tools.ToRadians(this.#maxDownAngle);
    if (BABYLON.Toolkit.InputController.a_mousex !== 0) mouseX *= this.arrowFactor;
    else if (BABYLON.Toolkit.InputController.g_mousex1 !== 0) mouseX *= this.stickFactor;
    if (BABYLON.Toolkit.InputController.a_mousey !== 0) mouseY *= this.arrowFactor;
    else if (BABYLON.Toolkit.InputController.g_mousey1 !== 0) mouseY *= this.stickFactor;

    // Introduce Damping Factors
    const dampingFactorX = 0.5; // Adjust as needed
    const dampingFactorY = 0.5; // Adjust as needed

    // Calculate Magnitude of Mouse Inputs
    let magnitudeX = Math.abs(mouseX);
    let magnitudeY = Math.abs(mouseY);

    // Apply Damping
    if (magnitudeX > magnitudeY) {
        mouseY *= dampingFactorY;
    } else {
        mouseX *= dampingFactorX;
    }

    // Calculate Controller Rotations
    this.#turnRotationY += mouseX;
    this.#lookRotationX -= mouseY;
    this.#lookRotationX = BABYLON.Scalar.Clamp(this.#lookRotationX, -upAngle, downAngle);

    // Check Non Mobile Platform Options
    if (!BABYLON.Toolkit.WindowManager.IsMobile())
    {
        if (this.engine.cameraTransform != null)
        {
            if (Math.abs(this.engine.cameraTransform.rotation.x - this.#lookRotationX) < this.#snapThreshold)
            {
                // Snap To Target Pitch If Close Enough
                this.engine.cameraTransform.rotation.x = this.#lookRotationX;
            }
            else
            {
                // Smoothly Interpolate To Pitch Rotation
                this.engine.cameraTransform.rotation.x = BABYLON.Scalar.Lerp(this.engine.cameraTransform.rotation.x, this.#lookRotationX, (this.lookEasing * deltaTime));
            }                
        }
    }

    // Update Player Character Controller Movement
    if (this.engine.playerTransform != null)
}

This implementation introduces damping factors for both mouse X and Y inputs. When one axis has a significantly larger input, the other axis’s input is dampened, reducing the wild movements while still allowing for smooth control.

1 Like