hpt 0 Denunciar post Postado Novembro 2, 2012 Tenho um codigo que faz a colisão entre 2 circulos, após a colisão eles retornam para lado oposto com o mesmo angulo e a mesma velocidade, mas tem um problema, os circulos sempre colidem no mesmo ponto, gostaria de saber se tem alguma forma deles colidirem em pontos aleatorios da tela? O codigo é o seguinte: #include <SFML/System.hpp> #include <SFML/Graphics.hpp> #include <iostream> #include <sstream> bool collision(sf::RenderWindow &Window, sf::Shape Circle1, sf::Shape &Circle2){//função de colisão entre os circulos if((abs(Circle1.GetPosition().x - Circle2.GetPosition().x)) + abs((Circle1.GetPosition().y - Circle2.GetPosition().y)) <= 100){ return true; }else{ return false; } } int main() { int x = 350; int y = 100; int xVetor = 3; int yVetor = 3; int x2 = 300; int y2 = 300; int x2Vetor = -3; int y2Vetor = -3; bool collided = false; sf::Shape Circle1 = sf::Shape::Circle(0, 0, 50, sf::Color(0, 0, 0), 1, sf::Color(0, 0, 0)); sf::Shape Circle2 = sf::Shape::Circle(0, 0, 50, sf::Color(0, 0, 0), 1, sf::Color(0, 0, 0)); sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Colisão entre Circulos"); //GAME LOOP while(Window.IsOpened()){ sf::Event Event; while(Window.GetEvent(Event)){ if(Event.Type == sf::Event::Closed || Event.Key.Code == sf::Key::Escape) Window.Close(); } Circle1.SetPosition(x, y); Circle2.SetPosition(x2, y2); x += xVetor; if((x < 50) || (x > 750)){ xVetor *= (-1); } y += yVetor; if ((y < 50) || (y > 550)){ yVetor *= (-1); } x2 += x2Vetor; if((x2 < 50) || (x2 > 750)){ x2Vetor *= (-1); } y2 += y2Vetor; if ((y2 < 50) || (y2 > 550)){ y2Vetor *= (-1); } //CHAMADA DE FUNÇÔES if (collision(Window, Circle1, Circle2) && !collided){ yVetor *= (-1); xVetor *= (-1); y2Vetor *= (-1); x2Vetor *= (-1); collided = true; } if (!collision(Window, Circle1, Circle2) && collided){ //x2Vetor *= (-1); collided = false; } Window.Clear(sf::Color(255, 255, 255)); Window.Draw(Circle1); Window.Draw(Circle2); Window.Display(); sf::Sleep(0.01f); } return EXIT_SUCCESS; } Compartilhar este post Link para o post Compartilhar em outros sites
Mateus GP 13 Denunciar post Postado Novembro 20, 2012 Utilize a função rand ao invés de (-1). //... xVetor *= -((rand() % MAIOR_VELOCIDADE) + 1); //... Compartilhar este post Link para o post Compartilhar em outros sites