Simulation Poisson Sauvegarde
Simulation Poisson Sauvegarde
Simulation Poisson Sauvegarde
"""
"""
import numpy as np
import os
import networkx as nx
import pygame
import sys
import random
import math
thorus=True
os.chdir(new_working_directory)
X=1920
Y=1080
Z=1000
numb_fish = 100
numb_pred = 0
predators_repulsion=1000
threshold_angle = np.radians(70)
turn_probability=20
pygame.display.set_caption("Sliders")
# Colors
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# Slider parameters
slider_width = 200
slider_height = 20
slider_x = 50
slider_spacing = 40
slider_min = 0
slider_max = 500
slider_x = WIDTH - slider_width - 50 # Adjust as needed for spacing from the right edge
# Draw slider
axis=random.randint(1,3)
angle_radians = math.radians(angle)
if axis == 1:
elif axis == 2:
elif axis == 3:
return rotated_vector
def in_vision_field(animal1,animal2,half_angle):
apex_to_point=np.array([animal2.x,animal2.y,animal2.z])-np.array([animal1.x,animal.y,animal.z])
return True
else:
return False
class fish:
speed = 30
color = (255,255,255)
self.x = x
self.y = y
self.z=z
self.direction = direction
self.direction_vectors = []
if np.linalg.norm(self.direction) != 1.0:
dx, dy = self.direction[:2]
vertices = [
class predator:
speed=10
color=(255,0,255)
self.x = x
self.y = y
self.z=z
self.direction = direction
self.direction_vectors=[]
if np.linalg.norm(self.direction) != 1.0:
chance=random.randint(1,10)
if chance==1:
self.direction = rotate_vector(self.direction,30)
elif chance==2:
self.direction = rotate_vector(self.direction,-30)
if np.linalg.norm(total_direction) > 0:
if isinstance(total_direction,np.ndarray):
self.direction = total_direction
self.y = updated_position[1]
self.z = updated_position[2]
if thorus==True:
if self.x>X:
self.x=0
if self.y>Y:
self.y=0
if self.z>Z:
self.z=0
if self.x<0:
self.x=X
if self.y<0:
self.y=Y
if self.z<0:
self.z=Z
else:
if self.x>X:
self.direction[0]=-self.direction[0]
if self.y>Y:
self.direction[1]=-self.direction[1]
if self.z>Z:
self.direction[2]=-self.direction[2]
if self.x<0:
self.direction[0]=-self.direction[0]
if self.y<0:
self.direction[1]=-self.direction[1]
if self.z<0:
self.direction[2]=-self.direction[2]
animals = []
distx = random.randint(1, X)
disty = random.randint(1, Y)
distz = random.randint(1, Z)
for n in range(numb_fish):
distx = random.randint(1, X)
disty = random.randint(1, Y)
distz = random.randint(1, Z)
for n in range(numb_pred):
distx = random.randint(1, X)
disty = random.randint(1, Y)
distz = random.randint(1, Z)
def fish_update(attraction,repulsion,alignment,half_angle):
coordinates = np.array([[animal.x, animal.y, animal.z] for animal in animals])
animal.direction_vectors = []
for i, j in zip(*all_indices):
if isinstance(animals[i], fish):
if isinstance(animals[j],fish):
#repulsion
animals[i].direction_vectors.append(rep)
animals[j].direction_vectors.append(-rep)
#alignement
al = al / np.linalg.norm(al)
animals[i].direction_vectors.append(al)
print(np.linalg.norm(alj))
#attraction
animals[i].direction_vectors.append(attr)
animals[j].direction_vectors.append(-attr)
animals[i].direction_vectors.append(prep)
if isinstance(animals[j],predator) and isinstance(animals[i],fish) and in_vision_field(animals[j],
animals[i], variables['Vision Angle']):
animals[j].direction_vectors.append(patt)
if isinstance(animal,fish):
if np.linalg.norm(total_direction) > 0:
chance=random.randint(1,turn_probability)
total_direction = rotate_vector(animal.direction,29)
elif chance==3 and (thorus==True) or (thorus==False and animals[i].x<X-5 and animals[i].y<Y-5
and animals[i].z<Z-5):
total_direction = rotate_vector(animal.direction,-29)
else:
pass
rotation_axis /= np.linalg.norm(rotation_axis)
if isinstance(total_direction,np.ndarray):
animal.direction = total_direction
animal.x = updated_position[0]
animal.y = updated_position[1]
animal.z = updated_position[2]
if thorus==True:
if animal.x>X:
animal.x=0
if animal.y>Y:
animal.y=0
if animal.z>Z:
animal.z=0
if animal.x<0:
animal.x=X
if animal.y<0:
animal.y=Y
if animal.z<0:
animal.z=Z
else:
if animal.x>X:
animal.direction[0]=-animal.direction[0]
animal.x=X
animals[i].direction_vectors.append(np.array([-1,0,0]))
if animal.y>Y:
animal.direction[1]=-animal.direction[1]
animal.y=Y
animals[i].direction_vectors.append(np.array([0,-1,0]))
if animal.z>Z:
animal.direction[2]=-animal.direction[2]
animal.z=Z
animals[i].direction_vectors.append(np.array([0,0,-1]))
if animal.x<0:
animal.direction[0]=-animal.direction[0]
animal.x=0
animals[i].direction_vectors.append(np.array([1,0,0]))
if animal.y<0:
animal.direction[1]=-animal.direction[1]
animal.y=0
animals[i].direction_vectors.append(np.array([0,1,0]))
if animal.z<0:
animal.direction[2]=-animal.direction[2]
animal.z=0
animals[i].direction_vectors.append(np.array([0,0,1]))
else:
animal.update()
if isinstance(animal,type(fish)):
# Initialize Pygame
pygame.init()
pygame.font.init()
# Font
# Variables
variables = {
'Repulsion': 50,
'Alignment': 50,
'Attraction': 50
pygame.display.set_caption("animals")
# Colors
BLUE = (0,25,50)
clock = pygame.time.Clock()
# Game loop
running = True
while running:
# Handle events
if event.type == pygame.QUIT:
running = False
y = 50 + idx * slider_spacing
screen.fill(BLUE)
fish_update(variables['Attraction'],variables['Repulsion'],variables['Alignment'],variables['Vision
Angle'])
# Draw sliders
y = 50
draw_slider(name, value, y)
y += slider_spacing
animal.draw(screen)
pygame.display.flip()
clock.tick(60)
# Quit Pygame
pygame.quit()
sys.exit()