Skip to content

Commit a3de220

Browse files
committed
Mouse/gamepad look around rewritten
pitch of camera rotation/position based on vehicle velocity
1 parent e700106 commit a3de220

File tree

2 files changed

+115
-92
lines changed

2 files changed

+115
-92
lines changed

script.cpp

Lines changed: 106 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,15 @@ float DegToRad(const float &deg)
401401
return float(deg * M_PI / 180.0);
402402
}
403403

404-
Quaternionf QuatEuler(Vector3f &camVec)
404+
Quaternionf QuatEuler(Vector3f &euler)
405405
{
406-
Vector3f rotVec = DegToRad(1.0f) * camVec;
406+
Vector3f rotVec = DegToRad(1.0f) * euler;
407407

408408
Matrix3f xRot = AngleAxisf(rotVec.x(), Vector3f(1.0f, 0.0f, 0.0f)).matrix();
409409
Matrix3f yRot = AngleAxisf(rotVec.y(), Vector3f(0.0f, 1.0f, 0.0f)).matrix();
410410
Matrix3f zRot = AngleAxisf(rotVec.z(), Vector3f(0.0f, 0.0f, 1.0f)).matrix();
411411

412-
Matrix3f rot = zRot * yRot*xRot;
412+
Matrix3f rot = zRot * yRot * xRot;
413413

414414
return Quaternionf(rot);
415415
}
@@ -427,32 +427,8 @@ Vector2i getMouseCoords() {
427427
return Vector2i(mX, mY);
428428
}
429429

430-
void updateMouseState() {
431-
int lastX = lastMouseCoords.x();
432-
int lastY = lastMouseCoords.y();
433-
434-
Vector2i currXY = getMouseCoords();
435-
int currX = currXY.x();
436-
int currY = currXY.y();
437-
438-
int movX = abs(lastX - currX);
439-
int movY = abs(lastY - currY);
440-
441-
mouseDeltaX = currX - lastX;
442-
mouseDeltaY = currY - lastY;
443-
444-
if (isAiming || movX >= 1 || movY >= 1) { // Mouse moved on last frame
445-
446-
hasMouseInputThisFrame = true;
447-
448-
if (isInVehicle && customCamEnabled && mouseMoveCountdown <= 0.0001f && smoothIsMouseLooking <= 0.001f)
449-
setGameplayCamRelativeRotation(relAngle3p); // Sync gameplay cam rotarion
450-
451-
mouseMoveCountdown = 1.5f;
452-
}
453-
else
454-
hasMouseInputThisFrame = false;
455-
430+
void updateMouseState()
431+
{
456432
float x = CONTROLS::GET_CONTROL_NORMAL(2, eControl::ControlLookLeftRight);
457433
float y = CONTROLS::GET_CONTROL_NORMAL(2, eControl::ControlLookUpDown);
458434

@@ -466,11 +442,6 @@ void updateMouseState() {
466442
}
467443
else
468444
hasInputThisFrame = false;
469-
470-
if((veh != NULL && (vehSpeed > 0.1)) || veh == NULL)
471-
mouseMoveCountdown = max(0.f, mouseMoveCountdown - (SYSTEM::TIMESTEP() * clamp01(vehSpeed * 0.08f)));
472-
473-
lastMouseCoords = currXY;
474445
}
475446

476447
void setGameplayCamRelativeRotation(float heading) {
@@ -866,10 +837,6 @@ Vector3f lookRotEuler(Vector3f dir, Vector3f upVector) {
866837
return lookM.eulerAngles(2, 1, 0);
867838
}
868839

869-
Vector3f QuatEuler(Quaternionf quat) {
870-
return quat.toRotationMatrix().eulerAngles(1, 0, 2);
871-
}
872-
873840
Quaternionf getEntityQuaternion(Entity entity) {
874841

875842
float x, y, z, w;
@@ -1085,49 +1052,63 @@ void updateCameraDriverSeat() {
10851052
//smoothRotSeat = Vector3fLerpAngle(smoothRotSeat, rot, clamp01(30.f * getDeltaTime()));
10861053
smoothQuatSeat = getEntityQuaternion(veh);
10871054

1088-
if (smoothIsMouseLooking > 0.001f && btnLookingFactor < 0.001f) {
1089-
Vector3f gameplayCamRot = toV3f(CAM::GET_GAMEPLAY_CAM_ROT(2));
1090-
Vector3f finalRotSeat = Vector3fLerpAngle(rot, gameplayCamRot, smoothIsMouseLooking);
1091-
CAM::SET_CAM_ROT(customCam, finalRotSeat.x(), finalRotSeat.y(), finalRotSeat.z(), 2);
1092-
}
1093-
else
1055+
float leftRightAngle = RelativeLookFactor < 0 ?
1056+
lerp(0.f, -LookLeftAngle1p, -RelativeLookFactor)
1057+
:
1058+
lerp(0.f, LookRightAngle1p, RelativeLookFactor)
1059+
;
1060+
1061+
float leftRightRad = leftRightAngle * DEG_TO_RAD;
1062+
1063+
float roll = 0.f, pitch = 0.f, yaw = leftRightRad;
1064+
Quaternionf qLookLeftRight;
1065+
qLookLeftRight = AngleAxisf(roll, Vector3f::UnitX())
1066+
* AngleAxisf(pitch, Vector3f::UnitY())
1067+
* AngleAxisf(yaw, Vector3f::UnitZ());
1068+
1069+
Quaternionf finalQ = smoothQuatSeat * qLookLeftRight;
1070+
1071+
if (isAiming || hasInputThisFrame)
10941072
{
1095-
float leftRightAngle = RelativeLookFactor < 0 ?
1096-
lerp(0.f, -LookLeftAngle1p, -RelativeLookFactor)
1097-
:
1098-
lerp(0.f, LookRightAngle1p, RelativeLookFactor)
1099-
;
1073+
if (timerResetLook < 0.00001f)
1074+
{
1075+
lookQuat = finalQ;
1076+
}
1077+
timerResetLook = 2.f;
11001078

1101-
float leftRightRad = leftRightAngle * DEG_TO_RAD;
1079+
float mx = (CONTROLS::GET_CONTROL_NORMAL(2, eControl::ControlLookLeftRight)) * -5.f;
1080+
float my = (CONTROLS::GET_CONTROL_NORMAL(2, eControl::ControlLookUpDown)) * (LastInputMethodWasMouseAndKeyboard ? -5.f : 5.f);
11021081

1103-
float roll = 0.f, pitch = 0.f, yaw = leftRightRad;
1104-
Quaternionf qLookLeftRight;
1105-
qLookLeftRight = AngleAxisf(roll, Vector3f::UnitX())
1106-
* AngleAxisf(pitch, Vector3f::UnitY())
1107-
* AngleAxisf(yaw, Vector3f::UnitZ());
1082+
if (!LastInputMethodWasMouseAndKeyboard())
1083+
{
1084+
mx *= 0.6f;
1085+
my *= 0.6f;
1086+
}
1087+
1088+
Vector3f vecLook = Vector3f(my, 0.f, mx);
1089+
1090+
Quaternionf result = lookQuat * QuatEuler(vecLook);
1091+
Vector3f resultEuler = QuatToEuler(result);
11081092

1109-
SET_CAM_QUATERNION(customCam, smoothQuatSeat * qLookLeftRight);
1093+
float rx = clamp(resultEuler[0], -62.f, 40.f);
11101094

1095+
lookQuat = QuatEuler(Vector3f(rx, 0.f, resultEuler[2]));
11111096
}
1097+
1098+
timerResetLook = clamp(timerResetLook - getDeltaTime(), 0.f, 2.f);
1099+
1100+
finalQ = slerp(finalQ, lookQuat, clamp01(timerResetLook));
1101+
1102+
SET_CAM_QUATERNION(customCam, finalQ);
11121103
}
11131104
else
11141105
{
11151106
CAM::STOP_CAM_POINTING(customCam);
11161107

11171108
Vector3f rot = toV3f(ENTITY::GET_ENTITY_ROTATION(veh, 2));
1118-
Quaternionf rotQuat = getEntityQuaternion(veh);
1119-
//smoothRotSeat = Vector3fLerpAngle(smoothRotSeat, rot, clamp01(30.f * SYSTEM::TIMESTEP()));
1120-
//smoothRotSeat = Vector3fInertialDampAngle(smoothRotSeat, rot, 0.06f);
1121-
smoothRotSeat = Vector3fLerpAngle(smoothRotSeat, rot, clamp01(30.f * getDeltaTime()));
1122-
smoothQuatSeat = slerp(smoothQuatSeat, rotQuat, clamp01(30.f * getDeltaTime()));
1123-
1124-
if (smoothIsMouseLooking > 0.001f && btnLookingFactor < 0.001f) {
1125-
Vector3f gameplayCamRot = toV3f(CAM::GET_GAMEPLAY_CAM_ROT(2));
1126-
Vector3f finalRotSeat = Vector3fLerpAngle(smoothRotSeat, gameplayCamRot, smoothIsMouseLooking);
1127-
CAM::SET_CAM_ROT(customCam, finalRotSeat.x(), finalRotSeat.y(), finalRotSeat.z(), 2);
1128-
}
1129-
else
1130-
{
1109+
smoothQuatSeat = getEntityQuaternion(veh);
1110+
1111+
11311112
float leftRightAngle = RelativeLookFactor < 0 ?
11321113
lerp(0.f, -LookLeftAngle1p, -RelativeLookFactor)
11331114
:
@@ -1142,8 +1123,40 @@ void updateCameraDriverSeat() {
11421123
* AngleAxisf(pitch, Vector3f::UnitY())
11431124
* AngleAxisf(yaw, Vector3f::UnitZ());
11441125

1145-
SET_CAM_QUATERNION(customCam, smoothQuatSeat * qLookLeftRight);
1146-
}
1126+
Quaternionf finalQ = smoothQuatSeat * qLookLeftRight;
1127+
1128+
if (isAiming || hasInputThisFrame)
1129+
{
1130+
if (timerResetLook < 0.00001f)
1131+
{
1132+
lookQuat = finalQ;
1133+
}
1134+
timerResetLook = 2.f;
1135+
1136+
float mx = (CONTROLS::GET_CONTROL_NORMAL(2, eControl::ControlLookLeftRight)) * -5.f;
1137+
float my = (CONTROLS::GET_CONTROL_NORMAL(2, eControl::ControlLookUpDown)) * (LastInputMethodWasMouseAndKeyboard ? -5.f : 5.f);
1138+
1139+
if (!LastInputMethodWasMouseAndKeyboard())
1140+
{
1141+
mx *= 0.6f;
1142+
my *= 0.6f;
1143+
}
1144+
1145+
Vector3f vecLook = Vector3f(my, 0.f, mx);
1146+
1147+
Quaternionf result = lookQuat * QuatEuler(vecLook);
1148+
Vector3f resultEuler = QuatToEuler(result);
1149+
1150+
float rx = clamp(resultEuler[0], -62.f, 40.f);
1151+
1152+
lookQuat = QuatEuler(Vector3f(rx, 0.f, resultEuler[2]));
1153+
}
1154+
1155+
timerResetLook = clamp(timerResetLook - getDeltaTime(), 0.f, 2.f);
1156+
1157+
finalQ = slerp(finalQ, lookQuat, clamp01(timerResetLook));
1158+
1159+
SET_CAM_QUATERNION(customCam, finalQ);
11471160
}
11481161

11491162
CAM::RENDER_SCRIPT_CAMS(true, false, 3000, 1, 0);
@@ -1468,20 +1481,20 @@ void updateCam3pNfsAlgorithm()
14681481

14691482
Quaternionf vehQuat = getEntityQuaternion(veh);
14701483
smoothQuat3P = slerp(smoothQuat3P, vehQuat, 3.f * getDeltaTime());
1471-
ultraSmoothQuat3P = slerp(ultraSmoothQuat3P, vehQuat, 1.8f * getDeltaTime());
14721484
velocityQuat3P = lookRotation(smoothVelocity);
14731485

1474-
if (isBike && vehSpeed > 1.f)
1486+
//float vehPitch = QuatToEuler(velocityQuat3P).x();
1487+
1488+
if (/*isBike && */ vehSpeed > 1.f)
14751489
{
1476-
Vector3f smoothEuler = QuatToEuler(ultraSmoothQuat3P);
1490+
Quaternionf quatVel = velocityQuat3P;
14771491

1478-
Vector3f latv = vehRightVector.cross(smoothVelocity);
1479-
latv[1] = 0.f;
1480-
latv.normalize();
1492+
Vector3f VquatVel = QuatToEuler(velocityQuat3P);
1493+
Vector3f sm3p = QuatToEuler(smoothQuat3P);
14811494

1482-
smoothEuler[0] = latv[0];
1495+
VquatVel[2] = sm3p[2];
14831496

1484-
smoothQuat3P = QuatEuler(smoothEuler);
1497+
smoothQuat3P = QuatEuler(VquatVel);
14851498
}
14861499

14871500
currentTowHeightIncrement = lerp(currentTowHeightIncrement, towHeightIncrement, 1.45f * getDeltaTime());
@@ -1563,12 +1576,12 @@ void updateCam3pNfsAlgorithm()
15631576
timerResetLook = 2.f;
15641577

15651578
float mx = (CONTROLS::GET_CONTROL_NORMAL(2, eControl::ControlLookLeftRight)) * -5.f;
1566-
float my = (CONTROLS::GET_CONTROL_NORMAL(2, eControl::ControlLookUpDown)) * -5.f;
1579+
float my = (CONTROLS::GET_CONTROL_NORMAL(2, eControl::ControlLookUpDown)) * (LastInputMethodWasMouseAndKeyboard ? -5.f : 5.f);
15671580

15681581
if (!LastInputMethodWasMouseAndKeyboard())
15691582
{
15701583
mx *= 0.6f;
1571-
my *= -0.6f;
1584+
my *= 0.6f;
15721585
}
15731586

15741587
Vector3f vecLook = Vector3f(my, 0.f, mx);
@@ -1586,7 +1599,18 @@ void updateCam3pNfsAlgorithm()
15861599
finalQuat3P = slerp(finalQuat3P, lookQuat, clamp01(timerResetLook));
15871600
//finalQuat3P = lookQuat;
15881601

1589-
Vector3f camPosSmooth = posCenter + extraCamHeight + V3CurrentTowHeightIncrement + (finalQuat3P * back * ((longitudeOffset3P) + 0.15f + airDistance));
1602+
if (smoothIsAiming > 0.00001f) {
1603+
float currentFov = lerp(fov3P, fov3PAiming, smoothIsAiming);
1604+
CAM::SET_CAM_FOV(customCam, currentFov);
1605+
}
1606+
1607+
if (isAiming) {
1608+
UI::SHOW_HUD_COMPONENT_THIS_FRAME(eHudComponent::HudComponentReticle);
1609+
}
1610+
1611+
float aimHeightIncrement = lerp(0.f, 0.22f, smoothIsAiming);
1612+
1613+
Vector3f camPosSmooth = posCenter + extraCamHeight + V3CurrentTowHeightIncrement + (finalQuat3P * back * ((longitudeOffset3P) + 0.15f + airDistance)) + (up * aimHeightIncrement);
15901614
camPosSmooth += slerp(smoothQuat3P, velocityQuat3P, smoothIsInAir) * back * distIncFinal;
15911615

15921616
Vector3f camPosFinal = camPosSmooth;
@@ -1986,7 +2010,6 @@ void update()
19862010
}
19872011
else
19882012
{
1989-
veh = NULL;
19902013
ResetMouseLook();
19912014
DisableCustomCamera();
19922015
return;

script.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ const char* const vehicleBones[] =
162162
"steering",
163163
"hbgrip_l",
164164
"hbgrip_r",
165-
"headlight_l",
166-
"headlight_r",
165+
//"headlight_l",
166+
//"headlight_r",
167167
//"taillight_l",
168168
//"taillight_r",
169169
//"indicator_lf",
@@ -173,13 +173,13 @@ const char* const vehicleBones[] =
173173
//"brakelight_l",
174174
//"brakelight_r",
175175
//"brakelight_m",
176-
"reversinglight_l",
177-
"reversinglight_r",
178-
"extralight_1",
179-
"extralight_2",
180-
"extralight_3",
181-
"extralight_4",
182-
"numberplate",
176+
//"reversinglight_l",
177+
//"reversinglight_r",
178+
//"extralight_1",
179+
//"extralight_2",
180+
//"extralight_3",
181+
//"extralight_4",
182+
//"numberplate",
183183
//"interiorlight",
184184
"siren1",
185185
"siren2",

0 commit comments

Comments
 (0)