src/proto2/Terrain.cc
changeset 141 73109c5652d3
parent 140 b264b39224e5
child 142 00672d0682ac
equal deleted inserted replaced
140:b264b39224e5 141:73109c5652d3
    17     Vector coor = getPixelLocation(point);
    17     Vector coor = getPixelLocation(point);
    18     return !(terrain[coor.x][coor.y] == EMPTY);
    18     return !(terrain[coor.x][coor.y] == EMPTY);
    19 }
    19 }
    20 
    20 
    21 bool Terrain::collides(const Vector &begin, const Vector &end) const {
    21 bool Terrain::collides(const Vector &begin, const Vector &end) const {
    22     // TODO: Implement. Bresenhams line algorithm could be usefull
       
    23     // TODO: Maybe we should have another function prototype that also
    22     // TODO: Maybe we should have another function prototype that also
    24     // returns the point where we collided.
    23     // returns the point where we collided.
    25 
    24 
    26     // 1. Go trough every point in line in order and check if we collide
    25     // We'll use Bresenhams line algorithm to go trough all the
       
    26     // "pixels" of the line.
       
    27     Vector b = getPixelLocation(begin);
       
    28     Vector e = getPixelLocation(end);
    27 
    29 
       
    30     bool steep = (abs(e.y - b.y) > abs(e.x - b.x)); // k > 1
       
    31     if (steep) { // Line is steep -> swap x and y coordinates
       
    32         std::swap(b.x, b.y);
       
    33         std::swap(e.x, e.y);
       
    34     }
       
    35     if (b.x > e.x) { // Line goes down -> make it go up
       
    36         std::swap(b, e);
       
    37     }
       
    38     uint16_t dx = e.x - b.x;
       
    39     uint16_t dy = abs(e.y - b.y);
       
    40     uint16_t err = dx/2;
       
    41     uint16_t ystep;
       
    42     uint16_t y = b.y;
       
    43     // Is the line ascending or descending
       
    44     if (b.y < e.y) ystep = 1;
       
    45     else ystep = -1;
       
    46     // Go trough the line
       
    47     for (uint16_t x =  b.x; x <= e.x; x++) {
       
    48         if (steep) { // X and Y coordinates must be switched if steep
       
    49             if (terrain[y][x] != EMPTY) { // Collision!
       
    50                 return true;
       
    51             }
       
    52         } else {
       
    53             if (terrain[x][y] != EMPTY) { // Collision!
       
    54                 return true;
       
    55             }
       
    56         }
       
    57         err = err - dy;
       
    58         if (err < 0) { // Check if we want to make an ystep
       
    59             y = y + ystep;
       
    60             err = err + dx;
       
    61         }
       
    62     }
       
    63     return false; // No Collision
    28 }
    64 }
    29 
    65 
    30 void Terrain::removeGround(const Vector &pos, const float r) {
    66 void Terrain::removeGround(const Vector &pos, const float r) {
    31     // TODO: Implement. Some circle algoritmh should be usefull here.
    67     // TODO: Implement. Some circle algoritmh should be usefull here.
    32 
    68