Skip to content
This repository has been archived by the owner on Aug 17, 2024. It is now read-only.

Commit

Permalink
Fix adaptive cruise control
Browse files Browse the repository at this point in the history
  • Loading branch information
ikt32 committed Aug 1, 2023
1 parent 5bb9db6 commit 9618fca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions Gears/CruiseControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bool CruiseControl::GetAdaptiveActive() {
return adaptiveActive;
}

SRayResult RayCast(Vehicle vehicle, float range, Vector3 startOffset, Vector3 endOffset) {
SRayResult RayCast(Vehicle vehicle, Vector3 startOffset, Vector3 endOffset) {
auto rayOrg = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(vehicle, startOffset);
auto rayEnd = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(vehicle, endOffset);

Expand All @@ -62,6 +62,19 @@ SRayResult RayCast(Vehicle vehicle, float range, Vector3 startOffset, Vector3 en
int rayResult = SHAPETEST::GET_SHAPE_TEST_RESULT(ray, &hit, &hitCoord, &surfaceNormal, &hitEntity);
float distance = Distance(rayOrg, hitCoord);

if (g_settings.Debug.DisplayInfo) {
GRAPHICS::DRAW_LINE(rayOrg,
rayEnd,
hit ? 255 : 71,
hit ? 255 : 71,
hit ? 255 : 71,
255);

if (hit) {
UI::DrawSphere(hitCoord, 0.10f, Util::ColorsI::SolidWhite);
}
}

return {
hit, hitCoord, hitEntity, distance
};
Expand Down Expand Up @@ -144,7 +157,8 @@ SRayResult MultiCast(Vehicle vehicle, float range) {
for (const auto& offset : offsets) {
Vector3 offVel = offset;
offVel.x += vecNextRot.x;
auto result = RayCast(g_playerVehicle, 120.0f, offset, offVel);
offVel.y += 120.0f;
auto result = RayCast(g_playerVehicle, offset, offVel);

if (result.Hit &&
(minResult.HitEntity == 0 || result.Distance < minResult.Distance)) {
Expand Down Expand Up @@ -262,6 +276,13 @@ void UpdateAdaptive(float& targetSetpoint, float& brakeValue) {
else if (Math::Near(distMin, distMax, 0.1f)) {
targetSetpoint = otherSpeed;
}

if (g_settings.Debug.DisplayInfo) {
auto entityCoords = ENTITY::GET_ENTITY_COORDS(hitEntity, true);
entityCoords.z += 2.0f;
UI::ShowText3D(entityCoords, { "Adaptive CC Target", std::format("Distance: {:.0f}", result.Distance) });
UI::DrawSphere(entityCoords, 0.25f, Util::ColorsI::SolidWhite);
}
}
else {
adaptiveActive = false;
Expand Down
2 changes: 1 addition & 1 deletion Gears/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ void update_manual_transmission() {
}

if (g_settings().DriveAssists.CruiseControl.Enable) {
if (g_controls.ButtonJustPressed(CarControls::KeyboardControlType::ToggleCC) ||
if (g_controls.ButtonJustPressed(CarControls::KeyboardControlType::ToggleCC) ||
g_controls.ButtonJustPressed(CarControls::WheelControlType::ToggleCC) ||
g_controls.ButtonHeld(CarControls::ControllerControlType::ToggleCC) ||
g_controls.PrevInput == CarControls::Controller && g_controls.ButtonHeld(CarControls::LegacyControlType::ToggleCC)) {
Expand Down

0 comments on commit 9618fca

Please sign in to comment.