Skip to content

Commit

Permalink
[Codechange] Auto transmission now also takes the torqueCurve into ac…
Browse files Browse the repository at this point in the history
…count

 * fixed the curWheelRevolutions calculation (wspeed is in m/s not in rad/s!)
 * improved a few helper functions in the BeamEngine class
 * improved the down shifting behaviour of the auto transmission code
  • Loading branch information
ulteq committed May 26, 2013
1 parent 18674f9 commit 480afed
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 75 deletions.
85 changes: 47 additions & 38 deletions source/main/gameplay/BeamEngine.cpp
Expand Up @@ -24,7 +24,7 @@ along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
#include "SoundScriptManager.h"
#include "TorqueCurve.h"

BeamEngine::BeamEngine(float minRPM, float maxRPM, float torque, std::vector<float> gears, float diff, int trucknum) :
BeamEngine::BeamEngine(float minRPM, float maxRPM, float torque, std::vector<float> gears, float dratio, int trucknum) :
apressure(0.0f)
, autocurAcc(0.0f)
, automode(AUTOMATIC)
Expand All @@ -41,6 +41,7 @@ BeamEngine::BeamEngine(float minRPM, float maxRPM, float torque, std::vector<flo
, curGearRange(0)
, curTurboRPM(0.0f)
, curWheelRevolutions(0.0f)
, diffRatio(dratio)
, engineTorque(torque - brakingTorque)
, gearsRatio(gears)
, hasair(true)
Expand Down Expand Up @@ -71,7 +72,7 @@ BeamEngine::BeamEngine(float minRPM, float maxRPM, float torque, std::vector<flo
gearsRatio[0] = -gearsRatio[0];
for (std::vector< float >::iterator it = gearsRatio.begin(); it != gearsRatio.end(); ++it)
{
(*it) *= diff;
(*it) *= diffRatio;
}
}

Expand Down Expand Up @@ -119,7 +120,7 @@ void BeamEngine::setOptions(float einertia, char etype, float eclutch, float cti
void BeamEngine::update(float dt, int doUpdate)
{
Beam* truck = BeamFactory::getSingleton().getTruck(trucknum);

float acc = curAcc;

acc = std::max(getIdleMixture(), acc);
Expand Down Expand Up @@ -187,7 +188,7 @@ void BeamEngine::update(float dt, int doUpdate)

if (running && contact && curEngineRPM < (maxRPM * 1.25f) && curEngineRPM > stallRPM)
{
totaltorque += getEnginePower() * acc;
totaltorque += getEnginePower(curEngineRPM) * acc;
}

if (running && curEngineRPM < stallRPM)
Expand Down Expand Up @@ -321,7 +322,7 @@ void BeamEngine::update(float dt, int doUpdate)
// 1st gear : special
if (curEngineRPM > minRPM)
{
curClutch = ((curEngineRPM - minRPM) / (maxRPM - minRPM));
curClutch = (curEngineRPM - minRPM) / (maxRPM - minRPM);
curClutch = std::min(curClutch, 1.0f);
} else
{
Expand All @@ -336,17 +337,23 @@ void BeamEngine::update(float dt, int doUpdate)
if (doUpdate && !shifting && !postshifting)
{
// gear hack
if (automode == AUTOMATIC && curGear >= 0 && (autoselect == DRIVE || autoselect == TWO))
int newGear = curGear;

if (automode == AUTOMATIC && curGear > 0 && (autoselect == DRIVE || autoselect == TWO))
{
if ((curEngineRPM > maxRPM - 100.0f && curGear > 1) || curWheelRevolutions * gearsRatio[curGear + 1] > maxRPM - 100.0f)
{
if ((autoselect == DRIVE && curGear < numGears) || (autoselect == TWO && curGear < 2))
{
shift(1);
newGear++;
}
} else if (curGear > 1 && curEngineRPM < minRPM)
{
shift(-1);
newGear--;
}
if (autoselect == TWO)
{
shiftTo(newGear);
}
}

Expand All @@ -362,7 +369,9 @@ void BeamEngine::update(float dt, int doUpdate)
float brake = 0.0f;

if (truck && truck->brakeforce > 0.0f)
{
brake = truck->brake / truck->brakeforce;
}

rpms.push_front(curEngineRPM);
accs.push_front(acc);
Expand All @@ -383,27 +392,28 @@ void BeamEngine::update(float dt, int doUpdate)
avgAcc /= accs.size();
avgBrake /= brakes.size();

int newGear = curGear;

if (avgAcc > 0.8f && curEngineRPM < maxRPM - oneThirdRPMRange)
{
while (newGear > 1 && curWheelRevolutions * gearsRatio[newGear] < maxRPM - oneThirdRPMRange)
while (newGear > 1 && curWheelRevolutions * gearsRatio[newGear] < maxRPM - oneThirdRPMRange &&
getEnginePower(curWheelRevolutions * gearsRatio[newGear]) > getEnginePower(curWheelRevolutions * gearsRatio[newGear+1]))
{
newGear--;
}
} else if (avgAcc > 0.6f && acc < 0.8f && acc > avgAcc + 0.3f && curEngineRPM < minRPM + halfRPMRange)
} else if (avgAcc > 0.6f && acc < 0.8f && acc > avgAcc + 0.1f && curEngineRPM < minRPM + halfRPMRange)
{
if (newGear > 1 && curWheelRevolutions * gearsRatio[newGear] < minRPM + halfRPMRange)
if (newGear > 1 && curWheelRevolutions * gearsRatio[newGear] < minRPM + halfRPMRange &&
getEnginePower(curWheelRevolutions * gearsRatio[newGear]) > getEnginePower(curWheelRevolutions * gearsRatio[newGear+1]))
{
newGear--;
}
} else if (avgAcc > 0.4f && acc < 0.8f && acc > avgAcc + 0.1f && curEngineRPM < minRPM + halfRPMRange)
{
if (newGear > 1 && curWheelRevolutions * gearsRatio[newGear] < minRPM + oneThirdRPMRange)
if (newGear > 1 && curWheelRevolutions * gearsRatio[newGear] < minRPM + oneThirdRPMRange &&
getEnginePower(curWheelRevolutions * gearsRatio[newGear]) > getEnginePower(curWheelRevolutions * gearsRatio[newGear+1]))
{
newGear--;
}
} else if (avgBrake < 0.2f && acc < avgAcc + 0.1f && curEngineRPM > avgRPM - halfRPMRange / 10.0f)
} else if (avgBrake < 0.2f && acc < std::min(avgAcc + 0.1f, 1.0f) && curEngineRPM > avgRPM - halfRPMRange / 10.0f)
{
if (avgAcc < 0.6f && avgAcc > 0.4f && curEngineRPM > minRPM + oneThirdRPMRange && curEngineRPM < maxRPM - oneThirdRPMRange)
{
Expand Down Expand Up @@ -439,30 +449,25 @@ void BeamEngine::update(float dt, int doUpdate)
brakes.pop_back();
}
}
#if 0
// engine speed limiter
if (curEngineRPM > maxRPM + 500.0f) // significantly lowers the top speed of most vehicles
setAcc(0.0f);
}
#endif
// avoid over-revving
if (automode <= SEMIAUTO && curGear != 0)
{
if (std::abs(curWheelRevolutions * gearsRatio[curGear + 1]) > maxRPM * 1.25f)
{
float clutch = 0.0f + 1.0f / (1.0f + std::abs(curWheelRevolutions * gearsRatio[curGear + 1] - maxRPM * 1.25f) / 2.0f);
curClutch = std::min(clutch, curClutch);
}
if (curGear * curWheelRevolutions < -10.0f)

// avoid over-revving
if (automode <= SEMIAUTO && curGear != 0)
{
float clutch = 0.0f + 1.0f / (1.0f + std::abs(-10.0f - curGear * curWheelRevolutions) / 2.0f);
curClutch = std::min(clutch, curClutch);
if (std::abs(curWheelRevolutions * gearsRatio[curGear + 1]) > maxRPM * 1.25f)
{
float clutch = 0.0f + 1.0f / (1.0f + std::abs(curWheelRevolutions * gearsRatio[curGear + 1] - maxRPM * 1.25f) / 2.0f);
curClutch = std::min(clutch, curClutch);
}
if (curGear * curWheelRevolutions < -10.0f)
{
float clutch = 0.0f + 1.0f / (1.0f + std::abs(-10.0f - curGear * curWheelRevolutions) / 2.0f);
curClutch = std::min(clutch, curClutch);
}
}
}
}

// audio stuff
updateAudio(doUpdate);
// audio stuff
updateAudio(doUpdate);
}

void BeamEngine::updateAudio(int doUpdate)
Expand Down Expand Up @@ -858,12 +863,12 @@ void BeamEngine::setManualClutch(float val)
}
}

float BeamEngine::getEnginePower()
float BeamEngine::getEnginePower(float rpm)
{
// engine power with limiter
float tqValue = 1.0f;

float rpmRatio = curEngineRPM / (maxRPM * 1.25f);
float rpmRatio = rpm / (maxRPM * 1.25f);

rpmRatio = std::min(rpmRatio, 1.0f);

Expand All @@ -877,7 +882,11 @@ float BeamEngine::getEnginePower()

float BeamEngine::getAccToHoldRPM(float rpm)
{
return (-brakingTorque * rpm / maxRPM) / getEnginePower();
float rpmRatio = rpm / (maxRPM * 1.25f);

rpmRatio = std::min(rpmRatio, 1.0f);

return (-brakingTorque * rpmRatio) / getEnginePower(curEngineRPM);
}

float BeamEngine::getIdleMixture()
Expand Down
12 changes: 6 additions & 6 deletions source/main/gameplay/BeamEngine.h
Expand Up @@ -28,7 +28,7 @@ class BeamEngine : public ZeroedMemoryAllocator

public:

BeamEngine(float minRPM, float maxRPM, float torque, std::vector<float> gears, float diff, int trucknum);
BeamEngine(float minRPM, float maxRPM, float torque, std::vector<float> gears, float dratio, int trucknum);
~BeamEngine();

float getAcc();
Expand Down Expand Up @@ -72,12 +72,13 @@ class BeamEngine : public ZeroedMemoryAllocator
bool hasTurbo() { return hasturbo; };
bool isRunning() { return running; };
char getType() { return type; };
float getAccToHoldRPM(float rpm);
float getEnginePower(float rpm);
float getEngineTorque() { return engineTorque; };
float getIdleMixture();
float getIdleRPM() { return idleRPM; };
float getMinRPM() { return minRPM; };
float getMaxRPM() { return maxRPM; };
float getAccToHoldRPM(float rpm);
float getIdleMixture();
float getMinRPM() { return minRPM; };
float getPrimeMixture();
int getAutoShift();
size_t getNumGears() { return gearsRatio.size() - 2; };
Expand All @@ -100,8 +101,6 @@ class BeamEngine : public ZeroedMemoryAllocator

protected:

float getEnginePower();

// gear stuff
float curWheelRevolutions;
int curGear;
Expand All @@ -124,6 +123,7 @@ class BeamEngine : public ZeroedMemoryAllocator
float brakingTorque;
float curAcc;
float curEngineRPM;
float diffRatio;
float engineTorque;
float hydropump;
float idleRPM;
Expand Down
1 change: 0 additions & 1 deletion source/main/physics/Beam.cpp
Expand Up @@ -117,7 +117,6 @@ Beam::Beam(int tnum , Ogre::Vector3 pos , Ogre::Quaternion rot , const char* fna
, last_net_time(0)
, lastlastposition(pos)
, lastposition(pos)
, lastwspeed(0.0)
, leftMirrorAngle(0.52)
, lights(1)
, lockSkeletonchange(false)
Expand Down
4 changes: 1 addition & 3 deletions source/main/physics/Beam.h
Expand Up @@ -163,6 +163,7 @@ class Beam :
std::vector< std::vector< int > > nodebeamconnections;
int label;

// wheel speed in m/s
float WheelSpeed;
float getWheelSpeed() { return WheelSpeed; }
Ogre::Vector3 getGForces();
Expand Down Expand Up @@ -339,9 +340,6 @@ class Beam :

Ogre::Real hydrolen;

//number of torque points
// int torquenum;
Ogre::Real lastwspeed;
Ogre::SceneNode *smokeNode;
Ogre::ParticleSystem* smoker;
float stabsleep;
Expand Down
1 change: 0 additions & 1 deletion source/main/physics/BeamData.h
Expand Up @@ -875,7 +875,6 @@ struct rig
int wingstart;

Ogre::String realtruckname;
bool patchEngineTorque;
bool loading_finished;

bool forwardcommands;
Expand Down
33 changes: 14 additions & 19 deletions source/main/physics/BeamForcesEuler.cpp
Expand Up @@ -1500,30 +1500,25 @@ void Beam::calcForcesEuler(int doUpdate, Real dt, int step, int maxstep)
}
}

//LOG("torque "+TOSTRING(torques[0])+" "+TOSTRING(torques[1])+" "+TOSTRING(torques[2])+" "+TOSTRING(torques[3])+" speed "+TOSTRING(newspeeds[0])+" "+TOSTRING(newspeeds[1])+" "+TOSTRING(newspeeds[2])+" "+TOSTRING(newspeeds[3]));
for (int i=0; i<free_wheel; i++) wheels[i].speed=newspeeds[i];
//wheel speed
if (proped_wheels) wspeed/=(float)proped_wheels;
lastwspeed=wspeed;
WheelSpeed=wspeed;

if (patchEngineTorque)
for (int i=0; i<free_wheel; i++)
{
if (engine && free_wheel)
{
engine->setSpin(wspeed * RAD_PER_SEC_TO_RPM);
}
} else
wheels[i].speed = newspeeds[i];
}
if (proped_wheels)
{
if (engine && free_wheel && wheels[0].radius != 0)
{
engine->setSpin(wspeed * RAD_PER_SEC_TO_RPM / wheels[0].radius);
}
wspeed /= (float)proped_wheels;
}

// wheel speed in m/s !
WheelSpeed = wspeed;

if (wheels[0].radius > 0.0f)
{
engine->setSpin(wspeed / (2.0f * Math::PI * wheels[0].radius) * 60);
}

// calculate driven distance
// distance [in meter/second] = speed [radians/second] * (2 * PI * radius [in meter]);
float distance_driven = fabs(((wspeed * RAD_PER_SEC_TO_RPM) * (2.0f * Math::PI * wheels[0].radius) * dt) / 60);
float distance_driven = fabs(wspeed * dt);
odometerTotal += distance_driven;
odometerUser += distance_driven;

Expand Down
7 changes: 0 additions & 7 deletions source/main/physics/input_output/SerializedRig.cpp
Expand Up @@ -197,7 +197,6 @@ SerializedRig::SerializedRig()
wingstart=-1;

this->realtruckname=String();
patchEngineTorque=false;
loading_finished=false;
forwardcommands=false;

Expand Down Expand Up @@ -485,12 +484,6 @@ int SerializedRig::loadTruck(Ogre::String filename, Ogre::SceneNode *parent, Ogr
break;
}

if (c.line == "patchEngineTorque")
{
patchEngineTorque = true;
continue;
}

if (c.line == "end_description" && c.mode == BTS_DESCRIPTION)
{
c.mode = BTS_NONE;
Expand Down

0 comments on commit 480afed

Please sign in to comment.