Skip to content

Commit 0f0e517

Browse files
authored
Turn traffic cars back to their road after a hit (#120)
1 parent 655d463 commit 0f0e517

6 files changed

Lines changed: 36 additions & 18 deletions

File tree

‎src/AiTrafficDriver.cpp‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ static const int SpeedFractionSteps = 16; // acceleration is in 1/16 pixel per t
4343
static const int SpeedChangeTicks = 4;
4444
static const int MaxLookAhead = 3; // blocks
4545
static const int BusLookAhead = 8; // pixels, bus tests road further ahead
46-
static const float RestTurnSpeed = 1.0f; // degrees per second, car that turns slower is at rest
4746

4847
static int WrapDirection(int direction)
4948
{
@@ -279,8 +278,7 @@ static const MapBlockInfo* GetGroundBlockAt(const glm::ivec2& position, Vehicle*
279278

280279
void AiTrafficDriver::Start(Vehicle* car)
281280
{
282-
mPosition = MetersToFixed(car->mTransform.GetPosition2());
283-
mHeading = Convert::AngleToFix16(-car->mTransform.mOrientation);
281+
SetPositionFromCar(car);
284282
mFrontDirection = mHeading;
285283
mSteer = 0;
286284
mCompass = GetCompass(mHeading);
@@ -291,27 +289,25 @@ void AiTrafficDriver::Start(Vehicle* car)
291289
mSpeedFraction = 0;
292290
mSpeedTicks = 0;
293291
mLookAhead = 0;
292+
}
293+
294+
void AiTrafficDriver::SetPositionFromCar(Vehicle* car)
295+
{
296+
mPosition = MetersToFixed(car->mTransform.GetPosition2());
297+
mHeading = Convert::AngleToFix16(-car->mTransform.mOrientation);
294298
mPrevPosition = mPosition;
295299
mPrevHeading = mHeading;
296300
mTickTime = 0.0f;
297-
mIsStarted = true;
298301
}
299302

300303
void AiTrafficDriver::UpdateFrame(Vehicle* car)
301304
{
302305
// take over driving when car stops after hit
303306
if (car->IsOutOfControl())
304307
{
305-
mIsStarted = false;
306-
if ((car->GetSpeedPixels() > 0) || (std::fabs(car->mPhysicsBody->GetAngularVelocity().to_degrees()) > RestTurnSpeed))
307-
return;
308-
309-
car->SetOutOfControl(false);
310-
}
311-
312-
if (!mIsStarted)
313-
{
314-
Start(car);
308+
SetPositionFromCar(car);
309+
mSpeed = 0;
310+
return;
315311
}
316312

317313
const float tickTime = 1.0f / GTA_TICKS_PER_SECOND;

‎src/AiTrafficDriver.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class AiTrafficDriver final
1414
void UpdateFrame(Vehicle* car);
1515

1616
private:
17+
// set position and heading from car body
18+
void SetPositionFromCar(Vehicle* car);
19+
1720
// game tick steps
1821
void UpdateTick(Vehicle* car);
1922
void ChooseTurn(Vehicle* car);
@@ -47,6 +50,5 @@ class AiTrafficDriver final
4750
int mSpeed = 0; // pixels per tick
4851
int mSpeedFraction = 0;
4952
int mSpeedTicks = 0;
50-
bool mIsStarted = false;
5153
float mTickTime = 0.0f;
5254
};

‎src/GameParams.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ void GameParams::SetToDefaults()
5757
mCarBurnDuration = 20.0f;
5858
mCarSpeedPassengerCanEnter = 8.0f;
5959
mCarExplosionChainDelayTime = 0.5f;
60-
mCarFallSpeed = 11.67f; // 8 pixels per tick
60+
mCarFallSpeed = Convert::PixelsPerTickToMetersPerSecond(8.0f);
6161
mCarFallStartHeight = Convert::PixelsToMeters(32);
6262
mCarFallLookAheadTime = 0.043f; // 1 tick
6363
mCarWallRestitution = 0.625f;
64-
mCarMinImpactSpeed = 1.46f; // 1 pixel per tick
64+
mCarMinImpactSpeed = Convert::PixelsPerTickToMetersPerSecond(1.0f);
65+
mCarRestSpeed = Convert::PixelsPerTickToMetersPerSecond(0.1f);
6566
mCarSkidMarksMaxCount = 0; // original game has 200
6667
mTurretRotationSpeed = 120.0f;
6768
mCarHeightSlideUnderThreshold = Convert::PixelsToMeters(20);

‎src/GameParams.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class GameParams
6767
float mCarFallLookAheadTime; // ground check looks ahead this time, seconds
6868
float mCarWallRestitution; // part of speed towards wall that vehicle keeps after hit
6969
float mCarMinImpactSpeed; // slower hits do not bounce and do not damage, meters per second
70+
float mCarRestSpeed; // car is at rest when it moves slower than this on each axis, meters per second
7071
int mCarSkidMarksMaxCount; // max skid marks on map, 0 for no limit
7172
// vehicles misc
7273
float mTurretRotationSpeed; // tank/firetruck turret, degrees per second

‎src/Vehicle.cpp‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static const float CarSteeringLockAngle = 0.873f; // radians
2525
static const float CarSteeringCenterAngle = 0.139f; // radians
2626
static const float CarSteeringRecenterSpeed = 3.24f; // radians per second, 0.139 per tick
2727
static const float CarRoadAssistSpeed = 0.65f; // radians per second, 0.0278 per tick
28-
static const float CarRampRiseSpeed = 5.83f; // meters per second, 4 pixels per tick
28+
static const float CarRampRiseSpeed = Convert::PixelsPerTickToMetersPerSecond(4.0f); // meters per second
2929
static const float CarRoadAssistAngle = glm::radians(11.25f);
3030
static const float CarRoadAssistLockAngle = glm::pi<float>() / 1024.0f;
3131
static const int BikeFallenSpriteOffset = 7;
@@ -306,6 +306,7 @@ void Vehicle::SimulationStep()
306306

307307
UpdateMapContactDamage();
308308
UpdateFallDamage();
309+
UpdateOutOfControl();
309310
// ai driver moves car by itself until car is out of control (hit)
310311
if (!IsDrivenByAi())
311312
{
@@ -1574,6 +1575,22 @@ void Vehicle::UpdateMapContactDamage()
15741575
mMapContact = false;
15751576
}
15761577

1578+
void Vehicle::UpdateOutOfControl()
1579+
{
1580+
if (!mIsOutOfControl)
1581+
return;
1582+
1583+
Pedestrian* carDriver = GetCarDriver();
1584+
if (carDriver && carDriver->IsHumanPlayerCharacter())
1585+
return;
1586+
1587+
const glm::vec2 velocity = mPhysicsBody->GetLinearVelocity();
1588+
if ((std::fabs(velocity.x) < gGameParams.mCarRestSpeed) && (std::fabs(velocity.y) < gGameParams.mCarRestSpeed))
1589+
{
1590+
mIsOutOfControl = false;
1591+
}
1592+
}
1593+
15771594
void Vehicle::UpdateFallDamage()
15781595
{
15791596
if (!IsInAir())

‎src/Vehicle.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ class Vehicle final: public GameObject, public SpriteAnimListener
192192
void UpdateDamageFromRailways();
193193
void UpdateMapContactDamage();
194194
void UpdateFallDamage();
195+
void UpdateOutOfControl();
195196

196197
// Bike falls on side and its driver falls off
197198
void KnockOverBike();

0 commit comments

Comments
 (0)