]> git.tdb.fi Git - r2c2.git/commitdiff
More efficient picking of terrain nodes
authorMikko Rasa <tdb@tdb.fi>
Thu, 21 Nov 2013 17:41:15 +0000 (19:41 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 21 Nov 2013 17:41:15 +0000 (19:41 +0200)
source/libr2c2/terrain.cpp

index e84c7cdc1c360102a89fb60d92af868363e27d2f..d4f3e416a84357efd1107d82d3c2c1b9fe91e573 100644 (file)
@@ -132,6 +132,11 @@ Vector Terrain::get_node_position(const NodeCoordinates &c) const
 
 Terrain::NodeCoordinates Terrain::get_closest_node(const Ray &ray) const
 {
+       Transform reverse_trans = Transform::rotation(rotation, Vector(0, 0, -1))*
+               Transform::translation(-position);
+       Ray local_ray = reverse_trans.transform(ray);
+
+       float ts = type.get_tile_size();
        NodeCoordinates coords;
        float closest_dist = -1;
        for(unsigned y=0; y<height; ++y)
@@ -139,17 +144,16 @@ Terrain::NodeCoordinates Terrain::get_closest_node(const Ray &ray) const
                        for(unsigned i=0; i<4; ++i)
                        {
                                NodeCoordinates c(x, y, i);
-                               /* XXX This is not very efficient.  Should transform the ray to
-                               local coordinate system. */
-                               Vector node_pos = get_node_position(c);
-                               Vector v = node_pos-ray.get_start();
-                               float dist = (v-ray.get_direction()*dot(ray.get_direction(), v)).norm();
+                               Vector node_pos((x+i%2)*ts, (y+i/2)*ts, get_node_elevation(c));
+                               Vector v = node_pos-local_ray.get_start();
+                               float dist = (v-local_ray.get_direction()*dot(local_ray.get_direction(), v)).norm();
                                if(closest_dist<0 || dist<closest_dist)
                                {
                                        coords = c;
                                        closest_dist = dist;
                                }
                        }
+
        return coords;
 }