# Q circles: The crop circles of iRacing?

May 7th, 2011 by Dave Kaemmer

One of the strangest story lines in the history of iRacing is the Q circle phenomenon. It is an eerie tale, and if words like quaternion and eigenvalue send shivers up your spine, then you might do well to stop reading, toss some salt over your shoulder, and just walk away.

Our story begins many years ago, during the development of Grand Prix Legends, when unbeknownst to anyone, an insidious, barely perceptible flaw was introduced into the physics code. It lay dormant for several years until the release of Nascar 4. Sometime after that, astute oval racers discovered something very odd: laptimes at Talladega and Daytona had a strange tendency to bounce up and down every other lap, even though the car was being driven nearly identically. The time variation was small, mere hundredths of a second. When I first heard about it, I was skeptical. Surely these drivers were just seeing patterns where there weren’t any; superstition was slowly growing into urban legend. I figured the non-issue would soon go away, unreproducible in true, scientific, back to back tests. After all, what on earth could explain such a bizarre claim?

And yet, the issue didn’t go away. iRacing competition director Shannon Whitmore showed me one day how it worked, and sure enough, his laptimes fluctuated up and down, every other lap, by a few hundredths. Even more worrisome was the fact that this could be used to advantage in qualifying. Apparently, simply pretending to do a lap, by driving in a circle in pitlane before qualifying, would shift the good laptime/bad laptime fluctuation from odd/even laps to even/odd! This was key in qualifying at the superspeedways, since the best laptime is usually obtained on the second flying lap, after the car has had a longer time to reach top speed. By shifting the bizarre good laptime to the second qualifying lap, you could gain an advantage of a few hundredths, which is a pretty significant time difference, especially at the top levels of simracing.

And so the “Q circle” was born. Racers being racers, constantly on the lookout for a competitive advantage of any kind, we began to see the ritual of driving in a circle before qualifying at the superspeedways. It became downright embarrassing. People took sides, and argued vehemently for or against the practice. Was it a legitimate competitive advantage, or an unsporting cheat that should be banned? In the Nascar World Championship Races, the Q circle issue exploded into a contentious debate, with the solution being to have the drivers all send in a replay of their qualifying laps so we could insure that nobody was Q circling!

How could this be addressed? How could we even detect that it was being done? If someone happened to spin on their outlap, thereby gaining the Q circle advantage, how could we legitimately disqualify them? We needed to solve the mystery of the Q circles. We had quite a few discussions amongst the engineers, but to no avail. We couldn’t figure out how it could be happening. Theories abounded, but none, when researched, could explain it. Subtle differences in lap timing and scoring? Couldn’t find anything. Some fluctuation in air density or some other calculations every minute or so? Nope. How could the millions of calculations that were being done every second lead to behavior that was so predictable based on the mere number of laps completed—or actually the mere number of circles completed? It was a complete and utter mystery, and a very bizarre one at that.

Apparently, simply pretending to do a lap, by driving in a circle in pitlane before qualifying, would shift the good laptime/bad laptime fluctuation from odd/even laps to even/odd!

One day iRacing president Tony Gardner and Shannon came into my office and said we needed to establish a ruling on the Q circles. Should we allow them or not? Things were coming to a head. Tempers were rising. The legitimacy of iRacing as a fair arbiter of competition was being called into question. Things looked bad. I envisioned my career in a shambles, derailed by the Q circle conundrum. What could they be? What could be causing them?

It was at this very moment that a flash of realization came to me. It had to do with quaternions, I was almost sure of it! I said so to Tony and Shannon, but this did not seem to register with them. Shannon gave me a look that said, “stop joking around,” and Tony muttered, “what the heck is a Q-bonian?” While they continued to talk about how we should address the Q circle “exploit” I fired up the sim in the debugger and set a breakpoint (a way to stop the sim in mid-computation) where I knew I could get a look at the player car’s quaternion.

Of course, I should explain what a quaternion is. It is a mathematical entity (invented by the mathematician William Rowan Hamilton, famous for the Hamiltonian matrix) that provides a very useful way to represent the 3D orientation of an object. You are more likely to have heard about Euler angles, the well known yaw, pitch, and roll. These were invented by Leonhard Euler, the greatest mathematician of all time, who probably did more to change the modern world than any other human you can think of. If you’ve heard about race cars being pitch-sensitive, or having too much body roll in the turns, you know about Euler angles. Euler angles work for representing the rotation of an object in 3D, but they have some serious shortcomings. It’s difficult to take an object’s angular velocity (how fast it’s spinning) and update the object’s orientation using Euler angles. Also, there are orientations (the dreaded gimbal lock) where Euler angles suddenly change direction in a very non-linear way. A better way to represent orientations lies in a theorem that was proved by none other than…Euler. He showed how any rotation of an object in 3D can be accomplished by picking some axis through the object, and rotating it by some angle around that axis. Euler had discovered the basic idea behind quaternions, but it would take more than sixty years after Euler’s death for William Hamilton to turn this idea into something useful (in 1843).

A quaternion is made up of four numbers. Three of them specify the axis (a vector) about which an object is rotated, and one specifies the single angle of rotation about that axis, basically. They work very nicely for representing rotations, since they can be easily rotated by an object’s angular velocity, plus they don’t have a gimbal lock orientation. Quaternions are actually a little more complicated than that, and the reason I’m going on about this is that you need to know more to understand Q circles. The single angle number is actually the cosine of half the angle of rotation. The vector part representing the axis is scaled by the sine of half the angle of rotation. How Hamilton dreamed all this up, I have no idea. But the key thing is that it takes 720 degrees of rotation of an object, or two full circles, to take the cosine and sine parts through a complete cycle. Quaternions are not unique, that is, there are two quaternions representing any given rotation. Think about it: if you rotate an object by some angle around some axis, then that’s the same as rotating the object around the axis pointing exactly in the opposite direction by the negative of the same angle. So for a given orientation of your race car, there are two ways to specify it: with the positive or negative version of some quaternion.

So this is what I was looking for in the debugger. I wanted to see if the player car’s quaternion was the positive or negative version. The sim stopped at my breakpoint and I looked at the values. Quaternion was positive. Then I restarted the sim, and drove in a circle. Sure enough, my quaternion had become negative, just as I suspected, since I hadn’t gone through 720 degrees of rotation! Another circle and it was positive again. So at last it seemed I had found something that could potentially be related to the Q circles.

But the hunt wasn’t over yet. This was certainly something that varied every other lap, and could be affected by driving a Q circle. But the quaternions aren’t used for much except generating a rotation matrix (more math for handling rotations), and the positive and negative version of a quaternion both generate identical rotation matrices, according to the math. It shouldn’t matter whether your quaternion is positive or negative, the formulas give the same matrix, and that matrix is used everywhere else to actually rotate all the vectors used by the physics. Aerodynamic drag and downforce, tire forces, and even gravity are all manipulated using this identical rotation matrix. But maybe there was a bug—a mistake in the formulas coded in the simulation that would give a slightly different matrix for the positive and negative quaternion. With any luck, there might be a bug, and I could fix it!

A quaternion is made up of four numbers. Three of them specify the axis (a vector) about which an object is rotated, and one specifies the single angle of rotation about that axis, basically.

Unfortunately, there didn’t appear to be a bug. I pored over the matrix from quaternion routines and they were just what they were supposed to be. Still, I needed to check by running the code. I set another breakpoint, and stopped the code just at the instant the player’s car was placed on track. I wrote down all nine numbers from the rotation matrix, and all four numbers from the quaternion. I changed the code so that the negative quaternion was generated first, instead of the positive, when the car was first placed on track. It occurred to me that I could “fix” the problem by randomly assigning each driver a positive or negative quaternion, so that nobody would know whether they should drive a Q circle or not! I had to laugh, knowing the controversy that would erupt. No, I would have to fix this the right way. If it turned out to be some difference between the positive and negative quaternion, then I could easily force the code to always use only the positive one. But if there was no difference, the Q circles would return to their legendary status as eerie mystery. I digress. So here I was, now looking at the rotation matrix generated by the negative quaternion. The numbers weren’t all exactly the same as those from the positive case, but they were so close that it couldn’t really matter. Less than one part in four million difference for the numbers that were different, and some of the numbers were exactly the same.

Some of you may be wondering why the numbers were different at all. Some of you may be wondering why you’re still reading this. Some of you aren’t still reading this. For the first group, I’ll tell you: round-off error. Arithmetic on a computer is almost always done with a limited amount of precision, and the precision used in iRacing is generally the twenty three bits of mantissa in an IEEE format floating point number. That jargon is pretty much demanding an explanation, but time is too short for that. The only important thing to know is that one part in four million is a difference of plus or minus one bit of precision in a floating point number. Round-off error is just what you get when you’re trying to split the bill for $25.85 at a restaurant two ways, and you get $12.92 and a half cent. There is no such thing as a half cent, just as there is no such thing as a twenty fourth bit of precision in an IEEE float. Someone’s going to have to pay $12.93, and someone gets a better deal at $12.92. In restaurant bill terms, the negative quaternion was getting stuck with $40,000.01, and the positive quaternion was getting off with $40,000.00. Round-off error just doesn’t matter, for the most part, since it tends to be random and cancel itself out. Split a million restaurant bills, and you’re not going to save a ton of money on the round-off . . . unless you always get to save the half cent. But here is the interesting thing: the negative quaternion was always getting the larger restaurant bill!

The reason that’s the case has to do with the computer’s “round-off” mode, which controls which way the half cent (half bit) gets rounded. And it turns out to be significant. Almost all the force vectors, velocity vectors, and acceleration vectors that are used to move the car in the simulation are multiplied by this rotation matrix. That means on average, when the quaternion is negative, all these vectors end up being about one bit of precision (one part in 4 million or so) longer than when the quaternion is positive. When you use all these very slightly longer vectors for an entire lap at Talladega, you get a measurable time difference. That’s because you’d have a teeny amount more downforce, and a teeny amount more cornering force, and a teeny amount more velocity, plus probably other effects that helped you go just a teeny amount faster! So there wasn’t a bug, but just a piling up of round-off error one way or another for an entire lap. Amazingly, the top twenty drivers in the Nascar iRacing Driver’s World Championship field qualified entirely within that accumulated round-off error!

So the mystery is now understood (and fixed with an update scheduled this week), and I can sleep better at night without these eerie Q circles interrupting my dreams. And you can just roll straight out of pitlane, confident that if other drivers are driving in a circle, they’re only making themselves dizzy.