Find nearest path point to an entity

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:

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.
Posted By: Puppeteer

Re: Find nearest path point to an entity - 07/20/14 16:26

Sorry, but I would not suggest to use that solution ingame because this is just brute force.

If you want to have better results I would recommend to use this solution:
PDF here

They use a combination of quadratic minimization and newtons method.
I have implemented this method in 2012 and it works really well.
© 2024 lite-C Forums