Dark Bit Factory & Gravity
PROGRAMMING => General coding questions => Topic started by: Pot Noodle on May 04, 2012
-
Hi Guys, Still playing around with this car game ::) I have been sorting out the transmission.
But now that's all good I need a way to convert RPM to a usable floating point value.
The problem is that the RPM are high integers and my road speed is low floats like 0.1+
Can someone show me the light? Please.
Code + Sound Included in Zip.
-
Revolutions Per Minute
One Revolution as an angle is 2.0*pi radians. That's the easy part. The hard part is determining exactly how long a minute is in ticks. If you know that your game renders at 30 frames per second you can take that as a rough estimate of the speed.
Minute = 60*Second/30 (frames)
So RPM = 2.0*pi / (60*Second/30)
You could get this far more accurate but it's alot of work and this estimate should be good enough for your needs.
Here's my c++ for calculating the same (uses win32)
// set up the high resolution timer
if (!QueryPerformanceFrequency((LARGE_INTEGER*)&tick.TicksPerSecond)) {
return Error("MetricStartUp - Failed to obtain the CPU speed!");
}
if (!QueryPerformanceCounter((LARGE_INTEGER*)&tick.FirstTick)) {
return Error("MetricStartUp - Failed to read the high resolution time stamp counter!");
}
tick.OldTick = tick.FirstTick;
tick.CurTick = tick.FirstTick;
tick.NumTicks = tick.CurTick - tick.FirstTick;
tick.TicksPerSecond = max(tick.TicksPerSecond, 1);
// calculate durations and speeds in clock cycles
metric.second = (unsigned __int32)(tick.TicksPerSecond&0x00000000FFFFFFFF);
metric.millisecond = metric.second / 1000;
metric.minute = 60*metric.second;
metric.hour = 60*metric.minute;
metric.rpm = 2*Π/metric.minute; // revolutions per minute
metric.mps = 1.0f/metric.second; // meters per second