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?