Skip to content

Commit

Permalink
Driving a car using a gamepad stick (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Feb 1, 2025
1 parent 76876d0 commit 5de70df
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 126 deletions.
55 changes: 18 additions & 37 deletions src/xrGame/Car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1084,39 +1084,20 @@ void CCar::UpdatePower()
i->UpdatePower();
}

void CCar::SteerRight()
void CCar::Steer(float angle)
{
b_wheels_limited = true; // no need to limit wheels when stiring
m_pPhysicsShell->Enable();
xr_vector<SWheelSteer>::iterator i, e;
i = m_steering_wheels.begin();
e = m_steering_wheels.end();
for (; i != e; ++i)
i->SteerRight();
e_state_steer = right;
}
void CCar::SteerLeft()
{
b_wheels_limited = true; // no need to limit wheels when stiring
m_pPhysicsShell->Enable();
xr_vector<SWheelSteer>::iterator i, e;
i = m_steering_wheels.begin();
e = m_steering_wheels.end();
for (; i != e; ++i)
i->SteerLeft();
e_state_steer = left;
}

void CCar::SteerIdle()
{
b_wheels_limited = false;
m_pPhysicsShell->Enable();
xr_vector<SWheelSteer>::iterator i, e;
i = m_steering_wheels.begin();
e = m_steering_wheels.end();
for (; i != e; ++i)
i->SteerIdle();
e_state_steer = idle;
for (auto& steering : m_steering_wheels)
steering.Steer(angle);

if (fis_zero(angle))
e_state_steer = idle;
else if (angle > 0)
e_state_steer = right;
else
e_state_steer = left;
}

void CCar::LimitWheels()
Expand Down Expand Up @@ -1163,21 +1144,21 @@ void CCar::PressRight()
if (lsp)
{
if (!fwp)
SteerIdle();
Steer(0.0f);
}
else
SteerRight();
Steer(1.0f);
rsp = true;
}
void CCar::PressLeft()
{
if (rsp)
{
if (!fwp)
SteerIdle();
Steer(0.0f);
}
else
SteerLeft();
Steer(-1.0f);
lsp = true;
}
void CCar::PressForward()
Expand Down Expand Up @@ -1235,17 +1216,17 @@ void CCar::DriveForward()
void CCar::ReleaseRight()
{
if (lsp)
SteerLeft();
Steer(-1.0f);
else
SteerIdle();
Steer(0.0f);
rsp = false;
}
void CCar::ReleaseLeft()
{
if (rsp)
SteerRight();
Steer(1.0f);
else
SteerIdle();
Steer(0.0f);
lsp = false;
}
void CCar::ReleaseForward()
Expand Down
8 changes: 2 additions & 6 deletions src/xrGame/Car.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ class CCar : public CEntity,
float GetSteerAngle();

void Init();
void SteerRight();
void SteerLeft();
void SteerIdle();
void Steer(float angle);
void Limit();
void Load(LPCSTR /*section*/){};
};
Expand Down Expand Up @@ -465,9 +463,7 @@ class CCar : public CEntity,
float DriveWheelsMeanAngleRate();
IC float EngineRpmFromWheels() { return _abs(DriveWheelsMeanAngleRate() * m_current_gear_ratio); }
/////////////////////////////////////////////////////////////////////////
void SteerRight();
void SteerLeft();
void SteerIdle();
void Steer(float angle);
void Transmission(size_t num);
void CircleSwitchTransmission();
void TransmissionUp();
Expand Down
70 changes: 29 additions & 41 deletions src/xrGame/CarInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,11 @@ void CCar::OnControllerPress(int cmd, const ControllerAxisState& state)

case kMOVE_AROUND:
{
if (!fis_zero(state.x))
if (std::abs(state.x) > 0.35f)
{
if (state.x > 0.35f)
OnKeyboardPress(kR_STRAFE);
else if (state.x < -0.35f)
OnKeyboardPress(kL_STRAFE);
Steer(state.x);
if (OwnerActor())
OwnerActor()->steer_Vehicle(state.x);
}
if (!fis_zero(state.y))
{
Expand All @@ -265,7 +264,7 @@ void CCar::OnControllerPress(int cmd, const ControllerAxisState& state)
default:
OnKeyboardPress(cmd);
break;
};
}
}

void CCar::OnControllerRelease(int cmd, const ControllerAxisState& state)
Expand All @@ -279,16 +278,17 @@ void CCar::OnControllerRelease(int cmd, const ControllerAxisState& state)
break;

case kMOVE_AROUND:
OnKeyboardRelease(kFWD);
OnKeyboardRelease(kBACK);
OnKeyboardRelease(kL_STRAFE);
OnKeyboardRelease(kR_STRAFE);
Steer(0.0f);
if (fwp)
OnKeyboardRelease(kFWD);
if (bkp)
OnKeyboardRelease(kBACK);
break;

default:
OnKeyboardPress(cmd);
break;
};
}
}

void CCar::OnControllerHold(int cmd, const ControllerAxisState& state)
Expand All @@ -309,45 +309,33 @@ void CCar::OnControllerHold(int cmd, const ControllerAxisState& state)

case kMOVE_AROUND:
{
if (!fis_zero(state.x))
const float angle = std::abs(state.x) > 0.35f ? state.x : 0.0f;

Steer(angle);
if (OwnerActor())
OwnerActor()->steer_Vehicle(angle);

if (std::abs(state.y) > 0.35f)
{
if (state.x > 0.35f && !rsp) // right
if (state.y < 0.f) // forward
{
OnKeyboardRelease(kL_STRAFE);
OnKeyboardPress(kR_STRAFE);
}
else if (state.x < -0.35f && !lsp) // left
{
OnKeyboardRelease(kR_STRAFE);
OnKeyboardPress(kL_STRAFE);
if (bkp)
OnKeyboardRelease(kBACK);
OnKeyboardPress(kFWD);
}
else
else // state.y > 0.f backward
{
if (lsp)
OnKeyboardRelease(kL_STRAFE);
if (rsp)
OnKeyboardRelease(kR_STRAFE);
if (fwp)
OnKeyboardRelease(kFWD);
OnKeyboardPress(kBACK);
}
}
if (!fis_zero(state.y))
else
{
if (state.y > 0.35f && !bkp) // backward
{
if (fwp)
OnKeyboardRelease(kFWD);
OnKeyboardPress(kBACK);
}
else if (state.y < -0.35f && !fwp) // forward
{
if (bkp)
OnKeyboardRelease(kBACK);
OnKeyboardPress(kFWD);
}
else
{
if (fwp)
OnKeyboardRelease(kFWD);
if (bkp)
OnKeyboardRelease(kBACK);
}
}
break;
}
Expand Down
90 changes: 48 additions & 42 deletions src/xrGame/CarWheels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,61 +271,67 @@ void CCar::SWheelSteer::Init()
limited = false;
}

void CCar::SWheelSteer::SteerRight()
void CCar::SWheelSteer::Steer(float angle)
{
limited = true; // no need to limit wheels when steering
if (pos_right > 0)
{
pwheel->SetSteerHiLimit(hi_limit);
pwheel->ApplySteerAxisVel(pwheel->car->m_steering_speed);
}
else
{
pwheel->SetSteerLoLimit(lo_limit);
pwheel->ApplySteerAxisVel(-pwheel->car->m_steering_speed);
}
}
void CCar::SWheelSteer::SteerLeft()
{
limited = true; // no need to limit wheels when steering
if (pos_right < 0)
{
pwheel->SetSteerHiLimit(hi_limit);
pwheel->ApplySteerAxisVel(pwheel->car->m_steering_speed);
}
else
{
pwheel->SetSteerLoLimit(lo_limit);
pwheel->ApplySteerAxisVel(-pwheel->car->m_steering_speed);
}
}
void CCar::SWheelSteer::SteerIdle()
{
limited = false;
if (pwheel->car->e_state_steer == right)
const bool zeroAngle = fis_zero(angle);
limited = zeroAngle;

if (zeroAngle)
{
if (pos_right < 0)
if (pwheel->car->e_state_steer == right)
{
pwheel->SetSteerHiLimit(0.f);
pwheel->ApplySteerAxisVel(pwheel->car->m_steering_speed);
if (pos_right < 0)
{
pwheel->SetSteerHiLimit(0.f);
pwheel->ApplySteerAxisVel(pwheel->car->m_steering_speed);
}
else
{
pwheel->SetSteerLoLimit(0.f);
pwheel->ApplySteerAxisVel(-pwheel->car->m_steering_speed);
}
}
else
{
pwheel->SetSteerLoLimit(0.f);
pwheel->ApplySteerAxisVel(-pwheel->car->m_steering_speed);
if (pos_right > 0)
{
pwheel->SetSteerHiLimit(0.f);
pwheel->ApplySteerAxisVel(pwheel->car->m_steering_speed);
}
else
{
pwheel->SetSteerLoLimit(0.f);
pwheel->ApplySteerAxisVel(-pwheel->car->m_steering_speed);
}
}
}
else
{
if (pos_right > 0)
if (angle > 0.0f)
{
pwheel->SetSteerHiLimit(0.f);
pwheel->ApplySteerAxisVel(pwheel->car->m_steering_speed);
if (pos_right > 0)
{
pwheel->SetSteerHiLimit(hi_limit * std::abs(angle));
pwheel->ApplySteerAxisVel(pwheel->car->m_steering_speed);
}
else
{
pwheel->SetSteerLoLimit(lo_limit * std::abs(angle));
pwheel->ApplySteerAxisVel(-pwheel->car->m_steering_speed);
}
}
else
else // angle < 0.0f
{
pwheel->SetSteerLoLimit(0.f);
pwheel->ApplySteerAxisVel(-pwheel->car->m_steering_speed);
if (pos_right < 0)
{
pwheel->SetSteerHiLimit(hi_limit * std::abs(angle));
pwheel->ApplySteerAxisVel(pwheel->car->m_steering_speed);
}
else
{
pwheel->SetSteerLoLimit(lo_limit * std::abs(angle));
pwheel->ApplySteerAxisVel(-pwheel->car->m_steering_speed);
}
}
}
}
Expand Down

0 comments on commit 5de70df

Please sign in to comment.