Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem in moving sprite to point A to point B  (Read 3208 times)

0 Members and 1 Guest are viewing this topic.

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Problem in moving sprite to point A to point B
« on: August 08, 2013, 09:57:43 pm »
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.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: Problem in moving sprite to point A to point B
« Reply #1 on: August 08, 2013, 10:06:19 pm »
I'm without time to read it through but that jittery really seems like float precision issues, it seems to result from the object approaching point B but never reaching it, up to infinity, so as time goes on, the distance between the two gets infinitely small and the computer cant handle that..

Whether it helps or not, good luck ;D

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Problem in moving sprite to point A to point B
« Reply #2 on: August 08, 2013, 10:12:43 pm »
Yes grimshaw I know that what messes it up, I knew the fact that it could not reach that tiny fraction. Sorry that my post is very long.

If I could ask, how can I make my sprite to go left and right and up and down from to tile to tile then? I just couldn't figure it out.

But I'll try to turn everything to integers and see what happens. If not I might not consider vectors anymore. I knew its a very bad idea. I'll keep this thread alive for awhile.

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Problem in moving sprite to point A to point B
« Reply #3 on: August 08, 2013, 10:16:23 pm »
just add in a statement that checks if the distance remaining to the tile is less then the distance traveled in 1 frame. If it is, then snap the position to the tile position. This will stop the jittering from happening.

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Problem in moving sprite to point A to point B
« Reply #4 on: August 08, 2013, 10:21:58 pm »
WOw, I could not think of that, can you explain a little bit more? How am I suppose to do it?

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Problem in moving sprite to point A to point B
« Reply #5 on: August 08, 2013, 10:44:00 pm »
In case you want to change your mind about... changing your mind >.>

if( abs(spritePosition.x - targetPosition.x) < 1 && abs(spritePosition.y == targetPosition.y) < 1 ) // *Note
    {
         //were within a small distance to the target, so lets snap to the target. No one should notice
         spritePosition.x = targetPosition.x;
         spritePosition.y = targetPosition.y;
         spriteCurrentTile = path[ c ];
         spriteNextTile    = path[ c + 1 ];
         c++;
         cout << "moved" << endl;
    }
 

Note: 1 is some arbitrary number, you can make it whatever you want. Even do some math beforehand to find the distance you want.

Also the abs() (absolute function) call might be off, I'm at work so I can't test it at the moment, but a quick google search should put you in the right spot.

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Problem in moving sprite to point A to point B
« Reply #6 on: August 08, 2013, 11:00:51 pm »
And that's how you do it! Thank you very much Gobbles, actually, it still jitters, what I have done is to compare it to <= 1. It works now! Thank you very now I can rest in peace.

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Problem in moving sprite to point A to point B
« Reply #7 on: August 08, 2013, 11:16:16 pm »
Anytime :)

 

anything