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

Author Topic: character movement  (Read 2113 times)

0 Members and 1 Guest are viewing this topic.

vrige

  • Newbie
  • *
  • Posts: 11
    • View Profile
character movement
« on: December 03, 2019, 03:39:14 pm »
I am having some problems trying to show the moviment of my character after i have clicked 'A'.
This is all my code: https://github.com/vrige/sfml_1.
This is the solution that i tried, but it didn't work:
             
 chrono::milliseconds timespan(500);
 /*
     code
*/

      else if (event.key.code == sf::Keyboard::A) {
                        cout << "tasto A" << endl;
                        if (!player.getIsMoved()) {                  

                            if (aStar.astar()) {
                                vector<char> path = aStar.getEasierToReadPath();
                           
                                for (char iter : path) {
                                    player.movePlayer(iter);

                                    this_thread::sleep_for(timespan); //here
                                    player.setIsMoved(false);
                                }
                            }
                        }
                    }
Sorry for my english.
« Last Edit: December 03, 2019, 03:41:08 pm by vrige »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: character movement
« Reply #1 on: December 03, 2019, 05:11:05 pm »
What exactly do you mean by "didn't work"?

What is it actually doing and what do you expect it to do?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

vrige

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: character movement
« Reply #2 on: December 04, 2019, 10:09:12 am »
Now it shows that the character can go from the start to the goal, but i would like to show all the intermediate steps. I would like to show graphically the path chosen by AStar. So i have tried to make it sleeps in each steps, but it didn't work.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: character movement
« Reply #3 on: December 04, 2019, 07:53:17 pm »
Does it move the entire movement immediate but you want it to travel in increments?
Try multiplying the movement by the time that has passed for that one frame/loop (in seconds):

It's possible, though, that you instead want to interpolate points. That is, given two points, you want to work out the points along the line between them.
You could use something like this (this code is from here):
template <class T>
T linear(const T& start, const T& end, const alphaT& alpha)
{
        return static_cast<T>(start * (1 - alpha) + end * alpha);
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

vrige

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: character movement
« Reply #4 on: December 05, 2019, 11:45:00 am »
I have found an old solution for my code.
https://en.sfml-dev.org/forums/index.php?topic=24242.0 based on https://github.com/SFML/SFML-Game-Development-Book/blob/master/01_Intro/Source/Game.cpp
Thank you for the help!

                            if (aStar.astar()) {
                                vector<char> path = aStar.getEasierToReadPath();

                                sf::Clock clock;
                                sf::Time timeSinceLastUpdate = sf::Time::Zero;
                                float frametime = 1.0f / 6.0f; //รจ un immagine al secondo
                                sf::Time frameTime = sf::seconds(frametime);

                                for (char iter : path) {
                                    while(true){
                                        sf::Time elapsedTime = clock.restart();
                                        timeSinceLastUpdate += elapsedTime;

                                        if (timeSinceLastUpdate > frameTime){

                                            timeSinceLastUpdate -= frameTime;
                                            player.movePlayer(iter);
                                            view.setCenter(player.getPos().x * player.getPlayerSize().x, player.getPos().y * player.getPlayerSize().y);
                                            player.setIsMoved(false);

                                            window.setView(view);
                                            window.clear(sf::Color::White);
                                            window.draw(map);
                                            window.draw(player);
                                            window.display();

                                            break;
                                        }
                                    }
                                }
                            }