Hello guys! I have actually posted similar thread but I will a post new one. I have finally made my sprite to move from point a to point b with AI Path Finding. The problem is that my sprite jitters like crazy at a certain tile and it takes a while before it gets into the next tile when it jitters, you know it goes back and forth in small area so fast it seems its like shaking. I know for the fact in could not resolve its x and y position by smallest fraction. I don't know what I am missing here.
The manner by which I move the sprite is I use normalization of the vector. I found the movement cool. But I know that calculation messes it up.
This is just a prototype so stop lecturing me on conservative way on writing things. I am just used on seeing things in full scope rather than abstracting it in code.
This is the function which generate the path from the AI path finder
void MovableWorldObjects::getPath()
{
_Iterator end_iter = closedList.find( endTileID );
_Iterator start_iter = closedList.find( startTileID );
Tile * t;
t = closedList[ currentTileID ];
path.push_back( t );
while( end_iter != start_iter )
{
t = end_iter->second->getParentTile();
path.push_back( t );
end_iter = closedList.find( t->getTileID() );
}
std::reverse( path.begin() , path.end() );
if( ( c + 1 ) < path.size() )
{
spriteCurrentTile = path[ c ];
spriteNextTile = path[ c + 1 ];
cout << "from getTile()--the next selected tile is : : " << spriteNextTile->getTileID() << endl;
}
/*
cout << endl << endl;
cout << "Path is : " << endl;
for( size_t i = 0; i < path.size(); i++ )
{
cout << path[ i ]->getTileID() << " " << endl;
int x = path[ i ]->getTileGridPositionX();
int y = path[ i ]->getTileGridPositionY();
}
*/
}
This is my function that tells the sprite to move and follow the path
void MovableWorldObjects::moveToDestination( float e )
{
float velocity = 150.0f;
float tilesize = static_cast< float >( tileset->getTilesize() );
if( _isPathFound )
{
if( ( c + 1 ) < path.size() )
{
targetPosition.x = spriteNextTile->getTileGridPositionX() * tilesize;
targetPosition.y = spriteNextTile->getTileGridPositionY() * tilesize;
spritePosition.x = perspectiveX;
spritePosition.y = perspectiveY;
//cout << "Sprite moving at : " << movex << " x " << movey << endl;
delta.x = targetPosition.x - spritePosition.x;
delta.y = targetPosition.y - spritePosition.y;
float dist = sqrt( ( delta.x * delta.x ) + ( delta.y * delta.y ) );
delta /= dist;
float distMoved = e * velocity;
float movex = spritePosition.x + delta.x * distMoved;
float movey = spritePosition.y + delta.y * distMoved;
perspectiveX = movex;
perspectiveY = movey;
if( spritePosition.x == targetPosition.x && spritePosition.y == targetPosition.y )
{
spriteCurrentTile = path[ c ];
spriteNextTile = path[ c + 1 ];
c++;
cout << "moved" << endl;
}
WorldObjects::setSpritePosition( movex , movey );
}
else
{
_isPathFound = false;
cout << endl << endl;
cout << "==================================" << endl;
}
}
else
{
c = 0;
}
}
Also I am quite a little bit confuse regarding the top left coordinates, I have set sprite origin to its center.
and I wanted to check if the sprite move to the "Center" of the tile. If I divide the target by 2 it doesn't get the right path. So I am basing the checks on the top left not on the center. I need this because I have to rotate the sprite from tile to tile.
I been doing this for a very long time. For no reasons at all I could not find my shortcomings in this code. I am struggling, this is suppose to be very easy. I could grasp the algorithm very well. I can't refactor or restart again from scratch because I can't get over this thing. I just can't give up. I could not even sleep and concentrate on other things, personally, this really bugs me.
If you can help me that'd be great and you could free me from my worries.