Exemple #1
0
        private Transform UpdateCameraRear(Ped player, Vehicle veh)
        {
            // smooth out current rotation and position
            currentRotation = Mathr.QuaternionNLerp(currentRotation, rearCamCurrentTransform.quaternion, (responsivenessMultiplier * generalMovementSpeed) * getDeltaTime());
            currentPos      = Vector3.Lerp(currentPos, rearCamCurrentTransform.position, Mathr.Clamp01((responsivenessMultiplier * generalMovementSpeed) * getDeltaTime()));

            Quaternion look;

            var wantedPos = veh.Position + (veh.ForwardVector * finalDummyOffset);

            // If veh has towed vehicle, increase camera distance by towed vehicle longitude

            // should camera be attached to vehicle's back or back his velocity
            var fixedVsVelocity = getFixedVsVelocityFactor(veh, speedCoeff);

            // Compute camera position rear the vechicle
            var wantedPosFixed = wantedPos - Vector3.Transform(Extensions.RelativeFront, veh.Quaternion) * fullLongitudeOffset;

            // smooth out velocity
            smoothVelocity = Mathr.Vector3SmoothDamp(smoothVelocity, Vector3.Normalize(veh.Velocity), ref smoothVelocitySmDamp, generalMovementSpeed, 9999999f, responsivenessMultiplier * getDeltaTime());
            // Compute camera postition rear the direction of the vehcle
            if (speedCoeff >= stoppedSpeed)
            {
                wantedPosVelocity = wantedPos + Vector3.Transform(Extensions.RelativeBottom, Mathr.QuaternionLookRotation(smoothVelocity)) * fullLongitudeOffset;
            }

            // Smooth factor between two above cam positions
            smoothFixedVsVelocity = Mathr.Lerp(smoothFixedVsVelocity, fixedVsVelocity, (fixedVsVelocitySpeed) * getDeltaTime());

            if (!isCycleOrByke)
            {
                tempSmoothVsVl        = Mathr.Lerp(tempSmoothVsVl, Mathr.Clamp(speedCoeff / 2.3f, 0.025f, 1f), (fixedVsVelocitySpeed / 20f));
                smoothFixedVsVelocity = Mathr.Lerp(0f, smoothFixedVsVelocity, tempSmoothVsVl);
            }

            wantedPos = Vector3.Lerp(wantedPosFixed, wantedPosVelocity, Mathr.Clamp01(smoothFixedVsVelocity));

            //currentPos = Vector3.Lerp(currentPos, wantedPos, Mathr.Clamp01((responsivenessMultiplier * cameraStickiness) * getDeltaTime()));
            currentPos = Vector3.Lerp(currentPos, wantedPos, Mathr.Clamp01((responsivenessMultiplier * cameraStickiness) * getDeltaTime()));

            //rearCamCurrentTransform.position = currentPos;
            mainCamera.PointAt(pointAt);
            look = Extensions.Euler(mainCamera.Rotation);

            // Rotate the camera towards the velocity vector.
            var finalCamRotationSpeed = Mathr.Lerp(cameraRotationSpeedLowSpeed, cameraRotationSpeed, ((speedCoeff / lowSpeedLimit) * 1.32f) * getDeltaTime() * 51f);

            look = Mathr.QuaternionNLerp(currentRotation, look, (1.8f * finalCamRotationSpeed) * getDeltaTime());

            //// Auto align over time
            //if (alignmentSpeed > 0.001f)
            //{
            //    var wantedRot = veh.Rotation;
            //    //wantedRot.y = 0f;
            //    //look = Mathr.QuaternionNLerp(look, Quaternion.Euler(wantedRot), (alignmentSpeed * getDeltaTime()) * (1 - smoothIsInAir));

            //    Transform alignTr = new Transform(currentPos + fullHeightOffset, Quaternion.Identity);
            //    alignTr.PointAt(pointAt);
            //    alignTr.quaternion = Quaternion.Lerp(alignTr.quaternion, Quaternion.Euler(wantedRot), alignmentSpeed * getDeltaTime() * (1 - smoothIsInAir));

            //    currentPos = veh.Position + fullHeightOffset + (alignTr.quaternion * Vector3.RelativeBack * (fullLongitudeOffset + currentDistanceIncrement));
            //}


            // Fix stuttering (mantain camera distance fixed in local space)
            Transform fixedDistanceTr = new Transform(currentPos + fullHeightOffset + (Extensions.WorldUp * extraCamHeight), Quaternion.Identity);

            fixedDistanceTr.PointAt(pointAt);

            Quaternion rotInitial = fixedDistanceTr.quaternion;

            // Auto alignment (WIP)

            //Vector3 rotBehindVector = veh.Position + fullHeightOffset - (veh.Quaternion * Vector3.RelativeFront * (fullLongitudeOffset + currentDistanceIncrement));
            //rotBehindVector.Z = fixedDistanceTr.position.Z;

            //Quaternion rotBehind = Mathr.LookAt(rotBehindVector, pointAt);

            //Quaternion finalRot = Quaternion.Lerp(rotInitial, rotBehind, alignmentSpeed * (1 - smoothIsInAir) * (1 - smoothIsRearGear));

            //fixedDistanceTr.position = veh.Position + fullHeightOffset + (finalRot * Vector3.RelativeBack * (fullLongitudeOffset + currentDistanceIncrement));
            // End Autoalignment

            fixedDistanceTr.position = veh.Position + fullHeightOffset + (Extensions.WorldUp * extraCamHeight) + (Vector3.Transform(Extensions.RelativeBack, rotInitial) * (fullLongitudeOffset + currentDistanceIncrement));

            //fixedDistanceTr.position = fixedDistanceTr.position + fullHeightOffset;

            var transform = new Transform();

            transform.position = fixedDistanceTr.position;
            //transform.position = currentPos + fullHeightOffset;
            transform.rotation   = look.ToEulerAngles();
            transform.quaternion = look;

            rearCamCurrentTransform = transform;

            return(transform);
        }
Exemple #2
0
        public async Task OnTick()
        {
            await Task.FromResult(0);

            if (init && customCamEnabled)
            {
                init = false;
            }

            var player = Game.Player.Character;

            if (player.IsInVehicle() && customCamEnabled && !Game.Player.IsAiming && !Game.IsControlPressed(2, Control.VehicleLookBehind))
            {
                Vehicle veh        = player.CurrentVehicle;
                var     NewVehHash = veh.GetHashCode();

                if (oldVehHash != NewVehHash)
                {
                    UpdateVehicleProperties(veh);
                }

                oldVehHash = NewVehHash;

                if (isSuitableForCam)
                {
                    Transform rearCameraTransform;
                    Transform cameraMouseLooking;

                    updateMouseAndGamepadInput(veh);

                    processCameraCommon(player, veh);

                    if (!camSet)
                    {
                        // Just entered on vehicle
                        Function.Call(Hash.SET_FOLLOW_VEHICLE_CAM_VIEW_MODE, 0);

                        ResetMouseLookTween();
                        ResetSmoothValues(veh);
                        SetupCamera(player, veh);
                        //setupDebugStats(veh);

                        if (firstVeh && notifyModEnabled)
                        {
                            firstVeh = false;
                        }
                    }
                    else
                    {
                        if (showDebugStats)
                        {
                            //drawDebugStats(veh);
                        }
                    }
                    Game.DisableControlThisFrame(2, Control.NextCamera);

                    isRearCameraOnly  = smoothIsMouseLooking < 0.005f;
                    isMouseCameraOnly = smoothIsMouseLooking > 0.995f;

                    if (isRearCameraOnly)
                    {
                        rearCameraTransform = UpdateCameraRear(player, veh);

                        mainCamera.Position = rearCameraTransform.position;
                        mainCamera.Rotation = rearCameraTransform.rotation;
                    }
                    else if (isMouseCameraOnly)
                    {
                        cameraMouseLooking = UpdateCameraMouse(player, veh);

                        mainCamera.Position = cameraMouseLooking.position;
                        //mainCamera.Rotation = cameraMouseLooking.rotation;

                        // ugly fix, but works (still some stuttering while mouse looking)
                        mainCamera.PointAt(pointAt);
                    }
                    else
                    {
                        rearCameraTransform = UpdateCameraRear(player, veh);
                        cameraMouseLooking  = UpdateCameraMouse(player, veh);

                        mainCamera.Position = Vector3.Lerp(rearCameraTransform.position, cameraMouseLooking.position, Mathr.Clamp01(smoothIsMouseLooking));
                        mainCamera.Rotation = Mathr.QuaternionNLerp(rearCameraTransform.quaternion, cameraMouseLooking.quaternion, smoothIsMouseLooking).ToEulerAngles();
                    }
                }
            }
            else if (camSet)
            {
                ExitCustomCameraView();
            }

            if (camSet && !customCamEnabled)
            {
                ExitCustomCameraView();
            }

            //tweener.Update(realDelta);
            tweener.Update(getDeltaTime());
        }