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

Author Topic: How do you make a sprite follow another sprite?  (Read 3159 times)

0 Members and 1 Guest are viewing this topic.

Programmy

  • Newbie
  • *
  • Posts: 19
    • View Profile
How do you make a sprite follow another sprite?
« on: September 26, 2018, 04:29:55 am »
like in the rpg where if a character goes up the second and third will follow?
my code only does it where the second sprite follows but it lags behind after some distance
and is obviously not the solution...

#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>


int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
       

        sf::Texture heroTexture;
        sf::Sprite heroSprite;

        if (!heroTexture.loadFromFile("Eagle.png"))
        {
                std::cout << "Failed to load Eagle" << std::endl;

        }
        heroSprite.setTexture(heroTexture);


        //square snake
        std::vector<sf::Sprite>hero;
        hero.push_back(sf::Sprite(heroSprite));
        hero.push_back(sf::Sprite(heroSprite));
        hero.push_back(sf::Sprite(heroSprite));




        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();

                       
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                hero[0].move(-10,0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                hero[0].move(+10, 0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                        {
                                hero[0].move(0, -10);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        {
                                hero[0].move(0, +10);
                        }
                       

                        /////////////
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                hero[1].move(-9, 0);
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                hero[1].move(+9, 0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                        {
                                hero[1].move(0, -9);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        {
                                hero[1].move(0, +9);
                        }

                }

               

               
                window.clear();
                window.draw(hero[0]);
                window.draw(hero[1]);
                window.draw(hero[2]);
                window.display();
        }

        return 0;
}
 

Grenthal

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: How do you make a sprite follow another sprite?
« Reply #1 on: September 26, 2018, 11:55:23 am »
Hello,

As everything, this could be achieved using various approaches.
The one that I would suggest would look like something below (I’m writing this outside of compiler so please threat this only as pseudocode – just to show the logic). Keep in mind that potential collision with environment is omitted here.

// object containing npc related information like its sprites and methods for handling movement, getting position etc.
std::vector<NPC> party;

public void moveParty(Direction direction) {
    for (int i=0; i<party.size(); ++i) {
        NPC currentNpc = party.get(i);
                               
        if ( i == 0 ) { // first NPC
            bool isMovementPossible = COLLISION_CHECKING_METHOD();
            if (!isMovementPossible) {
                break;  // movement is not possible
            }
                                       
            sf::Vector2f currentPosition = currentNpc.getPosition();
            currentNpc.updateLastPosition(currentPosition);
            currentNpc.move(direction);
                       
        } else { // other NPC's are just following
            NPC previousNpcLocation = party.get(i - 1).getLastPosition();
            currentNpc.moveToPosition(previousNpcLocation);
        }
    }
}

Above is shown at example of some npc party but the same logic would apply for one entity build from many sprites (like snake for example). It’s just a matter of defining main object name (npc, snake or whatever).

I think you’ll get the idea  :)
« Last Edit: September 26, 2018, 03:27:05 pm by Grenthal »

 

anything