- Messages
- 240
- Country

This tutorial will discuss a way to create non-linearity tables, to be used in any situation when we need some value to be derived from another variable, and can be expressed with a table.
This approach could be used in place of SDK non-linearity tables, but if you work entirely in GDI and can't use them, a custom approach is needed. The advantage of using a C++ class, is that is entirely generic, and you can use it everywhere in your code, to simulate any kind of relationship between variables tied to each other, not just graphical objects.
In this example, we are going to simulate a fake "vibration" simulation, tied to the engine RPM. Here's the link to the Source code:
http://www.mediafire.com/download/g93gb616i6pveb8/Interpolator_sample.zip
It includes code from the previous CNamedVar sample, and add some bits.
The new files added to the project are:
interpolater.h
interpolater.cpp
They are the implementation of the InterpTable class. You can have a look at those files to understand how the class is made.
Using the class is very simple, you first declare an object variable of the InterpTable type:
Then, somewhere at your code startup, you fill up the objects with entries that make up the table, like this:
This table has 4 entries and it will work in a way that, when supplied with a number from 0 to 60, the result will be linearly interpolated from 0 to 1.5, then from 1.5 to 4.0 with a value between 60 and 80, and from 4.0 to 4.5 if the value goes from 80 to 100.
If you supply with a value less than the minimum ( less than 0 ), it will return 0, and if you supply it with more than 100, it will return 4.5.
To "use" the object, just call the interpolate member function over it, passing a value, like this:
In this case, since we passed 65 to it, we'll receive a value slightly higher than 1.5, according to the table of values created before.
Let's see how we used it in the tutorial. The declaration will happen, as in the previous examples, in the constructor of the CSimulation class in the CSimulation.h header file. This means our table will be inside the CSimulation Singleton, together with the other variables, user or sim based.
So, we added this code to the CSimulation constructor:
We also added a new CFSVar, to read the engine max percent rpm from FSX, which is named EnginePctRPM.
Next, we fill the table with values, in the CSimulation class constructor BODY, the one that starts after the curly brackets:
In the CSimulation.cpp file, we add the update for the engine pct rpm variable in the CSimulation::Update() function, the one that updates at 6hz should be fine for this application.
Finally, we go into the actual gauge that "draws" stuff on screen, the GDIPlus_Gauge.cpp file, in the PANEL_SERVICE_PRE_DRAW cycle of the callback routine:
The last two lines have been added from the previous example and, for clarity, we first print the original value of the EnginePctRPM variable, using Get(1) instead of Update(1) since we already updated it at 6hz and don't need to update it again at the fps rate, and THEN we print the derived value, calling the interpolate member function over the tab_vc_vib_rpm object, passing the EnginePctRPM in order to calculate our fictional "vibration" simulation, derived from the engine RPM.
In this case, we'll obtain a value ranging from 0 to 1.2, depending on the actual max pct rpm of the engine, that will be interpolated according to our table. If you compile the sample, you'll see this is exactly.
Of course, you can easily use those tables to calculate values to drive animations. You could tie an animation, for example for a 3d needle of a 3d VC gauge, to an L: variable, and set it using this method to calculate an angle to rotate the needle, based on some other variable, so you don't need any XML code to handle non-linearity, your needle will have the usual "dumb" code definition in the modeldef.xml file, that won't do anything other than linking the animation to the L: variable, and all the work to decide how the needle is supposed to be rotated, will be in your C++ code.
But the approach is really generic: you can use it to simulate new systems. For example, in the MS F/A-18, I used a couple of tables (in addition to all 3d needles having non-linearity tables made this way) to derive the engine nozzle position based on the engine rpm, with two different tables used, depending if afterburners were active or not.
This approach could be used in place of SDK non-linearity tables, but if you work entirely in GDI and can't use them, a custom approach is needed. The advantage of using a C++ class, is that is entirely generic, and you can use it everywhere in your code, to simulate any kind of relationship between variables tied to each other, not just graphical objects.
In this example, we are going to simulate a fake "vibration" simulation, tied to the engine RPM. Here's the link to the Source code:
http://www.mediafire.com/download/g93gb616i6pveb8/Interpolator_sample.zip
It includes code from the previous CNamedVar sample, and add some bits.
The new files added to the project are:
interpolater.h
interpolater.cpp
They are the implementation of the InterpTable class. You can have a look at those files to understand how the class is made.
Using the class is very simple, you first declare an object variable of the InterpTable type:
Code:
InterpTable MyTable;
Then, somewhere at your code startup, you fill up the objects with entries that make up the table, like this:
Code:
MyTable.addEntry( 0.0, 0.0 );
MyTable.addEntry( 60.0, 1.5 );
MyTable.addEntry( 80.0, 4.0 );
MyTable.addEntry( 100.0, 4.5 );
This table has 4 entries and it will work in a way that, when supplied with a number from 0 to 60, the result will be linearly interpolated from 0 to 1.5, then from 1.5 to 4.0 with a value between 60 and 80, and from 4.0 to 4.5 if the value goes from 80 to 100.
If you supply with a value less than the minimum ( less than 0 ), it will return 0, and if you supply it with more than 100, it will return 4.5.
To "use" the object, just call the interpolate member function over it, passing a value, like this:
Code:
double MyDerivedValue = MyTable.interpolate( 65.0 );
In this case, since we passed 65 to it, we'll receive a value slightly higher than 1.5, according to the table of values created before.
Let's see how we used it in the tutorial. The declaration will happen, as in the previous examples, in the constructor of the CSimulation class in the CSimulation.h header file. This means our table will be inside the CSimulation Singleton, together with the other variables, user or sim based.
So, we added this code to the CSimulation constructor:
Code:
CMultipleVar<SIMVAR_GENERAL_ENG_PCT_MAX_RPM, Units::percent, 1> EnginePctRPM;
// Declare an Interpolation (non-linearity) table to simulate something based on engine rpm
InterpTable tab_vc_vib_rpm;
We also added a new CFSVar, to read the engine max percent rpm from FSX, which is named EnginePctRPM.
Next, we fill the table with values, in the CSimulation class constructor BODY, the one that starts after the curly brackets:
Code:
CSimulation()
: mStarted(false),
MyVar1("MyVar1", 4, 2, 10, true),
MyVar2("MyVar2", 3, 1, 9, true)
{
// Fill the interpolation table with values
tab_vc_vib_rpm.addEntry(60.0, 0.0);
tab_vc_vib_rpm.addEntry(70.0, 0.3);
tab_vc_vib_rpm.addEntry(80.0, 0.6);
tab_vc_vib_rpm.addEntry(90.0, 0.8);
tab_vc_vib_rpm.addEntry(100.0, 1.2);
}
In the CSimulation.cpp file, we add the update for the engine pct rpm variable in the CSimulation::Update() function, the one that updates at 6hz should be fine for this application.
Code:
void CSimulation::Update6hz(void)
{
PlaneLatitude.Update();
PlaneLongitude.Update();
PlaneAltitude.Update();
Throttle.Update(1);
EnginePctRPM.Update(1);
}
Finally, we go into the actual gauge that "draws" stuff on screen, the GDIPlus_Gauge.cpp file, in the PANEL_SERVICE_PRE_DRAW cycle of the callback routine:
Code:
StringCbPrintf(mpszDest, mcbDest, L"LAT: %6.3f°\nLON: %6.3f°\nALT: %8.0f ft.\n\nHeading: %6.3f°\nMyVar1: %.2f\nMyVar2: %.2f\nENG PCT MAX: %.2f\nENG VIB: %.2f",
Sim->PlaneLatitude.Get(),
Sim->PlaneLongitude.Get(),
Sim->PlaneAltitude.Get(),
Sim->Heading.Get(),
Sim->MyVar1.Get(),
Sim->MyVar2.Get(),
Sim->EnginePctRPM.Get( 1 ),
Sim->tab_vc_vib_rpm.interpolate( Sim->EnginePctRPM.Get( 1 ) )
);
The last two lines have been added from the previous example and, for clarity, we first print the original value of the EnginePctRPM variable, using Get(1) instead of Update(1) since we already updated it at 6hz and don't need to update it again at the fps rate, and THEN we print the derived value, calling the interpolate member function over the tab_vc_vib_rpm object, passing the EnginePctRPM in order to calculate our fictional "vibration" simulation, derived from the engine RPM.
In this case, we'll obtain a value ranging from 0 to 1.2, depending on the actual max pct rpm of the engine, that will be interpolated according to our table. If you compile the sample, you'll see this is exactly.
Of course, you can easily use those tables to calculate values to drive animations. You could tie an animation, for example for a 3d needle of a 3d VC gauge, to an L: variable, and set it using this method to calculate an angle to rotate the needle, based on some other variable, so you don't need any XML code to handle non-linearity, your needle will have the usual "dumb" code definition in the modeldef.xml file, that won't do anything other than linking the animation to the L: variable, and all the work to decide how the needle is supposed to be rotated, will be in your C++ code.
But the approach is really generic: you can use it to simulate new systems. For example, in the MS F/A-18, I used a couple of tables (in addition to all 3d needles having non-linearity tables made this way) to derive the engine nozzle position based on the engine rpm, with two different tables used, depending if afterburners were active or not.
Last edited:


