Menu

#94 Distance raced calculated incorrectly

closed
1
2012-07-03
2009-04-30
No

While using TORCS in an AI learning experiment I have encountered a bug in the way the distance raced variable is calculated. The bug only occurs if you race over the the start/finish line then cross the start/finish line again but in the wrong direction. What happens is the track length is added to the distance raced as soon as this occurs therefore giving the player an extra raced distance of the track length. The code to be changed is located in raceengine.cpp ("src/libs/raceengineclient/raceengine.cpp") on line 334. The changes to make are below.

This should be changed from:
car->_distRaced = (car->_laps - 1) * ReInfo->track->length + car->_distFromStartLine;

to:
car->_distRaced = (car->_laps - (info->lapFlag + 1)) * ReInfo->track->length + car->_distFromStartLine;

This works by tracking how many laps the wrong way you have went (lapFlag) and subtracting them from car->_laps, which when multiplied will give the correct distance raced in car->_distRaced. There are no added variables to this code, it just uses what was already there.

Thanks,
Hamid Homatash

Discussion