Jump to content

  • Log In with Google      Sign In   
  • Create Account

#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