Posted By: PadMalcom
Find nearest path point to an entity - 07/20/14 01:48
path_spline in general starts at dist=0. To determine the nearest point on the path from an entity you can use:
So you can, for instance, spawn an entity next to the player and the entity knows at what distance it should start path_spline to directly start at the nearest position to the path.
Code:
var get_nearest_path_point(ENTITY* ent, char* pathname) { ENTITY* e = ent_create(NULL, ent->x, NULL); path_set(e, pathname); var length = path_length(e); var optDist = 0; var entDist = 10000; var dist = 0; var vLastPos[3]; var vDir[3]; while(dist <= length) { path_spline(e,e.x,dist); dist += 5*time_step; if (vec_dist(e->x, ent->x) < entDist) { entDist = vec_dist(e->x, ent->x); optDist = dist; } } ptr_remove(e); return optDist; }
So you can, for instance, spawn an entity next to the player and the entity knows at what distance it should start path_spline to directly start at the nearest position to the path.