Salutations tout le monde,

Je tente de faire un petit script de simulation de collisions de molécules dans une "boite".
J'ai commencé à faire ma QGraphicsScene pour tester la gestion des collisions.

Problème: quand je mets 10-15 molécules, tout va bien (en tout cas comme je veux...), mais dès que j'en rajoute, ça commence à ralentir sévèrement.

J'ai tenté d'optimiser mon code au maximum, mais là, je sèche, d'où mon post.

Je vous mets une version simplifiée de mon code (qui rame toujours), avec en plus la possibilité d'injecter des particules supplémentaires en appuyant sur la touche "H" (pour .... hop !)
Je vous remercie d'avance si vous pouvez jeter un coup d'oeil à ce code et m'aider à le rendre plus rapide.

Nollan

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys
import math
 
from PyQt5 import QtGui
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
 
 
class Particule(QGraphicsItem):
    def __init__(self,root):
        super().__init__()
        self.x = 0
        self.y = 0
        self.setRayon(2)
        self.setVitesseInitiale(5)
        self.setVxVyInit(45)
        self.setMasse(0.001)
        self.noIdentifiant = root.count
 
 
    def boundingRect(self):
        return QRectF(-self.rayon, -self.rayon, 2 * self.rayon, 2 * self.rayon)
 
    def paint(self, painter, option, widget):
        painter.setBrush(Qt.green)
        painter.drawEllipse(-self.rayon, -self.rayon, 2 * self.rayon, 2 * self.rayon)
 
    def setMasse(self,masse):
        self.masse=masse
 
    def getMasse(self):
        return self.masse
 
    def getX(self):
        return self.mapToScene(0, 0).x()
 
    def getY(self):
        return self.mapToScene(0, 0).y()
 
    def getVx(self):
        return self.vx
 
    def getVy(self):
        return self.vy
 
    def setVx(self,vx):
        self.vx = vx
 
    def setVy(self,vy):
        self.vy = vy
 
    def getRayon(self):
        return self.rayon
 
    def setRayon(self,rayon):
        self.rayon = rayon
 
    def setVitesseInitiale(self,vitesse):
        self.vitesse = vitesse
 
    def setVitesse(self,vx,vy):
        self.vx , self.vy = vx , vy
 
    def setVxVyInit(self,angle):
        self.vx = int(math.cos(angle * math.pi / 180) * self.vitesse)
        self.vy = int(math.sin(angle * math.pi / 180) * self.vitesse)                
 
    def bouge(self):
        self.x , self.y = self.getX() , self.getY()
        self.y = self.y + self.vy
        self.x = self.x + self.vx
        self.setY(self.y)
        self.setX(self.x)
        self.testCollision()
 
    def testCollision(self):
 
        zoneDanger = self.scene().items(QRectF(self.mapToScene(-2 * self.rayon-abs(self.vx), -2 * self.rayon-abs(self.vy)),
                                               self.mapToScene(2 * self.rayon + abs(self.vx), 2 * self.rayon + abs(self.vy))))
 
        for item in zoneDanger:
 
            if item is self:
                continue
            if type(self) is type(item):
                if (self.getX()-item.getX())*(self.getX()-item.getX()) + (self.getY()-item.getY())*(self.getY()-item.getY()) < (self.getRayon() + item.getRayon())*(self.getRayon() + item.getRayon()):
                    v1x = self.getVx()
                    v1y = self.getVy()
                    v2x = item.getVx()
                    v2y = item.getVy()
                    m1 = self.getMasse()
                    m2 = item.getMasse()
                    mu1 = m1 /(m1 + m2)
                    mu2 = m2 / (m1 + m2)
                    p = mu1 * m2 * math.sqrt((v1x * v1x + v1y * v1y + v2x * v2x + v2y * v2y) - 2 * v1x * v2x - 2 * v1y * v2y)
                    theta = qrand() % 360 /180 * math.pi
                    v3x = (p / m1 ) * math.cos(theta) + mu1 * v1x + mu2 * v2x
                    v3y = (p / m1 ) * math.sin(theta) + mu1 * v1y + mu2 * v2y
                    v4x = (p / m2 ) * math.cos(math.pi + theta) + mu1 * v1x + mu2 * v2x
                    v4y = (p / m2 ) * math.sin(math.pi + theta) + mu1 * v1y + mu2 * v2y
                    self.setVitesse(v3x, v3y)
                    item.setVitesse(v4x, v4y)
                    root.collision +=1
            else:
                item.rebondit(self)
 
class Bord(QGraphicsItem):
    def __init__(self):
        super().__init__()
        self.largeur = 10
        self.hauteur = 100
    def setLargeur(self,largeur):
        self.largeur = largeur
    def setHauteur(self,hauteur):
        self.hauteur = hauteur
    def getHauteur(self):
        return self.hauteur
    def getLargeur(self):
        return self.largeur
    def boundingRect(self):
        return QRectF(0, 0, self.largeur, self.hauteur)
 
    def paint(self, painter, option, widget):
        painter.setBrush(Qt.black)
        painter.drawRect(0, 0, self.largeur, self.hauteur)
 
    def getX(self):
        return self.mapToScene(0, 0).x()
 
    def getY(self):
        return self.mapToScene(0, 0).y()
 
    def setDirection(self,direction):
        self.direction = direction
    def getDirection(self):
        return self.direction
 
    def rebondit(self,particule):
        if self.collidesWithItem(particule):
            if self.direction == "Gauche":
                particule.setVx(-particule.getVx())
                particule.setX(int(self.getX() - particule.getRayon() - 1))
            elif self.direction == "Droite":
                particule.setVx(-particule.getVx())
                particule.setX(int(self.getX()+self.getLargeur()+particule.getRayon()+1))
            elif self.direction == "Bas":
                particule.setVy(-particule.getVy())
                particule.setY(int(self.getY()+self.getHauteur()+particule.getRayon()+1))    
            elif self.direction == "Haut":
                particule.setVy(-particule.getVy())
                particule.setY(int(self.getY() - particule.getRayon() - 1))
 
 
class BordG(Bord): #Bord Gauche
    def __init__(self):
        super().__init__()
        self.setDirection("Droite")
 
class BordD(Bord): #Bord Droite
    def __init__(self):
        super().__init__()
        self.setDirection("Gauche")
 
class BordB(Bord): #Bord Bas
    def __init__(self):
        super().__init__()
        self.setDirection("Haut")
 
class BordH(Bord): #Bord Haut
    def __init__(self):
        super().__init__()
        self.setDirection("Bas")
 
class Fenetre(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.count = 0
        self.collision = 0
 
        mainLayout = QHBoxLayout()
 
        self.scene = QGraphicsScene(0, 0, 500, 400)
 
        self.bordGauche = BordG()
        self.bordGauche.setHauteur(420)
 
        self.bordDroit = BordD()
        self.bordDroit.setHauteur(420)
 
        self.bordBas = BordB()
        self.bordBas.setLargeur(520)
        self.bordBas.setHauteur(10)
 
        self.bordHaut = BordH()
        self.bordHaut.setLargeur(520)
        self.bordHaut.setHauteur(10)
 
        self.scene.addItem(self.bordHaut)  
        self.scene.addItem(self.bordGauche)
        self.scene.addItem(self.bordDroit)
        self.scene.addItem(self.bordBas)
 
        self.bordGauche.setPos(-10 , -10)
        self.bordDroit.setPos(500 , -10)
        self.bordBas.setPos(-10 , 400)
        self.bordHaut.setPos(-10,-10)
 
        self.graphicsView = QGraphicsView(self.scene)
        mainLayout.addWidget(self.graphicsView)
        self.setLayout(mainLayout)
 
 
        self.dicoParticules=dict()
        for i in range(10):
            self.dicoParticules[i] = Particule(self)
            self.dicoParticules[i].setVxVyInit(qrand() %45 + 10)
            self.scene.addItem(self.dicoParticules[i])
            self.AugmenteCount()
 
 
 
        self.timer = QTimer()
        self.timer.timeout.connect(self.timerEvent)
        self.timer.start(1000 / 33)
 
    def keyPressEvent(self, event):
        key = event.key()
        if key == Qt.Key_H:
            self.AugmenteCount()
            self.dicoParticules[self.count]=Particule(self)
            self.dicoParticules[self.count].setVxVyInit(qrand() %45 + 10)
            self.scene.addItem(self.dicoParticules[self.count])
 
    def AugmenteCount(self):
        self.count +=1
 
    def timerEvent(self):
        for i in self.dicoParticules:
            self.dicoParticules[i].bouge()
 
 
 
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    root = Fenetre()
    root.show()
    sys.exit(app.exec_())