@@ -794,11 +794,20 @@ namespace mplot
794794 *
795795 * \param model_to_scene The model to scene transformation for the parent of the navmesh
796796 *
797+ * \param search_dist_mult a multiplier on the search distance. The length of vdir in this
798+ * function should cross the landscape model. By default it's the vector from the camera
799+ * location in the model frame of reference to the middle of the bounding box. If the vector
800+ * is too long when finding the surface of a convex hull, such as a model of a rock, it is
801+ * possible to mis-identify the back side of the model. However, for finding a location on a
802+ * large, flat, one-sided landscape, we want to make vdir long. search_dist_mult is applied
803+ * to vdir.
804+ *
797805 * \return tuple containing: the hit point in scene coordinates; the triangle normal of the
798806 * triangle we hit; and the indices of the triangle we hit.
799807 */
800808 std::tuple<sm::vec<float >, sm::vec<float >, std::array<uint32_t , 4 >>
801- find_triangle_hit (const sm::mat44<float >& camspace, const sm::mat44<float >& model_to_scene)
809+ find_triangle_hit (const sm::mat44<float >& camspace, const sm::mat44<float >& model_to_scene,
810+ const float search_dist_mult = 1 .0f )
802811 {
803812 sm::mat44<float > scene_to_model = model_to_scene.inverse ();
804813 // use camera location in gltf to start from, then find model surface.
@@ -807,15 +816,16 @@ namespace mplot
807816 this ->tn0 = {};
808817 sm::vec<float > hit = {};
809818 sm::vec<float > vdir = this ->bb .mid () - camloc_mf;
810- std::tie (hit, this ->ti0 , this ->tn0 ) = this ->find_triangle_crossing (camloc_mf , vdir);
819+ vdir *= search_dist_mult;
820+ std::tie (hit, this ->ti0 , this ->tn0 ) = this ->find_triangle_crossing (camloc_mf, vdir);
811821
812822 if (this ->ti0 [0 ] == std::numeric_limits<uint32_t >::max ()) { std::cout << __func__ << " : No hit\n " ; }
813823
814824 sm::vec<float > hp_scene = (model_to_scene * hit).less_one_dim ();
815825
816- constexpr bool debug = false ;
826+ constexpr bool debug = true ;
817827 if constexpr (debug) {
818- std::cout << " found hit at " << hit << " (model); " << hp_scene << " (scene)\n " ;
828+ std::cout << " found hit at " << hit << " (model); " << hp_scene << " (scene) in direction " << vdir << " \n " ;
819829 // Check we'll get a hit when we compute_mesh_movement:
820830 sm::vec<sm::vec<float >, 3 > tv_mf = this ->triangle_vertices (this ->ti0 );
821831 std::cout << " tn0: " << this ->tn0 << " , length " << this ->tn0 .length () << std::endl;
0 commit comments