-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
120 lines (96 loc) · 4.7 KB
/
utils.py
File metadata and controls
120 lines (96 loc) · 4.7 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
import numpy as np
import pygame
import math
import predator
# Position => a tuple of 2D coordinates
# ListOfCounterCreatures => The list which is to be used to get creatures in the field of view
# Upperbound => the field of view
def PreyFilterUsingEuclideanDistances(Position, ListOfCounterCreatures, Upperbound):
response = []
for animal in ListOfCounterCreatures:
x,y = animal.rect.centerx, animal.rect.centery
distance = ((x-Position[0])**2 + (y-Position[1])**2)**(0.5)
if distance < Upperbound:
if distance != 0:
response.append((animal,(1/distance)))
else:
response.append((animal, float('inf')))
return response
def PredictPredatorDirection(Position, CreaturesAround, behaviourRate):
if len(CreaturesAround)==0:
return pygame.math.Vector2(0, 0)
desiredVelocitylist = []
for details in CreaturesAround:
animal, factor = details
desiredVelocitylist.append([behaviourRate*factor*(animal.rect.x - Position[0]) , behaviourRate*factor*(animal.rect.y - Position[1])])
desiredVelocities = np.array(desiredVelocitylist)
resultant = np.sum(desiredVelocities, axis = 0)
resultantVector2 = pygame.math.Vector2(resultant[0], resultant[1])
target = CreaturesAround[0][0]
maxFactor = CreaturesAround[0][1]
for vel, details in zip(desiredVelocities, CreaturesAround):
animal, factor = details
currVector = pygame.math.Vector2(vel[0], vel[1])
if abs(resultantVector2.angle_to(currVector)) <= 45 and maxFactor < factor:
maxFactor = factor
target = animal
desired = (pygame.math.Vector2(target.rect.x, target.rect.y) - pygame.math.Vector2(Position))*behaviourRate
return desired
def FoodAndPredatorFilterUsingEuclideanDistances(Position, ListOfCounterCreatures, ListOfFood, Upperbound):
preds = []
foods = []
for creature in ListOfCounterCreatures:
x,y = creature.rect.centerx, creature.rect.centery
distance = ((x-Position[0])**2 + (y-Position[1])**2)**(0.5)
if distance < Upperbound:
if distance != 0:
preds.append((creature, (1/distance)))
else:
preds.append((creature, float('inf')))
for food in ListOfFood:
x, y = food.x, food.y
distance = ((x-Position[0])**2 + (y-Position[1])**2)**(0.5)
if distance < Upperbound:
if distance != 0:
foods.append((food,(1/distance)))
else:
foods.append((food, float('inf')))
return foods, preds
def PredictPreyDirection(Position, CreaturesAround, FoodAround, foodBehaviour, creatureBehaviour):
if len(CreaturesAround) == 0 and len(FoodAround) == 0:
return pygame.math.Vector2(0, 0)
desiredVelocityCreaturelist = [[0,0]]
for details in CreaturesAround:
animal, factor = details
desiredVelocityCreaturelist.append([creatureBehaviour*factor*(animal.rect.x - Position[0]) , creatureBehaviour*factor*(animal.rect.y - Position[1])])
desiredVelocityFoodlist = [[0, 0]]
for details in FoodAround:
food, factor = details
desiredVelocityFoodlist.append([foodBehaviour*factor*(food.x - Position[0]), foodBehaviour*factor*(food.y - Position[1])])
desiredVelocitiesCreature = np.array(desiredVelocityCreaturelist)
desiredVelocitiesFood = np.array(desiredVelocityFoodlist)
resultant1 = np.sum(desiredVelocitiesCreature, axis = 0)
resultant2 = np.sum(desiredVelocitiesFood, axis = 0)
resultantVector2 = pygame.math.Vector2(resultant1[0] + resultant2[0], resultant1[1] + resultant2[1])
if(len(CreaturesAround) != 0):
target = CreaturesAround[0][0]
maxFactor = CreaturesAround[0][1]
else:
target = FoodAround[0][0]
maxFactor = FoodAround[0][1]
for vel, details in zip(desiredVelocitiesCreature, CreaturesAround):
animal, factor = details
currVector = pygame.math.Vector2(vel[0], vel[1])
if abs(resultantVector2.angle_to(currVector)) <= 45 and maxFactor < factor:
maxFactor = factor
target = animal
for vel, details in zip(desiredVelocitiesFood, FoodAround):
food, factor = details
currVector = pygame.math.Vector2(vel[0], vel[1])
if abs(resultantVector2.angle_to(currVector)) <= 45 and maxFactor < factor:
maxFactor = factor
target = food
if type(target) == predator.Predator:
return (pygame.math.Vector2(target.rect.x, target.rect.y) - pygame.math.Vector2(Position))*creatureBehaviour
else:
return (pygame.math.Vector2(target.x, target.y) - pygame.math.Vector2(Position))*foodBehaviour