Skip to content

Commit 7c4a7d5

Browse files
authored
Merge pull request #1 from Rbn3D/mt_support
Look left/right/back implemented. Support for Manual Transmission mod.
2 parents e0bd106 + 79bfb2b commit 7c4a7d5

File tree

3 files changed

+198
-78
lines changed

3 files changed

+198
-78
lines changed

CustomCameraVPlus.ini

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,24 @@
22
fov = 77.5
33
distanceOffset = 0.0
44

5+
lookLeftAngle = 90.0
6+
lookRightAngle = 90.0
7+
58
[1stPersonView]
6-
fov = 77.5
9+
fov = 75.0
10+
11+
lookLeftAngle = 75.0
12+
lookRightAngle = 80.0
13+
14+
[general]
15+
16+
# Support Look left/right/behind from Manual Transmission mod by ikt
17+
# Set to 1 to enable, 0 to disable
18+
GetInputFromGearsAsi = 1
19+
20+
[keyMappings]
21+
reloadSettingsKey = F10
22+
toggleModKey = 1
23+
24+
lookLeftKey = B
25+
lookRightKey = N

script.cpp

Lines changed: 164 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -147,41 +147,23 @@ float relAngle3p = 0.f;
147147
const float DEG_TO_RAD = 0.0174532925f;
148148
const float RADIAN_TO_DEG = 57.29577951f;
149149

150-
//Matrix4f CrossProductMatrix(const Vector3f & n)
151-
//{
152-
// Matrix4f m;
153-
//
154-
// m << 0.0f, -n.z, n.y, 0.0f,
155-
// n.z, 0.0f, -n.x, 0.0f,
156-
// -n.y, n.x, 0.0f, 0.0f,
157-
// 0.0f, 0.0f, 0.0f, 1.0f;
158-
//
159-
// return m;
160-
//}
161-
//
162-
//Matrix4f TensorProductMatrix(const Vector3f & n)
163-
//{
164-
// Matrix4f m;
165-
//
166-
// m << n.x * n.x, n.x * n.y, n.x * n.z, 0.0f,
167-
// n.y * n.x, n.y * n.y, n.y * n.z, 0.0f,
168-
// n.z * n.x, n.z * n.y, n.z * n.z, 0.0f,
169-
// 0.0f, 0.0f, 0.0f, 1.0f;
170-
//
171-
// return m;
172-
//}
173-
//
174-
//Vector3f RotateAround(const Vector3f & axis, float amount, const Vector3f & vectorToRotate)
175-
//{
176-
// Matrix4f identity;
177-
// identity.Identity();
178-
// Matrix4f first = identity * cos(amount);
179-
// Matrix4f second = TensorProductMatrix(axis) * (1 - cos(amount));
180-
// Matrix4f third = CrossProductMatrix(axis) * sin(amount);
181-
// Vector4f endVal = (first + second + third) * vectorToRotate;
182-
// return Vector3f(endVal.x, endVal.y, endVal.z);
183-
//}
150+
float LookLeftAngle1p = 75.0f;
151+
float LookRightAngle1p = 80.0f;
152+
153+
float LookLeftAngle3p = 90.0f;
154+
float LookRightAngle3p = 90.0f;
155+
156+
bool isLookingBack = false;
157+
158+
// look left = -1.f; lookRight = 1.f; Center = 0.f (For smooth transitions)
159+
float RelativeLookFactor = 0.f;
184160

161+
bool readInputFromMt = true;
162+
163+
char * reloadKey = "F10";
164+
char * toggleModKey = "1";
165+
char * lookLeftKey = "B";
166+
char * lookRightKey = "N";
185167

186168
bool AreSameFloat(float a, float b)
187169
{
@@ -486,14 +468,36 @@ void ReadSettings(bool notify)
486468
SI_Error res = ini.LoadFile("CustomCameraVPlus.ini");
487469

488470
if (res == SI_Error::SI_OK) {
471+
const char* dist3rdPerson = ini.GetValue("3rdPersonView", "distanceOffset", "0.0");
472+
489473
const char* fov3rdPerson = ini.GetValue("3rdPersonView", "fov", "77.5");
490474
const char* fov1stPerson = ini.GetValue("1stPersonView", "fov", "77.5");
491475

492-
const char* dist3rdPerson = ini.GetValue("3rdPersonView", "distanceOffset", "0.0");
476+
const char* c3rdPersonLookLeft = ini.GetValue("3rdPersonView", "lookLeftAngle", "90.0");
477+
const char* c3rdPersonLookRight = ini.GetValue("3rdPersonView", "lookRightAngle", "90.0");
478+
479+
const char* c1stPersonLookLeft = ini.GetValue("1stPersonView", "lookLeftAngle", "75.0");
480+
const char* c1stPersonLookRight = ini.GetValue("1stPersonView", "lookRightAngle", "80.0");
481+
482+
const char* cReadFromMt = ini.GetValue("general", "GetInputFromGearsAsi", "1");
483+
484+
reloadKey = strdup(ini.GetValue("keyMappings", "reloadSettingsKey", "F10"));
485+
toggleModKey = strdup(ini.GetValue("keyMappings", "toggleModKey", "1"));
486+
lookLeftKey = strdup(ini.GetValue("keyMappings", "lookLeftKey", "B"));
487+
lookRightKey = strdup(ini.GetValue("keyMappings", "lookRightKey", "N"));
488+
489+
distanceOffset = std::stof(dist3rdPerson);
493490

494491
fov3P = std::stof(fov3rdPerson);
495492
fov1P = std::stof(fov1stPerson);
496-
distanceOffset = std::stof(dist3rdPerson);
493+
494+
LookLeftAngle3p = std::stof(c3rdPersonLookLeft);
495+
LookRightAngle3p = std::stof(c3rdPersonLookRight);
496+
497+
LookLeftAngle1p = std::stof(c1stPersonLookLeft);
498+
LookRightAngle1p = std::stof(c1stPersonLookRight);
499+
500+
readInputFromMt = std::stoi(cReadFromMt) > 0;
497501

498502
if (notify) {
499503
ShowNotification("CCVPlus: Settings reloaded");
@@ -998,7 +1002,7 @@ void setupCustomCamera() {
9981002

9991003
void updateCameraDriverSeat() {
10001004

1001-
if (CONTROLS::IS_CONTROL_PRESSED(0, eControl::ControlLookBehind))
1005+
if (CONTROLS::IS_CONTROL_PRESSED(0, eControl::ControlLookBehind) || isLookingBack)
10021006
{
10031007
lookBehind1p();
10041008

@@ -1036,48 +1040,111 @@ void updateCameraDriverSeat() {
10361040

10371041
CAM::SET_CAM_COORD(customCam, camPos.x(), camPos.y(), camPos.z());
10381042

1043+
float btnLookingFactor = abs(RelativeLookFactor);
1044+
10391045
if (isBike) {
10401046
CAM::STOP_CAM_POINTING(customCam);
10411047
Vector3f rot = toV3f(ENTITY::GET_ENTITY_ROTATION(veh, 2));
10421048
rot[1] = rot.y() * 0.5f; // rot.y = rot.y * .5f
10431049

1044-
if (smoothIsMouseLooking > 0.001f) {
1050+
//smoothRotSeat = Vector3fLerpAngle(smoothRotSeat, rot, clamp01(30.f * getDeltaTime()));
1051+
smoothQuatSeat = getEntityQuaternion(veh);
1052+
1053+
if (smoothIsMouseLooking > 0.001f && btnLookingFactor < 0.001f) {
10451054
Vector3f gameplayCamRot = toV3f(CAM::GET_GAMEPLAY_CAM_ROT(2));
10461055
Vector3f finalRotSeat = Vector3fLerpAngle(rot, gameplayCamRot, smoothIsMouseLooking);
10471056
CAM::SET_CAM_ROT(customCam, finalRotSeat.x(), finalRotSeat.y(), finalRotSeat.z(), 2);
10481057
}
10491058
else
10501059
{
1051-
CAM::SET_CAM_ROT(customCam, rot.x(), rot.y(), rot.z(), 2);
1060+
float leftRightAngle = RelativeLookFactor < 0 ?
1061+
lerp(0.f, -LookLeftAngle1p, -RelativeLookFactor)
1062+
:
1063+
lerp(0.f, LookRightAngle1p, RelativeLookFactor)
1064+
;
1065+
1066+
float leftRightRad = leftRightAngle * DEG_TO_RAD;
1067+
1068+
float roll = 0.f, pitch = 0.f, yaw = leftRightRad;
1069+
Quaternionf qLookLeftRight;
1070+
qLookLeftRight = AngleAxisf(roll, Vector3f::UnitX())
1071+
* AngleAxisf(pitch, Vector3f::UnitY())
1072+
* AngleAxisf(yaw, Vector3f::UnitZ());
1073+
1074+
SET_CAM_QUATERNION(customCam, smoothQuatSeat * qLookLeftRight);
1075+
10521076
}
10531077
}
10541078
else
10551079
{
1056-
CAM::STOP_CAM_POINTING(customCam);
1057-
1058-
Vector3f rot = toV3f(ENTITY::GET_ENTITY_ROTATION(veh, 2));
1059-
Quaternionf rotQuat = getEntityQuaternion(veh);
1060-
//smoothRotSeat = Vector3fLerpAngle(smoothRotSeat, rot, clamp01(30.f * SYSTEM::TIMESTEP()));
1061-
//smoothRotSeat = Vector3fInertialDampAngle(smoothRotSeat, rot, 0.06f);
1062-
smoothRotSeat = Vector3fLerpAngle(smoothRotSeat, rot, clamp01(30.f * getDeltaTime()));
1063-
smoothQuatSeat = slerp(smoothQuatSeat, rotQuat, clamp01(30.f * getDeltaTime()));
1064-
1065-
if (smoothIsMouseLooking > 0.001f) {
1066-
Vector3f gameplayCamRot = toV3f(CAM::GET_GAMEPLAY_CAM_ROT(2));
1067-
Vector3f finalRotSeat = Vector3fLerpAngle(smoothRotSeat, gameplayCamRot, smoothIsMouseLooking);
1068-
CAM::SET_CAM_ROT(customCam, finalRotSeat.x(), finalRotSeat.y(), finalRotSeat.z(), 2);
1069-
}
1070-
else
1071-
{
1072-
//CAM::SET_CAM_ROT(customCam, smoothRotSeat.x(), smoothRotSeat.y(), smoothRotSeat.z(), 2);
1073-
SET_CAM_QUATERNION(customCam, smoothQuatSeat);
1074-
}
1075-
1080+
CAM::STOP_CAM_POINTING(customCam);
1081+
1082+
Vector3f rot = toV3f(ENTITY::GET_ENTITY_ROTATION(veh, 2));
1083+
Quaternionf rotQuat = getEntityQuaternion(veh);
1084+
//smoothRotSeat = Vector3fLerpAngle(smoothRotSeat, rot, clamp01(30.f * SYSTEM::TIMESTEP()));
1085+
//smoothRotSeat = Vector3fInertialDampAngle(smoothRotSeat, rot, 0.06f);
1086+
smoothRotSeat = Vector3fLerpAngle(smoothRotSeat, rot, clamp01(30.f * getDeltaTime()));
1087+
smoothQuatSeat = slerp(smoothQuatSeat, rotQuat, clamp01(30.f * getDeltaTime()));
1088+
1089+
if (smoothIsMouseLooking > 0.001f && btnLookingFactor < 0.001f) {
1090+
Vector3f gameplayCamRot = toV3f(CAM::GET_GAMEPLAY_CAM_ROT(2));
1091+
Vector3f finalRotSeat = Vector3fLerpAngle(smoothRotSeat, gameplayCamRot, smoothIsMouseLooking);
1092+
CAM::SET_CAM_ROT(customCam, finalRotSeat.x(), finalRotSeat.y(), finalRotSeat.z(), 2);
1093+
}
1094+
else
1095+
{
1096+
float leftRightAngle = RelativeLookFactor < 0 ?
1097+
lerp(0.f, -LookLeftAngle1p, -RelativeLookFactor)
1098+
:
1099+
lerp(0.f, LookRightAngle1p, RelativeLookFactor)
1100+
;
1101+
1102+
float leftRightRad = leftRightAngle * DEG_TO_RAD;
1103+
1104+
float roll = 0.f, pitch = 0.f, yaw = leftRightRad;
1105+
Quaternionf qLookLeftRight;
1106+
qLookLeftRight = AngleAxisf(roll, Vector3f::UnitX())
1107+
* AngleAxisf(pitch, Vector3f::UnitY())
1108+
* AngleAxisf(yaw, Vector3f::UnitZ());
1109+
1110+
SET_CAM_QUATERNION(customCam, smoothQuatSeat * qLookLeftRight);
1111+
}
10761112
}
10771113

10781114
CAM::RENDER_SCRIPT_CAMS(true, false, 3000, 1, 0);
10791115
}
10801116

1117+
void ProccessLookLeftRightOrBackInput()
1118+
{
1119+
const float rotSpeed = 9.f;
1120+
1121+
bool evalLeft = IsKeyDown(str2key(lookLeftKey)) || (readInputFromMt && GetDecoratorBool("mt_looking_left"));
1122+
bool evalRight = IsKeyDown(str2key(lookRightKey)) || (readInputFromMt && GetDecoratorBool("mt_looking_right"));
1123+
1124+
isLookingBack = CONTROLS::IS_CONTROL_PRESSED(0, eControl::ControlVehicleLookBehind) || (readInputFromMt && GetDecoratorBool("mt_looking_back")) || (evalLeft && evalRight);
1125+
1126+
if (evalLeft && !evalRight) {
1127+
RelativeLookFactor += rotSpeed * getDeltaTime();
1128+
}
1129+
else if (evalRight) {
1130+
RelativeLookFactor -= rotSpeed * getDeltaTime();
1131+
}
1132+
else
1133+
{
1134+
if (RelativeLookFactor > 0.f)
1135+
RelativeLookFactor = clamp01(RelativeLookFactor - rotSpeed * getDeltaTime());
1136+
else
1137+
RelativeLookFactor = clamp(RelativeLookFactor + rotSpeed * getDeltaTime(), -1.f, 0.f);
1138+
}
1139+
1140+
RelativeLookFactor = clamp(RelativeLookFactor, -1.f, 1.f);
1141+
}
1142+
1143+
BOOL GetDecoratorBool(char * decoratorKey)
1144+
{
1145+
return DECORATOR::DECOR_IS_REGISTERED_AS_TYPE(decoratorKey, eDecorType::DECOR_TYPE_BOOL) && DECORATOR::DECOR_EXIST_ON(veh, decoratorKey) && DECORATOR::DECOR_GET_BOOL(veh, (char *)decoratorKey);
1146+
}
1147+
10811148
void lookBehind1p()
10821149
{
10831150
Vector3f camPos;
@@ -1195,22 +1262,36 @@ void updateCameraSmooth3P() {
11951262

11961263
Quaternionf finalQuat = slerp(smoothQuat3P, velocityQuat3P, smoothIsInAir);
11971264

1198-
/*viewLock = clamp01(viewLock - (getDeltaTime() *10.f));
1199-
viewLock = clamp01(viewLock + (vehSpeed *.05f));
1265+
Quaternionf mouseLookRot = lookRotation(getGameplayCameraDirection());
12001266

1201-
smoothViewLock = lerp(smoothViewLock, viewLock, clamp01(1.6f * getDeltaTime()));
1267+
bool lookBehind = false;
1268+
if (CONTROLS::IS_CONTROL_PRESSED(0, eControl::ControlLookBehind) || isLookingBack)
1269+
lookBehind = true;
12021270

1203-
float vehAcceleration = getVehicleAcceleration();
1271+
float leftRightAngle = 0.f;
1272+
if (!lookBehind) {
1273+
leftRightAngle = RelativeLookFactor < 0 ?
1274+
lerp(0.f, -LookLeftAngle3p, -RelativeLookFactor)
1275+
:
1276+
lerp(0.f, LookRightAngle3p, RelativeLookFactor)
1277+
;
1278+
}
12041279

1205-
if (smoothViewLock >= 0.001f && smoothViewLock <= 0.99f && !isMouseLooking()) {
1206-
Vector3f dirCustomCam = finalQuat * front;
1207-
setGameplayCameraDirection(vehSpeed > 10.f ? dirCustomCam : vehForwardVector);
1208-
}*/
1280+
float leftRightRad = leftRightAngle * DEG_TO_RAD;
12091281

1210-
Quaternionf mouseLookRot = lookRotation(getGameplayCameraDirection());
1282+
float roll = 0.f, pitch = 0.f, yaw = leftRightRad;
1283+
Quaternionf qLookLeftRight;
1284+
qLookLeftRight = AngleAxisf(roll, Vector3f::UnitX())
1285+
* AngleAxisf(pitch, Vector3f::UnitY())
1286+
* AngleAxisf(yaw, Vector3f::UnitZ());
1287+
1288+
float btnLookingFactor = (abs(RelativeLookFactor));
1289+
1290+
if (lookBehind)
1291+
btnLookingFactor = 1.f;
12111292

12121293
//finalQuat = slerp(finalQuat, mouseLookRot, max(smoothIsMouseLooking, 1.f - smoothViewLock));
1213-
finalQuat = slerp(finalQuat, mouseLookRot, smoothIsMouseLooking);
1294+
finalQuat = slerp(finalQuat * qLookLeftRight, mouseLookRot, clamp01(smoothIsMouseLooking - btnLookingFactor));
12141295

12151296
if (smoothIsAiming > 0.00001f) {
12161297
float currentFov = lerp(fov3P, fov3PAiming, smoothIsAiming);
@@ -1242,11 +1323,9 @@ void updateCameraSmooth3P() {
12421323

12431324
Vector3f relativeLookDir = back;
12441325

1245-
bool lookBehind = false;
1246-
if (CONTROLS::IS_CONTROL_PRESSED(0, eControl::ControlLookBehind)) {
1326+
if(lookBehind) {
12471327
relativeLookDir = front;
12481328
finalDistMult *= -1.f;
1249-
lookBehind = true;
12501329
}
12511330

12521331
currentTowHeightIncrement = lerp(currentTowHeightIncrement, towHeightIncrement, 1.45f * getDeltaTime());
@@ -1285,15 +1364,19 @@ void updateCameraSmooth3P() {
12851364
angleFront = -angleFront;
12861365
}
12871366

1288-
if (!lookBehind && !isBike)
1367+
if (!lookBehind /* && !isBike */)
12891368
{
12901369
angle = angle * 0.003f;
12911370
angleFront = angleFront * 0.003f;
12921371
//showText(0.01f, 0.200f, 0.4, ("angle: " + std::to_string(angle)).c_str(), 4, solidWhite, true);
12931372
offsetLatPoint = latVector * (angle * (1.f - smoothIsMouseLooking) * (1.f - smoothIsInAir) * (1.f - smoothIsAiming) * clamp01((vehSpeed - 0.002f) * 0.1f) * clamp01(1.f - ((vehSpeed * 0.01f))));
12941373
offsetLatPointFront = latVector * (angleFront * (1.f - smoothIsMouseLooking) * (1.f - smoothIsInAir) * (1.f - smoothIsAiming) * clamp01((vehSpeed - 0.002f) * 0.1f) * clamp01(1.f - ((vehSpeed * 0.01f))));
12951374
}
1296-
setCamPos(customCam, camPos + (offsetLatPointFront * 1.455f));
1375+
1376+
//if (isBike)
1377+
// setCamPos(customCam, camPos);
1378+
//else
1379+
setCamPos(customCam, camPos + (offsetLatPointFront * 1.455f));
12971380

12981381
//if (smoothIsMouseLooking > 0.001f) {
12991382
// Vector3f camPos = lerp(camPos, getGameplayCameraPos(), smoothIsMouseLooking);
@@ -1314,7 +1397,10 @@ void updateCameraSmooth3P() {
13141397
setCamPos(customCam, toV3f(endCoords) + (finalQuat * front * 0.01f));
13151398
}
13161399

1317-
camPointAt(customCam, finalPosCenter + (-up * .170f) + (offsetLatPointFront * -1.4825f));
1400+
//if(isBike)
1401+
// camPointAt(customCam, finalPosCenter + (-up * .170f));
1402+
//else
1403+
camPointAt(customCam, finalPosCenter + (-up * .170f) + (offsetLatPointFront * -1.4825f));
13181404
}
13191405

13201406
void updateCustomCamera()
@@ -1589,7 +1675,7 @@ void updateTimers() {
15891675

15901676
void update()
15911677
{
1592-
if (IsKeyJustUp(str2key("1"))) {
1678+
if (IsKeyJustUp(str2key(toggleModKey))) {
15931679
customCamEnabled = !customCamEnabled;
15941680
}
15951681

@@ -1661,12 +1747,13 @@ void update()
16611747
setupCurrentCamera();
16621748
}
16631749

1664-
if (IsKeyJustUp(str2key("F10"))) {
1750+
if (IsKeyJustUp(str2key(reloadKey))) {
16651751
//showDebug = !showDebug; // TODO Comment this line out before release!
16661752
ReadSettings(true);
16671753
}
16681754

16691755
updateTimers();
1756+
ProccessLookLeftRightOrBackInput();
16701757
updateCustomCamera();
16711758

16721759
Vector3 rotCam = CAM::GET_CAM_ROT(customCam, 2);

0 commit comments

Comments
 (0)