2.2 CommentsIntroductionThis section covers some selected topics related to coding for TORCS. Performance
Because there are several robots in the simulation, we have to take care that our robot doesn't eat
up too much performance. Look at the previous listing, I call there 3 times RtTrackSideTgAngleL. That's
very bad, because there are very expensive utility functions, so you should keep the number of calls
minimal. So whereever you can recycle an expensive computation, do it (that's a bit oversimplified, I know). Boolean ExpressionsIf you need complicated boolean expressions, encapsulate them into a function or a method. This will make the code easier to understand, because the name states what we want to ask (remember isStuck()). Portability
You have seen that TORCS runs on Windows, Linux/x86 and Linux/ppc. So you have to take care for
portability. There are also some issues with Visual C++ 6.0. For LoopsYou can write (according to the standard) this for loops:
for (int i=0; i < 100; i++) { ... } In fact the variable i should disappear after the for loop, but it doesn't in VC++. So to make VC++ happy, write
int i; Constants in ClassesImplement constants like I do in the next section, or use #define. If you need integer constants you can also abuse enumerations for them. The following example won't work with VC++:
class bla { Magic NumbersWhat's also very bad on my bot till now are the numbers, which are spread all over the place. For now we should define them on a central location as constants. Constants are superior over numbers, because they have meaningful names and you need to change them just once. In a later chapter we will read some of them from an XML file. Numerical ProblemsTo implement your robot you will certainly need floating point numbers. Keep in mind that these are not the same like the Real numbers from mathematics, they are discrete and there are gaps between any two numbers. If you are interested in the details search the web for "IEEE 754 floating point numbers". Adding up NumbersIf you add up values of very different magnitude, e. g. the float 20.000001 and 0.000001 the sum will be still 20.000001 (and not 20.000002). The cause is the internal representation of floating point numbers, be aware of such problems. As example you can produce this way an endless while loop, when the guard waits for the sum to grow to a certain limit. Special NumbersThe IEEE 754 floating point numbers have some special numbers to signal a special state or problems. The most interesting is the NaN (not a number) which results e. g. in the case when you divide 0.0 with 0.0. Once you have a NaN stored in a variable all computed results based on it are NaN as well. Sometimes you can avoid the danger with manipulating the expression. Summary
|
Back |
Let's get more cars. |