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

Author Topic: String to movement help  (Read 3998 times)

0 Members and 1 Guest are viewing this topic.

nswift

  • Newbie
  • *
  • Posts: 2
    • View Profile
String to movement help
« 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

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: String to movement help
« Reply #1 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
Time step and deltatime (the article goes quite in depth but you only need to understand the first part)
« Last Edit: August 01, 2014, 06:50:44 am by Strelok »

nswift

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: String to movement help
« Reply #2 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.

kimci86

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: String to movement help
« Reply #3 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)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: String to movement help
« Reply #4 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"? ;)
Laurent Gomila - SFML developer

kimci86

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: String to movement help
« Reply #5 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).

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: String to movement help
« Reply #6 on: August 01, 2014, 04:02:26 pm »
Maybe you should learn what while actually means.....

See my following post.
« Last Edit: August 14, 2014, 03:52:36 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

kimci86

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: String to movement help
« Reply #7 on: August 02, 2014, 11:42:33 am »
Sorry if I was not easily understandable.
I hope this diagram will make it easy.



The distance to travel is calculated on each frame and the player follows the route while this distance is not traveled.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: String to movement help
« Reply #8 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.
Laurent Gomila - SFML developer

Codespear

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: String to movement help
« Reply #9 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.     
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: String to movement help
« Reply #10 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
« Last Edit: August 14, 2014, 04:42:42 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

 

anything