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)
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;
}