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

Author Topic: Sprite moves back to orignal position even after use of move()  (Read 1416 times)

0 Members and 1 Guest are viewing this topic.

CyrusK

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Hello, I'm having a problem, a sprite moves back to its original position even after use of move() function and i cant figure out why.
Seperate Axis Theorem function that utilizes move funcion.
void SAT(Player player,sf::Sprite sprite)
{
    float length = abs(player.sprite.getPosition().x-sprite.getPosition().x);
    float half1 = player.sprite.getGlobalBounds().width/2;
    float half2 = sprite.getGlobalBounds().width/2;
    float gap = length - half1 - half2;
    if(gap > 0)
    {
        //no collision
    }
    else if (gap == 0)
    {
        float lengthy = abs(player.sprite.getPosition().y-sprite.getPosition().y);
    float half1y = player.sprite.getGlobalBounds().width/2;
    float half2y = sprite.getGlobalBounds().width/2;
    float gapy = lengthy - half1 - half2;
    if(gapy==0)
    {
        std::cout << "touch\n";
    }
    }
    else if(gap < 0)
    {
        float lengthy = abs(player.sprite.getPosition().y-sprite.getPosition().y);
    float half1y = player.sprite.getGlobalBounds().height/2;
    float half2y = sprite.getGlobalBounds().height/2;
    float projection = half1-half2;
    float gapy = lengthy - half1 - half2;
    if(gapy<0)
    {
        std::cout << "length: " << length << std::endl;
        std::cout << "lengthy: " << lengthy <<std::endl;
        std::cout << "gapy: " <<gapy <<std::endl;
        std::cout << "gap: "<< gap <<std::endl;

        player.sprite.move(500,150);
    }
    }

}

Main function
int main()
{
   sf::Texture pT;
   pT.loadFromFile("player2.png");
   Player player(pT);
   sf::Sprite tile;
   tile.setTexture(pT);
   tile.setPosition(100,100);
   player.sprite.setPosition(100,100);

app.create(sf::VideoMode(600,800),"Mobus");

   while(app.isOpen())
   {


       sf::Event event;
       while(app.pollEvent(event))
       {
           if(event.type == sf::Event::Closed)
            app.close();
       }
    app.clear(color);
    SAT(player,tile);
    app.draw(player.sprite);
    player.update();
    app.draw(tile);
    app.display();

              }



    return 0;
}
 
Maybe I'm missing something obvious?
« Last Edit: April 08, 2015, 08:18:31 pm by CyrusK »

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: Sprite moves back to orignal position even after usef of move()
« Reply #1 on: April 08, 2015, 08:19:52 pm »
You are applying the move to a player object which gets destroyed after the call to SAT.
The original one is never moved ;)

The same applies for the sprite btw.

do something like void SAT(Player& player,sf::Sprite& sprite)

CyrusK

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Sprite moves back to orignal position even after use of move()
« Reply #2 on: April 08, 2015, 08:43:46 pm »
 ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D
Thank you, it worked.