Repository navigation
Expand file tree
/
Copy pathParticleSim.py
More file actions
199 lines (158 loc) · 6.58 KB
/
Copy pathParticleSim.py
File metadata and controls
199 lines (158 loc) · 6.58 KB
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
import pygame
import random
import math
# Initialisation de Pygame
pygame.init()
# Paramètres de la fenêtre
screenWidth = 1000
screenHeight = 800
# Couleurs
black = (0, 0, 0)
blue = (50, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
orange = (255,200,0)
purple = (110,50,255)
# Paramètres ajustables
MAX_SPEED = 2
ATTRACTION_DIST = 150 # Distance maximale d'attraction
REPULSION_DIST = 150 # Distance minimale de répulsion
CLOSE_REPULSION_DIST = 20 # Distance très courte pour une répulsion plus forte
FRICTION = 10 # Réduction de vitesse progressive
# Forces de base
REPULSION_FORCE = 0.15
CLOSE_REPULSION_FORCE = 1
# Matrice des forces d'attraction
FORCE_MATRIX = {
# random.uniform()
("red", "red"): 1,
("red", "blue"): random.uniform(-1,1),
("red", "green"): random.uniform(-1,1),
("red", "orange"): random.uniform(-1,1),
("red", "purple"): random.uniform(-1,1),
("green", "green"): 1,
("green", "red"): random.uniform(-1,1),
("green", "blue"): random.uniform(-1,1),
("green", "orange"): random.uniform(-1,1),
("green", "purple"): random.uniform(-1,1),
("blue", "blue"): 1,
("blue", "red"): random.uniform(-1,1),
("blue", "green"): random.uniform(-1,1),
("blue", "orange"): random.uniform(-1,1),
("blue", "purple"): random.uniform(-1,1),
("orange", "orange"): 1,
("orange", "red"): random.uniform(-1,1),
("orange", "green"): random.uniform(-1,1),
("orange", "blue"): random.uniform(-1,1),
("orange", "purple"): random.uniform(-1,1),
("purple", "purple"): 1,
("purple", "red"): random.uniform(-1,1),
("purple", "green"): random.uniform(-1,1),
("purple", "blue"): random.uniform(-1,1),
("purple", "orange"): random.uniform(-1,1)
}
# Chaque couple (type1, type2) a son coefficient unique
print(FORCE_MATRIX)
# Création de la fenêtre
screen = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("Cells Simulation")
''' ------------ Classes ------------ '''
class Ball:
def __init__(self, x, y, color, radius, type_):
self.x = x
self.y = y
self.color = color
self.radius = radius
self.type = type_ # "red", "green", "blue"
self.x_vel = random.uniform(-MAX_SPEED, MAX_SPEED)
self.y_vel = random.uniform(-MAX_SPEED, MAX_SPEED)
def draw(self):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
def move(self, balls):
"""Déplacement avec attraction et correction de chevauchement"""
force_x, force_y = 0, 0
for ball in balls:
if ball == self:
continue
dx = ball.x - self.x
dy = ball.y - self.y
distance = math.sqrt(dx ** 2 + dy ** 2) + 1e-5 # Évite division par zéro
min_distance = self.radius + ball.radius # Distance minimale sans chevauchement
# Déterminer la force d'interaction en fonction des couleurs
attraction_force = FORCE_MATRIX.get((self.type, ball.type), 0)
if distance < ATTRACTION_DIST:
force_x += attraction_force * dx / distance
force_y += attraction_force * dy / distance
# Répulsion à courte distance
if distance < REPULSION_DIST:
repulsion = REPULSION_FORCE * (1 - (distance / REPULSION_DIST))
force_x -= repulsion * dx / distance
force_y -= repulsion * dy / distance
# Répulsion forte si trop proches
if distance < CLOSE_REPULSION_DIST:
close_repulsion = CLOSE_REPULSION_FORCE * (1 - (distance / CLOSE_REPULSION_DIST)) ** 2
force_x -= close_repulsion * dx / distance
force_y -= close_repulsion * dy / distance
# **Correction de superposition** : déplacement forcé si chevauchement
if distance < min_distance:
overlap = min_distance - distance
correction_x = (overlap * dx / distance) / 2
correction_y = (overlap * dy / distance) / 2
self.x -= correction_x
self.y -= correction_y
ball.x += correction_x
ball.y += correction_y
# Mise à jour de la vitesse et de la position
self.x_vel = (self.x_vel + force_x) * FRICTION
self.y_vel = (self.y_vel + force_y) * FRICTION
# Appliquer la vitesse maximale
speed = math.sqrt(self.x_vel ** 2 + self.y_vel ** 2)
if speed > MAX_SPEED:
self.x_vel = (self.x_vel / speed) * MAX_SPEED
self.y_vel = (self.y_vel / speed) * MAX_SPEED
self.x += self.x_vel
self.y += self.y_vel
# Rebond contre les bords
if self.x - self.radius < 0:
self.x = screenWidth - self.radius - 1
if self.x + self.radius > screenWidth:
self.x = 0 + self.radius + 1
if self.y - self.radius < 0:
self.y = screenHeight - self.radius - 1
if self.y + self.radius > screenHeight:
self.y = 0 + self.radius + 1
''' ------------ Fonctions ------------ '''
def add_balls(nb_red, nb_green, nb_blue, nb_orange, nb_purple, radius):
"""Ajoute des balles rouges, vertes et bleues"""
balls = []
for _ in range(nb_red):
x, y = random.randint(radius, screenWidth - radius), random.randint(radius, screenHeight - radius)
balls.append(Ball(x, y, red, radius, "red"))
for _ in range(nb_green):
x, y = random.randint(radius, screenWidth - radius), random.randint(radius, screenHeight - radius)
balls.append(Ball(x, y, green, radius, "green"))
for _ in range(nb_blue):
x, y = random.randint(radius, screenWidth - radius), random.randint(radius, screenHeight - radius)
balls.append(Ball(x, y, blue, radius, "blue"))
for _ in range(nb_orange):
x, y = random.randint(radius, screenWidth - radius), random.randint(radius, screenHeight - radius)
balls.append(Ball(x, y, orange, radius, "orange"))
for _ in range(nb_purple):
x, y = random.randint(radius, screenWidth - radius), random.randint(radius, screenHeight - radius)
balls.append(Ball(x, y, purple, radius, "purple"))
return balls
''' ------------ Boucle principale ------------ '''
balls = add_balls(10, 10, 10, 10, 10, 5)
running = True
clock = pygame.time.Clock()
while running:
screen.fill((12, 24, 39))
for ball in balls:
ball.move(balls)
ball.draw()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
clock.tick(60)
pygame.quit()