I need to know how to make each circle collide with the other along with what I'm going to do to follow that direction after that collision.
This is the program's main window class:
class canvas : public QWidget {
Q_OBJECT
public:
explicit canvas(QWidget *parent) : QWidget(parent) {
m_particulas.resize(30);
int n = m_particulas.size();
for(int i = 0 ; i<n ; ++i) {
m_particulas[i].init();
}
startTimer(5);
}
protected:
void paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.setWindow(0,0,1000,1000);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 5, Qt::SolidLine, Qt::RoundCap));
painter.setBrush(QBrush(Qt::darkCyan, Qt::SolidPattern));
int n = m_particulas.size();
for(int i = 0 ; i<n ; ++i) {
double x = m_particulas[i].x();
double y = m_particulas[i].y();
double L = 2*m_particulas[i].r();
painter.drawEllipse(QPointF(x, y), L, L);
}
}
void timerEvent(QTimerEvent *) {
int n = m_particulas.size();
for(int i = 0 ; i<n ; ++i) {
m_particulas[i].andar();
}
update();
}
signals:
public slots:
private:
std::vector<Particula> m_particulas;
};
And that's the class of every particle on the screen:
class Particula {
private:
double m_x;
double m_y;
double m_vx;
double m_vy;
double m_r;
public:
Particula() {};
void init() {
m_r = 10;
m_x = 900.0*rand()/RAND_MAX + 50;
m_y = 900.0*rand()/RAND_MAX + 50;
m_vx = 2.0 * rand()/RAND_MAX - 1;
m_vy = 2.0 * rand()/RAND_MAX - 1;
double norma = sqrt(m_vx*m_vx + m_vy*m_vy);
m_vx /= norma;
m_vy /= norma;
}
double x() const { return m_x; }
double y() const { return m_y; }
double r() const { return m_r; }
void andar() {
m_x += m_vx;
m_y += m_vy;
if(m_x > 990-m_r) m_vx *= -1; //inferior - multiplicado por -1 para inverter a direção...
if(m_y > 990-m_r) m_vy *= -1; //direita
if(m_x < 30-m_r) m_vx *= -1; //esquerda
if(m_y < 30-m_r) m_vy *= -1; //superior
}
};