Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Trying to figure out ways to avoid obstacles with this new vector based approach

Trying to figure out ways to avoid obstacles with this new vector based approach

Calin
Calin
DreamLand editor · · 2 min read
244,244 13

A couple days ago I felt lost. And I still do. I had no idea where to start. Eventually I came up with this code. I`m creating an additional vector that points in the opposite direction of where the obstacle is. If I move using this vector I can make some room between the unit and the obstacle, which then allows me to move a little bit towards the required path node using the initial vector. It works but it appears ugly. The unit doesn`t have a constant speed while going around the obstacle. Also I haven't tried yet but my guess is that the unit will get "confused" if there are several obstacle units next to each other, which I guess is the main problem


if (useInitialVec)
{
float VectorX = NextNodeX - UpixPosX;
float VectorY = NextNodeY - UpixPosY;
float VecLength = sqrt(VectorX * VectorX + VectorY * VectorY);
float SpeedPercent = (0.016 * 100) / VecLength;
float AdvanceX = (VectorX * SpeedPercent) / 100;
float AdvanceY = (VectorY * SpeedPercent) / 100;

float DesiredX = UpixPosX + AdvanceX;
float DesiredY = UpixPosY + AdvanceY;

float DesCDtop = DesiredY - unitsize / 2;
float DesCDbott = DesiredY + unitsize / 2;
float DesCDleft = DesiredX - unitsize / 2;
float DesCDright = DesiredX + unitsize / 2;

bool CDTest = CDCheck(id,&obstacleid, DesCDtop, DesCDbott, DesCDleft, DesCDright);

if (CDTest == false)
{
 UpixPosX = DesiredX;
 UpixPosY = DesiredY;
}
else
{
 useInitialVec = false;
}
}
else
{
useInitialVec = true;
float ObstX;
float ObstY;
GetObstPos(obstacleid, &ObstX, &ObstY);

float VectorX = UpixPosX - ObstX;
float VectorY = UpixPosY - ObstY;
float VecLength = sqrt(VectorX * VectorX + VectorY * VectorY);
float SpeedPercent = (1 * 100) / VecLength;
float AdvanceX = (VectorX * SpeedPercent) / 100;
float AdvanceY = (VectorY * SpeedPercent) / 100;

UpixPosX = UpixPosX + AdvanceX;
UpixPosY = UpixPosY + AdvanceY;
}

Discussion

Loading comments...