Skip to content

Commit

Permalink
GAME: Use btAlignedObjectArray to store CarDynamics.
Browse files Browse the repository at this point in the history
CarDynamics requires aligned memory allocation, which is not guaranteed by stl containers in general.
  • Loading branch information
logzero committed Oct 16, 2014
1 parent d3a42f6 commit d78b2c2
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/ai/ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ void Ai::ClearCars()
ai_cars.clear();
}

void Ai::Update(float dt, const std::vector<CarDynamics> & checkcars)
void Ai::Update(float dt, const CarDynamics cars[], const int cars_num)
{
int size = ai_cars.size();
for (int i = 0; i < size; i++)
{
ai_cars[i]->Update(dt, checkcars);
ai_cars[i]->Update(dt, cars, cars_num);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ai/ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Ai

void ClearCars();

void Update(float dt, const std::vector<CarDynamics> & checkcars);
void Update(float dt, const CarDynamics cars[], const int cars_num);

///< Returns an empty vector if the car isn't AI-controlled.
const std::vector<float> & GetInputs(const CarDynamics * car) const;
Expand Down
2 changes: 1 addition & 1 deletion src/ai/ai_car.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AiCar

const std::vector<float> & GetInputs() const;

virtual void Update(float dt, const std::vector<CarDynamics> & checkcars) = 0;
virtual void Update(float dt, const CarDynamics cars[], const int cars_num) = 0;

/// This is optional for drawing debug stuff.
/// It will only be called, when VISUALIZE_AI_DEBUG macro is defined.
Expand Down
10 changes: 5 additions & 5 deletions src/ai/ai_car_experimental.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ float AiCarExperimental::RateLimit(float old_value, float new_value, float rate_
return new_value;
}

void AiCarExperimental::Update(float dt, const std::vector<CarDynamics> & checkcars)
void AiCarExperimental::Update(float dt, const CarDynamics cars[], const int cars_num)
{
float lastThrottle = inputs[CarInput::THROTTLE];
float lastBreak = inputs[CarInput::BRAKE];
fill(inputs.begin(), inputs.end(), 0);

AnalyzeOthers(dt, checkcars);
AnalyzeOthers(dt, cars, cars_num);
UpdateGasBrake();
UpdateSteer();
float rateLimit = THROTTLE_RATE_LIMIT * dt;
Expand Down Expand Up @@ -764,14 +764,14 @@ float AiCarExperimental::BrakeFromOthers(float speed_diff)
return bias;
}

void AiCarExperimental::AnalyzeOthers(float dt, const std::vector<CarDynamics> & checkcars)
void AiCarExperimental::AnalyzeOthers(float dt, const CarDynamics cars[], const int cars_num)
{
const float half_carlength = 1.25;
const btVector3 throttle_axis = Direction::forward;

for (std::vector<CarDynamics>::const_iterator i = checkcars.begin(); i != checkcars.end(); ++i)
for (int i = 0; i != cars_num; ++i)
{
const CarDynamics * icar = &*i;
const CarDynamics * icar = &cars[i];
if (icar != car)
{
OtherCarInfo & info = othercars[icar];
Expand Down
4 changes: 2 additions & 2 deletions src/ai/ai_car_experimental.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class AiCarExperimental : public AiCar

~AiCarExperimental();

void Update(float dt, const std::vector<CarDynamics> & checkcars);
void Update(float dt, const CarDynamics cars[], const int cars_num);

#ifdef VISUALIZE_AI_DEBUG
void Visualize();
Expand Down Expand Up @@ -80,7 +80,7 @@ class AiCarExperimental : public AiCar

void UpdateSteer();

void AnalyzeOthers(float dt, const std::vector<CarDynamics> & checkcars);
void AnalyzeOthers(float dt, const CarDynamics cars[], const int cars_num);

///< returns a float that should be added into the steering wheel command
float SteerAwayFromOthers();
Expand Down
10 changes: 5 additions & 5 deletions src/ai/ai_car_standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ float AiCarStandard::RateLimit(float old_value, float new_value, float rate_limi
return new_value;
}

void AiCarStandard::Update(float dt, const std::vector<CarDynamics> & checkcars)
void AiCarStandard::Update(float dt, const CarDynamics cars[], const int cars_num)
{
AnalyzeOthers(dt, checkcars);
AnalyzeOthers(dt, cars, cars_num);
UpdateGasBrake();
UpdateSteer();
}
Expand Down Expand Up @@ -639,14 +639,14 @@ float AiCarStandard::BrakeFromOthers(float speed_diff)
return bias;
}

void AiCarStandard::AnalyzeOthers(float dt, const std::vector<CarDynamics> & checkcars)
void AiCarStandard::AnalyzeOthers(float dt, const CarDynamics cars[], const int cars_num)
{
const float half_carlength = 1.25;
const btVector3 throttle_axis = Direction::forward;

for (std::vector<CarDynamics>::const_iterator i = checkcars.begin(); i != checkcars.end(); ++i)
for (int i = 0; i != cars_num; ++i)
{
const CarDynamics * icar = &*i;
const CarDynamics * icar = &cars[i];
if (icar != car)
{
OtherCarInfo & info = othercars[icar];
Expand Down
4 changes: 2 additions & 2 deletions src/ai/ai_car_standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class AiCarStandard : public AiCar

~AiCarStandard();

void Update(float dt, const std::vector<CarDynamics> & checkcars);
void Update(float dt, const CarDynamics cars[], const int cars_num);

#ifdef VISUALIZE_AI_DEBUG
void Visualize();
Expand Down Expand Up @@ -77,7 +77,7 @@ class AiCarStandard : public AiCar

void UpdateSteer();

void AnalyzeOthers(float dt, const std::vector<CarDynamics> & checkcars);
void AnalyzeOthers(float dt, const CarDynamics cars[], const int cars_num);

///< returns a float that should be added into the steering wheel command
float SteerAwayFromOthers();
Expand Down
30 changes: 16 additions & 14 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ void Game::AdvanceGameLogic()
{
PROFILER.beginBlock("ai");
ai.Visualize();
ai.Update(timestep, car_dynamics);
ai.Update(timestep, &car_dynamics[0], car_dynamics.size());
PROFILER.endBlock("ai");

PROFILER.beginBlock("physics");
Expand Down Expand Up @@ -1034,9 +1034,10 @@ void Game::ProcessGameInputs()
void Game::UpdateTimer()
{
// Check for cars doing a lap.
for (std::vector<CarDynamics>::iterator i = car_dynamics.begin(); i != car_dynamics.end(); ++i)
for (int i = 0; i != car_dynamics.size(); ++i)
{
int carid = cartimerids[&(*i)];
const CarDynamics & car = car_dynamics[i];
int carid = cartimerids[&car];

bool advance = false;
int nextsector = 0;
Expand All @@ -1045,7 +1046,7 @@ void Game::UpdateTimer()
nextsector = (timer.GetLastSector(carid) + 1) % track.GetSectors();
for (int p = 0; p < 4; ++p)
{
const Bezier * patch = i->GetWheelContact(WheelPosition(p)).GetPatch();
const Bezier * patch = car.GetWheelContact(WheelPosition(p)).GetPatch();
if (patch == track.GetSectorPatch(nextsector))
{
advance = true;
Expand All @@ -1058,14 +1059,14 @@ void Game::UpdateTimer()

// Update how far the car is on the track...
// Find the patch under the front left wheel...
const Bezier * curpatch = i->GetWheelContact(FRONT_LEFT).GetPatch();
const Bezier * curpatch = car.GetWheelContact(FRONT_LEFT).GetPatch();
if (!curpatch)
curpatch = i->GetWheelContact(FRONT_RIGHT).GetPatch();
curpatch = car.GetWheelContact(FRONT_RIGHT).GetPatch();

// Only update if car is on track.
if (curpatch)
{
Vec3 pos = ToMathVector<float>(i->GetCenterOfMass());
Vec3 pos = ToMathVector<float>(car.GetCenterOfMass());
Vec3 back_left, back_right, front_left;
if (!track.IsReversed())
{
Expand Down Expand Up @@ -1107,10 +1108,11 @@ void Game::UpdateTimer()
void Game::UpdateTrackMap()
{
std::list <std::pair<Vec3, bool> > carpositions;
for (std::vector<CarDynamics>::iterator i = car_dynamics.begin(); i != car_dynamics.end(); ++i)
for (int i = 0; i != car_dynamics.size(); ++i)
{
bool player = (carcontrols_local.first == &(*i));
Vec3 carpos = ToMathVector<float>(i->GetCenterOfMass());
const CarDynamics & car = car_dynamics[i];
bool player = (carcontrols_local.first == &car);
Vec3 carpos = ToMathVector<float>(car.GetCenterOfMass());
carpositions.push_back(std::make_pair(carpos, player));
}

Expand Down Expand Up @@ -1296,7 +1298,7 @@ void Game::UpdateCarInfo()

void Game::UpdateCars(float dt)
{
for (size_t i = 0; i < car_dynamics.size(); ++i)
for (int i = 0; i < car_dynamics.size(); ++i)
{
UpdateCarInputs(i);

Expand Down Expand Up @@ -1539,7 +1541,7 @@ bool Game::NewGame(bool playreplay, bool addopponents, int num_laps)
}

// Add cars to the timer system.
for (size_t i = 0; i < car_dynamics.size(); ++i)
for (int i = 0; i < car_dynamics.size(); ++i)
{
cartimerids[&car_dynamics[i]] = timer.AddCar(car_info[i].name);
if (carcontrols_local.first == &car_dynamics[i])
Expand Down Expand Up @@ -1673,7 +1675,7 @@ bool Game::LoadCar(
}

car_dynamics.push_back(CarDynamics());
CarDynamics & car = car_dynamics.back();
CarDynamics & car = car_dynamics[car_dynamics.size() - 1];
if (!car.Load(
*carconf, cardir, info.tire,
ToBulletVector(position),
Expand Down Expand Up @@ -1854,7 +1856,7 @@ void Game::SetGarageCar()
{
// update car position
dynamics.update(timestep);
car_graphics.back().Update(car_dynamics.back());
car_graphics.back().Update(car_dynamics[car_dynamics.size() - 1]);
nodes.push_back(&car_graphics.back().GetNode());
}
nodes.push_back(&track.GetTrackNode());
Expand Down
2 changes: 1 addition & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ friend class GameDownloader;

std::pair <CarDynamics *, CarControlMap> carcontrols_local;
std::map <const CarDynamics *, int> cartimerids;
std::vector <CarDynamics> car_dynamics;
btAlignedObjectArray <CarDynamics> car_dynamics;
std::vector <CarGraphics> car_graphics;
std::vector <CarSound> car_sounds;
std::vector <CarInfo> car_info;
Expand Down

0 comments on commit d78b2c2

Please sign in to comment.