From e5d4628ccd381c0e2b3ad501cd987afd3e5dcea5 Mon Sep 17 00:00:00 2001
From: logzero
Date: Thu, 14 Jul 2011 13:53:46 +0200
Subject: [PATCH 2/2] Car forward acceleration, velocity, distance logging.
---
include/car.h | 11 +++++-
include/cardynamics.h | 7 +--
src/cardynamics.cpp | 24 ++++--------
src/game.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 124 insertions(+), 21 deletions(-)
diff --git a/include/car.h b/include/car.h
index 8bf2419..ee3020d 100644
--- a/include/car.h
+++ b/include/car.h
@@ -77,7 +77,11 @@ public:
void GetEngineSoundList(std::list & outputlist);
- // interpolated
+ const CARDYNAMICS& GetDynamics() const
+ {
+ return dynamics;
+ }
+
const MATHVECTOR GetWheelPosition(const WHEEL_POSITION wpos) const
{
return ToMathVector(dynamics.GetWheelPosition(wpos));
@@ -209,6 +213,11 @@ public:
dynamics.DebugPrint(out, p1, p2, p3, p4);
}
+ void SetSimulationStepCB(void (*cb)(const CARDYNAMICS&))
+ {
+ dynamics.SetStepCB(cb);
+ }
+
bool Serialize(joeserialize::Serializer & s);
/// AI interface
diff --git a/include/cardynamics.h b/include/cardynamics.h
index b348cc7..4e7755a 100644
--- a/include/cardynamics.h
+++ b/include/cardynamics.h
@@ -134,11 +134,11 @@ public:
btScalar GetFeedback() const;
- void UpdateTelemetry(btScalar dt);
-
// print debug info to the given ostream. set p1, p2, etc if debug info part 1, and/or part 2, etc is desired
void DebugPrint(std::ostream & out, bool p1, bool p2, bool p3, bool p4) const;
+ void SetStepCB(void (*simcb)(const CARDYNAMICS& cd));
+
bool Serialize(joeserialize::Serializer & s);
protected:
@@ -194,10 +194,9 @@ protected:
btAlignedObjectArray wheel_orientation;
btAlignedObjectArray wheel_contact;
std::vector > suspension;
-
btAlignedObjectArray aerodynamics;
- std::list telemetry;
+ void (*step_callback)(const CARDYNAMICS&);
btScalar maxangle;
btScalar feedback;
bool damage;
diff --git a/src/cardynamics.cpp b/src/cardynamics.cpp
index 5589c5b..0a176eb 100644
--- a/src/cardynamics.cpp
+++ b/src/cardynamics.cpp
@@ -377,6 +377,7 @@ CARDYNAMICS::CARDYNAMICS() :
remaining_shift_time(0),
abs(false),
tcs(false),
+ step_callback(0),
maxangle(0),
damage(false)
{
@@ -635,12 +636,6 @@ bool CARDYNAMICS::Load(
// place car on track
AlignWithGround();
- // initialize telemetry
- telemetry.clear();
- //telemetry.push_back(CARTELEMETRY("brakes"));
- //telemetry.push_back(CARTELEMETRY("suspension"));
- //etc
-
return true;
}
@@ -981,14 +976,6 @@ btScalar CARDYNAMICS::GetFeedback() const
return feedback;
}
-void CARDYNAMICS::UpdateTelemetry(btScalar dt)
-{
- for (std::list::iterator i = telemetry.begin(); i != telemetry.end(); ++i)
- {
- i->Update(dt);
- }
-}
-
std::ostream & operator << (std::ostream & os, const btVector3 & v)
{
os << v[0] << ", " << v[1] << ", " << v[2];
@@ -1109,6 +1096,11 @@ void CARDYNAMICS::DebugPrint ( std::ostream & out, bool p1, bool p2, bool p3, bo
}
}
+void CARDYNAMICS::SetStepCB(void (*cb)(const CARDYNAMICS& d))
+{
+ step_callback = cb;
+}
+
static bool serialize(joeserialize::Serializer & s, btQuaternion & q)
{
_SERIALIZE_(s, q[0]);
@@ -1630,10 +1622,10 @@ void CARDYNAMICS::updateAction(btCollisionWorld * collisionWorld, btScalar dt)
const float tacho_factor = 0.1;
tacho_rpm = engine.GetRPM() * tacho_factor + tacho_rpm * (1.0 - tacho_factor);
- UpdateTelemetry(dt);
-
linear_velocity = body->getLinearVelocity();
angular_velocity = body->getAngularVelocity();
+
+ if (step_callback) step_callback(*this);
}
void CARDYNAMICS::UpdateWheelContacts()
diff --git a/src/game.cpp b/src/game.cpp
index fed86c3..338a9f6 100755
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -62,6 +62,91 @@
#define USE_STATIC_OPTIMIZATION_FOR_TRACK
+// logging hack
+class MotionStateLogger
+{
+public:
+ MotionStateLogger() :
+ _ticks(0),
+ _ticks_acc(0),
+ _distance(0),
+ _velocity(0)
+ {
+ // ctor
+ }
+
+ void init()
+ {
+ if (_out.good()) _out.close();
+ _out.open("log.txt");
+ _out << "Time,Acc,Vel,Dist\n";
+ _out << std::fixed << std::setprecision(4);
+ _ticks = 0;
+ _ticks_acc = 0;
+ _distance = 0;
+ _velocity = 0;
+ }
+
+ void step(const CARDYNAMICS& dynamics)
+ {
+ ++_ticks_acc;
+ if (_ticks_acc == _samplerate)
+ {
+ btQuaternion rot = dynamics.GetOrientation().inverse();
+ btVector3 velocity = quatRotate(rot, dynamics.GetVelocity());
+ btScalar dv = velocity[1] - _velocity;
+ _velocity = velocity[1];
+ _distance += _velocity / _freq;
+ _ticks += _ticks_acc;
+ _ticks_acc = 0;
+ _out << _ticks * _timestep << "," << dv * _freq << "," << _velocity << "," << _distance << "\n";
+ }
+ }
+
+ static void setSamplingFrequency(float freq, float timestep)
+ {
+ if (freq < 1 || freq > 1 / timestep) freq = 1 / timestep;
+ float samplerate = 1.0 / (timestep * freq);
+ _samplerate = samplerate;
+ _timestep = timestep;
+ _freq = 1.0 / (_timestep * (float)_samplerate);
+ _enable = true;
+ }
+
+ static void initDefaultInstance()
+ {
+ _default_logger.init();
+ }
+
+ static void stepDefaultInstance(const CARDYNAMICS& dynamics)
+ {
+ _default_logger.step(dynamics);
+ }
+
+ static bool isEnabled()
+ {
+ return _enable;
+ }
+
+private:
+ static MotionStateLogger _default_logger;
+ static unsigned _samplerate;
+ static float _timestep;
+ static float _freq;
+ static bool _enable;
+ std::ofstream _out;
+ unsigned long _ticks;
+ unsigned _ticks_acc;
+ btScalar _distance;
+ btScalar _velocity;
+};
+
+MotionStateLogger MotionStateLogger::_default_logger;
+unsigned MotionStateLogger::_samplerate(9);
+float MotionStateLogger::_timestep(1/90.0);
+float MotionStateLogger::_freq(1/10.0);
+bool MotionStateLogger::_enable(false);
+
template
static std::string toString (const T &t) {
std::ostringstream os;
@@ -572,6 +657,14 @@ bool GAME::ParseArguments(std::list & args)
renderconfigfile = argmap["-render"];
}
+ arghelp["-motion-log FREQUENCY"] = "Log player car frame forward acceleration, velocity and distance.";
+ if (!argmap["-motion-log"].empty())
+ {
+ unsigned frequency;
+ std::stringstream s(argmap["-motion-log"]);
+ s >> frequency;
+ MotionStateLogger::setSamplingFrequency(frequency, TickPeriod());
+ }
arghelp["-help"] = "Display command-line help.";
if (argmap.find("-help") != argmap.end() || argmap.find("-h") != argmap.end() || argmap.find("--help") != argmap.end() || argmap.find("-?") != argmap.end())
@@ -808,6 +901,9 @@ void GAME::AdvanceGameLogic()
collision.update(TickPeriod());
PROFILER.endBlock("physics");
+ // logging hack
+ MotionStateLogger::stepDefaultInstance(cars.begin()->GetDynamics());
+
PROFILER.beginBlock("car-update");
for (std::list ::iterator i = cars.begin(); i != cars.end(); ++i)
{
@@ -1777,6 +1873,13 @@ bool GAME::NewGame(bool playreplay, bool addopponents, int num_laps)
return false;
}
+ // logger hack
+ if (MotionStateLogger::isEnabled())
+ {
+ MotionStateLogger::initDefaultInstance();
+ cars.begin()->SetSimulationStepCB(&MotionStateLogger::stepDefaultInstance);
+ }
+
// Load AI cars.
if (addopponents)
{
--
1.7.4.msysgit.0