this is something I pulled from what I made 2 years ago.
int BRAINENG::FollowPath(int sprite,vector<float> pathpoints,float maxspeed,bool restart)
{
if(velocity.size()==0)
{
vector<float> vel;
vel.push_back(1);
vel.push_back(1);
velocity=vel;
currentpathpoint=2;
}
agk::SetSpritePhysicsCanRotate(sprite, 0);
vector<float> retvect;
vector<float>targetP;
vector<float>currentpoint;
if(currentpathpoint<pathpoints.size()-1)
{
agk::SetSpritePhysicsOn(sprite,2);
targetP.push_back(pathpoints[currentpathpoint]);
targetP.push_back(pathpoints[currentpathpoint+1]);
}
else if(currentpathpoint>=pathpoints.size()-1)
{
//agk::SetSpritePhysicsOff(sprite);
targetP.push_back(pathpoints[pathpoints.size()-2]);
targetP.push_back(pathpoints[pathpoints.size()-1]);
}
int postemp=0;
currentpoint.push_back(agk::GetSpriteX(sprite));
currentpoint.push_back(agk::GetSpriteY(sprite));
if(restartfollowpath&&restart)
{
agk::SetSpritePhysicsOn(sprite,2);
restartfollowpath=false;
agk::SetSpritePositionByOffset(sprite,pathpoints[0],pathpoints[1]);
currentpathpoint=2;
}
else if(Length(targetP,currentpoint)<=5)
{
if(currentpathpoint>=pathpoints.size()-3&&restart)
{
//retvect=Seek(currentpoint,targetP,maph,tileh);
agk::SetSpritePositionByOffset(sprite,pathpoints[0],pathpoints[1]);
restartfollowpath=true;
}
else
{
currentpathpoint=currentpathpoint+2;
if(currentpathpoint<pathpoints.size()-2)
{
targetP[0]=pathpoints[currentpathpoint];
targetP[1]=pathpoints[currentpathpoint+1];
retvect=Seek(targetP,currentpoint,maxspeed);
agk::SetSpritePhysicsVelocity(sprite,retvect[0],retvect[1]);
}
//agk::SetSpritePosition(sprite,retvect[0],retvect[1]);
}
}
else
{
if(currentpathpoint<pathpoints.size()-2)
{
targetP[0]=pathpoints[currentpathpoint];
targetP[1]=pathpoints[currentpathpoint+1];
retvect=Seek(targetP,currentpoint,maxspeed);
agk::SetSpritePhysicsVelocity(sprite,retvect[0],retvect[1]);
}
else
{
// if(currentpathpoint>=pathpoints.size()-2&¤tpathpoint<=pathpoints.size()+2)
// {
//currentpathpoint=currentpathpoint+2;
targetP[0]=pathpoints[pathpoints.size()-2];
targetP[1]=pathpoints[pathpoints.size()-1];
retvect=Seek(targetP,currentpoint,maxspeed);
agk::SetSpritePhysicsVelocity(sprite,retvect[0],retvect[1]);
// }
/* else
{
targetP[0]=pathpoints[pathpoints.size()-2];
targetP[1]=pathpoints[pathpoints.size()-1];
agk::SetSpritePhysicsVelocity(sprite,0,0);
agk::SetSpritePosition(sprite,targetP[0],targetP[1]);
}*/
}
}
return currentpathpoint;
}
Developer of Space Chips, pianobasic, zipzapzoom, and vet pinball apps. Developed the tiled map engine seen on the showcase. Veteran for the military.