During the last decade, electromagnetic tracking systems have matured to the level where they have been adopted in several medical applications. However, the tracking accuracy of EM tracking systems is highly sensitive to magnetic field distortions due to metal or electronic equipment in the environment. We have developed a system which detects and corrects such static disturbances of the magnetic field generated by EM tracking systems. The system uses an inexpensive haptic device acting as a robot and a highly accurate tracking device. Positions from both systems are automatically sampled and stored in a non-regular hexahedral mesh. Local transformations are computed for each hexahedron enabling transformation of intra-hexahedron positions between the two coordinate frames. This data-structure is used to correct EM tracking positions in real-time. The registration accuracy between the haptic and the EM tracking system improved from a root-mean-square error of 20.8 to 1.3 mm in an experiment in the operating room by applying the dewarping algorithm. The main advantages being that the system is highly portable, inexpensive, and can correct tracking signals in real-time.