Exemple #1
0
    public override void UpdateCamera(Camera camera)
    {
        if (!Player.Local.IsValid())
        {
            return;
        }

        WorldPos = camera.Pos;
        WorldRot = camera.Rot;

        camera.ViewModelFieldOfView = FieldOfView;

        var newPitch = WorldRot.Pitch();
        var newYaw   = WorldRot.Yaw();

        var pitchDelta = Angles.NormalizeAngle(newPitch - lastPitch);
        var yawDelta   = Angles.NormalizeAngle(lastYaw - newYaw);

        var playerVelocity = Player.Local.Velocity;
        var verticalDelta  = playerVelocity.z * Time.Delta;
        var viewDown       = Rotation.FromPitch(newPitch).Up * -1.0f;

        verticalDelta *= (1.0f - System.MathF.Abs(viewDown.Cross(Vector3.Down).y));
        pitchDelta    -= verticalDelta * 1;

        var offset = CalcSwingOffset(pitchDelta, yawDelta);

        offset += CalcBobbingOffset(playerVelocity);

        WorldPos += WorldRot * offset;

        lastPitch = newPitch;
        lastYaw   = newYaw;
    }
    public void OnPostPhysicsStep(float dt)
    {
        if (!PhysicsBody.IsValid())
        {
            return;
        }

        var body      = PhysicsBody;
        var transform = Transform;

        body.LinearDrag     = 1.0f;
        body.AngularDrag    = 1.0f;
        body.LinearDamping  = 4.0f;
        body.AngularDamping = 4.0f;

        var yawRot            = Rotation.From(new Angles(0, WorldRot.Angles().yaw, 0));
        var worldMovement     = yawRot * currentInput.movement;
        var velocityDirection = body.Velocity.WithZ(0);
        var velocityMagnitude = velocityDirection.Length;

        velocityDirection = velocityDirection.Normal;

        var velocityScale = (velocityMagnitude / leanMaxVelocity).Clamp(0, 1);
        var leanDirection = worldMovement.LengthSquared == 0.0f
                        ? -velocityScale * velocityDirection
                        : worldMovement;

        var targetUp  = (Vector3.Up + leanDirection * leanWeight * velocityScale).Normal;
        var currentUp = transform.NormalToWorld(Vector3.Up);
        var alignment = Math.Max(Vector3.Dot(targetUp, currentUp), 0);

        bool hasCollision = false;
        bool isGrounded   = false;

        if (!hasCollision || isGrounded)
        {
            var hoverForce    = isGrounded && currentInput.throttle <= 0 ? Vector3.Zero : -1 * transform.NormalToWorld(Vector3.Up) * -800.0f;
            var movementForce = isGrounded ? Vector3.Zero : worldMovement * movementAcceleration;
            var altitudeForce = transform.NormalToWorld(Vector3.Up) * currentInput.throttle * altitudeAcceleration;
            var totalForce    = hoverForce + movementForce + altitudeForce;
            body.ApplyForce((totalForce * alignment) * body.Mass);
        }

        if (!hasCollision && !isGrounded)
        {
            var spinTorque       = Transform.NormalToWorld(new Vector3(0, 0, currentInput.yaw * yawSpeed));
            var uprightTorque    = Vector3.Cross(currentUp, targetUp) * uprightSpeed;
            var uprightAlignment = alignment < uprightDot ? 0 : alignment;
            var totalTorque      = spinTorque * alignment + uprightTorque * uprightAlignment;
            body.ApplyTorque((totalTorque * alignment) * body.Mass);
        }
    }