9
I need to know how to make each circle collide with each other along with the part of how I’m going to go in such a direction after this collision.
This is the class of the main program window:
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
}
};
– Guilherme Bernal
I think you didn’t read what I wrote, and certainly don’t know how QT Creator works, because it’s not just one code for the program to work but several, so add the link containing the compressed file with the codes.
– Marlon Paranhos
I have read your question and use Qtcreator as the main IDE. I still don’t know what your question is. What problem do you have? In the current format there is no good answer without solving all the activity for you.
– Guilherme Bernal
Yes, the question seems to be very broad. If you don’t have a specific problem, it’s hard to answer. And @Guilhermebernal knows a lot about Qt Creator. I didn’t really understand the problem, but I doubt it’s related to Qt Creator. And QT Creator isn’t even the correct name for the Qt IDE. QT is short for Quicktime.
– Maniero
In the current code, the circles pass through each other, and I need to make them collide with each other and go back in opposite directions.
– Marlon Paranhos
Right. And what exactly is the problem you had? Didn’t you have a way to get the position of the circles? Can’t you calculate if two circles collide? Not sure what to do if they collide? Have you no idea how to change their direction of movement? Or did you manage to do all this and the result was different than expected? We need to know what you tried and where is the callus.
– Guilherme Bernal
The callus is in the part where I don’t know how to calculate the exact moment that each circle will collide with each other along with the part of how I’m going to go in that direction.
– Marlon Paranhos
@Marpd144 Edit the question and add it there, if possible explaining better what you have done with code, already tried and etc. This helps people understand your problem and help you.
– Lucas Lima