SFML community forums

Help => General => Topic started by: nswift on August 01, 2014, 06:24:07 am

Title: String to movement help
Post by: nswift on August 01, 2014, 06:24:07 am
Hey everyone,

Currently I'm having a problem trying to implement movement into my game that works with path-finding.

Right now what I have is A* code that will create a string, "0000100223331100", that shows which direction the player needs to walk. '0' is right, '1' is down, '2' is left, and '3' is up. I have also tried to change the string into a vector of sf::Vector2f's, but I've taken it out as I think it's unnecessary.

The problem I'm having is converting this string into player movement. I have tried a for-loop, but it doesn't work very well. The best I have so far is...

for(int i = 0; i < route.size(); i++)
{
        switch(route[i])
        {
        case '0':
                rect.move(32, 0);
                break;
        case '1':
                rect.move(0, 32);
                break;
        case '2':
                rect.move(-32, 0);
                break;
        case '3':
                rect.move(0, -32);
                break;
        }
}

... while this follows the path, it instantly teleports my play straight to the end. Instead I would like the player to slowly walk along the path.

I would love it if someone could post some code or explain to me how to structurally go about writing the code.

Thanks,
Nswift
Title: Re: String to movement help
Post by: Strelok on August 01, 2014, 06:34:42 am
You must consider the context you're working on: the game runs on a loop based on frames, that means when you write 32 you're actually writing that you want the player to move 32 pixels per frame which is a lot. You must base your movement in pixel per seconds so that you can use a deltatime in your loop to change the movement per frame accordingly. Of course this implies that the movement must be carried on until its conpletion (a single move won't be enough).
How time works in SFML (http://www.sfml-dev.org/tutorials/2.1/system-time.php)
Time step and deltatime (http://gafferongames.com/game-physics/fix-your-timestep/) (the article goes quite in depth but you only need to understand the first part)
Title: Re: String to movement help
Post by: nswift on August 01, 2014, 08:28:35 am
Sorry I must not have made my original post clear enough.

I understand how movement works, I've successfully made player movement work with mouse click and arrow keys, but I'm not sure how to go about making the player move by checking route.

I can't do a...

while(rect.pos != targetPos)
{
    rect.move(0.01*deltaTime.asMilliseconds(), 0);
}

... within the switch loop within the for loop, as my program will freeze.

I am wondering how I would go about writing and calling a function to make my player move with the specific route in mind.
Title: Re: String to movement help
Post by: kimci86 on August 01, 2014, 11:35:46 am
Hello,
What you can do is to compute the next position from your route and go towards this position on each frame.

(click to show/hide)
Title: Re: String to movement help
Post by: Laurent on August 01, 2014, 12:01:37 pm
Quote
while(toTravel > 0.f && !route.empty())
Shouldn't this be a "if" rather than a "while"? ;)
Title: Re: String to movement help
Post by: kimci86 on August 01, 2014, 02:18:40 pm
Quote
while(toTravel > 0.f && !route.empty())
Shouldn't this be a "if" rather than a "while"? ;)

No, it is a "while".
The player movement can be split into several parts in one frame: if the player is close enough to the m_nextPosition at the beginning of the frame or if the distance to travel is big enough (long frameTime or high speed).
Title: Re: String to movement help
Post by: zsbzsb on August 01, 2014, 04:02:26 pm
Maybe you should learn what while actually means.....

See my following post (http://en.sfml-dev.org/forums/index.php?topic=15953.msg114834#msg114834).
Title: Re: String to movement help
Post by: kimci86 on August 02, 2014, 11:42:33 am
Sorry if I was not easily understandable.
I hope this diagram will make it easy.

(https://dl.dropboxusercontent.com/u/32848850/route.png)

The distance to travel is calculated on each frame and the player follows the route while this distance is not traveled.
Title: Re: String to movement help
Post by: Laurent on August 02, 2014, 05:42:10 pm
But you can never have such a while loop inside your game loop, because you must continue to handle events, update other entities and draw everything. Explaining that is the point of this whole discussion.
Title: Re: String to movement help
Post by: Codespear on August 03, 2014, 09:42:44 am
Nswift,

You may want to reconsider the output of your A* algorithm.  Instead of it producing a sequence of movements, let it instead produce a sequence of way-points.  There is little reason to encode this list into a string; keep the points as Vector2f in a std::vector or something. 

These way-points are then the plan (or a map) for the player.  He must follow this plan to get to the destination.

Given the plan, the you take the first way-point and make it the destination of the player.  Based on the destination and the player's current position you work out the move direction (such as 'up').  Then, in the game loop, you move the player a tiny bit in that direction.  When it reaches the destination, you remove the way-point from the plan, and choose the next way-point.  Then you repeat the process just described. When the plan is empty, the player has reached its destination.     
 
Title: Re: String to movement help
Post by: zsbzsb on August 14, 2014, 03:49:40 pm
Alright, I want to be fair to kimci86 and clarify a few things.

Quote
while(toTravel > 0.f && !route.empty())
Shouldn't this be a "if" rather than a "while"? ;)

Laurent, you picked on the wrong while loop here. You should have been picking on this loop.

while(rect.pos != targetPos)

Maybe you should learn what while actually means.....

When I made this post I was picking on the person's code that Laurent had quoted (by mistake ;) ) and also me being in a hurry I didn't take time to read the entire post. So kimci86's code is perfectly fine and it was the OP's code that was wrong. I just wanted to clarify this and correct the confusion. Also I wanted to apologize to kimci86 for incorrectly posting about his code. All is good now  ;D