PathEngine home previous: Translating the agentnext: Tutorial 3
Contents, Programmers Guide, Tutorials, Tutorial 2, SlideAgainst()

SlideAgainst()

SlideAgainst() is defined in 'sliding.cpp' and is just a bit of quick and dirty vector maths.

The function is defined as follows:

void SlideAgainst(const tSigned32* collidingline, tSigned32 currentx, tSigned32 currenty, tSigned32& dx, tSigned32& dy)
{
	float dotproduct = dx * (collidingline[2] - collidingline[0]) + dy * (collidingline[3] - collidingline[1]);
	float ratio = dotproduct;
	tSigned32 axisx = collidingline[2] - collidingline[0];
	tSigned32 axisy = collidingline[3] - collidingline[1];
	float axis_lengthsquared = axisx * axisx + axisy * axisy;
	ratio /= axis_lengthsquared;
	dx = axisx * ratio;
	dy = axisy * ratio;
	tSigned32 targetx, targety;
	targetx = currentx + dx;
	targety = currenty + dy;
	if(SideOfLine(collidingline, targetx, targety) == SIDE_RIGHT)
	{
		PushToLeftOfLine(collidingline, targetx, targety);
		dx = targetx - currentx;
		dy = targety - currenty;
	}
}

This basically takes the component of the movement vector along the line of the obstruction.
It is slightly complicated by the possibility that approximation may result in a point behind the line we are sliding against.
This is detected by the SideOfLine() test, and fixed by calling PushToLeftOfLine().

PushToLeftOfLine() is defined as follows, and basically just adds or subtracts one to or from one of the coordinates depending on the angle of the line.

inline void PushToLeftOfLine(const tSigned32 *line, tSigned32 &x, tSigned32 &y)
{
	tSigned32 axisx = line[2] - line[0];
	tSigned32 axisy = line[3] - line[1];
	tSigned32 absolute_x, absolute_y;

	absolute_x = axisx;
	if(absolute_x < 0)
	{
		absolute_x = -axisx;
	}
	absolute_y = axisy;
	if(absolute_y < 0)
	{
		absolute_y = -axisy;
	}
	
	// force rounding in axis with smaller component
	if(absolute_y > absolute_x)
	{
		if(axisy > 0)
		{
			x--;
		}
		else
		{
			x++;
		}
	}
	else
	{
		if(axisx < 0)
		{
			y--;
		}
		else
		{
			y++;
		}
	}
}

Documentation for PathEngine release 5.16 - Copyright © 2002-2008 PathEnginenext: Tutorial 3