• Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

We're also offering banner ads on our site from just \$5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.

### #Actualmr_malee

Posted 25 September 2013 - 01:35 PM

I ended up with something like this:

//find the current gear ratio
float currentRatio = gearRatios[currentGear];

if (currentRatio > 0) {

float bestDist = float.PositiveInfinity;
int bestGear = currentGear;

int i, len = gears.Length;

//start at index 2 (gear 1)
//index 0 = reverse
//index 1 = neutral

for (i = 2; i < len; i++) {

//find the gear ratio and the ratio to move into this gear from the current
float gearRatio = gearRatios[i];
float moveRatio = gearRatio / currentRatio;

//find Optimal rpm. These are pre-calculated for each gear. They are generally around the redline mark (8000rpm for my car)
float rpmOptimal = optimalRPM[i];

//find the next rpm which the engine would produce if moving into this gear
float rpmNext = moveRatio * currentRPM;

//find distance between optimal and next
float rpmDist = rpmOptimal - rpmNext;

//if the next rpm is < maxRPM, the distance is above 0 (hasn't crossed optimal) and is < the current best, then this is a contender for the best gear

if (rpmNext < maxRPM && rpmDist >= 0 && rpmDist < bestDist) {

bestDist = rpmDist;
bestGear = i;
}
}
}


So far it's working nicely.

### #1mr_malee

Posted 25 September 2013 - 01:33 PM

I ended up with something like this:

//find the current gear ratio
float currentRatio = gearRatios[currentGear];

if (currentRatio > 0) {

float bestDist = float.PositiveInfinity;
int bestGear = currentGear;

int i, len = gears.Length;

//start at index 2 (gear 1)
//index 0 = reverse
//index 1 = neutral

for (i = 2; i < len; i++) {

//find the gear ratio and the ratio to move into this gear from the current
float gearRatio = gearRatios[i];
float moveRatio = gearRatio / currentRatio;

//find Optimal rpm. These are pre-calculated for each gear. They are generally around the redline mark
float rpmOptimal = optimalRPM[i];

//find the next rpm which the engine would produce if moving into this gear
float rpmNext = moveRatio * currentRPM;

//find distance between optimal and next
float rpmDist = rpmOptimal - rpmNext;

//if the next rpm is < maxRPM, the distance is above 0 (hasn't crossed optimal) and is < the current best, then this is a contender for the best gear

if (rpmNext < maxRPM && rpmDist >= 0 && rpmDist < bestDist) {

bestDist = rpmDist;
bestGear = i;
}
}
}


So far it's working nicely.

PARTNERS