Overtaking
Author: Bernhard Wymann
Keywords: torcs, berniw, bernhard wymann, overtaking, opponent

# 7.6 Finishing Overtaking ### Introduction Finally we need to add the offset to the target point. We will integrate that directly into the getTargetPoint() method. For that we need a normalized vector at the target point perpendicular to the track tangent. Before we add this vector we multiply it with the offset. ### Implementation Have a look on the changed Driver::getTargetPoint() method.
    /* compute target point for steering */
    v2d Driver::getTargetPoint()
    {
        tTrackSeg *seg = car->_trkPos.seg;
        float lookahead = LOOKAHEAD_CONST + car->_speed_x*LOOKAHEAD_FACTOR;
        float length = getDistToSegEnd();
        float offset = getOvertakeOffset();
The first change is the offset variable, initialized with getOvertakeOffset().
        while (length < lookahead) {
            seg = seg->next;
            length += seg->length;
        }

        length = lookahead - length + seg->length;
        v2d s;
        s.x = (seg->vertex[TR_SL].x + seg->vertex[TR_SR].x)/2.0;
        s.y = (seg->vertex[TR_SL].y + seg->vertex[TR_SR].y)/2.0;

        if ( seg->type == TR_STR) {
            v2d d, n;
            n.x = (seg->vertex[TR_EL].x - seg->vertex[TR_ER].x)/seg->length;
            n.y = (seg->vertex[TR_EL].y - seg->vertex[TR_ER].y)/seg->length;
            n.normalize();
Here the new v2d variable "n" has been added. It is initialized with the normalized vector perpendicular to the track tanget.
            d.x = (seg->vertex[TR_EL].x - seg->vertex[TR_SL].x)/seg->length;
            d.y = (seg->vertex[TR_EL].y - seg->vertex[TR_SL].y)/seg->length;
            return s + d*length + offset*n;
        } else {
Finally "n" is multiplied with offset and added to the target point. A similar implementation follows for the turns.
            v2d c, n;
            c.x = seg->center.x;
            c.y = seg->center.y;
            float arc = length/seg->radius;
            float arcsign = (seg->type == TR_RGT) ? -1.0 : 1.0;
            arc = arc*arcsign;
            s = s.rotate(c, arc);
            n = c - s;
            n.normalize();
            return s + arcsign*offset*n;
        }
    }
### Testdrive Before you go to the track you should set the camber of the front wheels to -5 degrees. Change this in the file 0/default.xml. My conclusion after some races is that the collision avoidance and overtaking works really well (at least for its simplicity). ### Downloads In case you got lost, you can download my robot for TORCS 1.2.0 or later. ### Feedback Let me know if you read this chapter and your thoughts about it. Please send me also spelling, grammar, math and code corrections. Thank you for the feedback. ### Summary You have implemented and understood collision avoidance and overtaking. You have a robot which is able to race. You know how to improve it.