-
Notifications
You must be signed in to change notification settings - Fork 0
/
organism.py
123 lines (101 loc) · 5.44 KB
/
organism.py
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 pymunk
import pygame
"""
"""
class Organsim:
def __init__(self, space, width, height):
self.height = height
self.width = width
self.density = 1
self.friction = 3
self.elasticity = 1
self.pos = (int(width*0.1), int(height*0.7))
self.mainbody = pymunk.Body()
self.mainbody.position = self.pos
self.size = (int(width/50), int(height/4))
self.mainbody_vis = pymunk.Poly.create_box(self.mainbody, self.size)
self.mainbody_vis.color = (255,255,0,100)
self.mainbody_vis.mass = self.size[0] * self.size[1] * self. density
self.mainbody_vis.elasticity = self.elasticity
self.mainbody_vis.friction = self.friction
self.mainbody_vis.filter = pymunk.ShapeFilter(1)
self.mainbody_1 = pymunk.Body()
self.mainbody_1.position = self.pos
self.size = (int(width/50), int(height/4))
self.mainbody_1vis = pymunk.Poly.create_box(self.mainbody_1, self.size)
self.mainbody_1vis.color = (0,255, 255,100)
self.mainbody_1vis.mass = self.size[0] * self.size[1] * self. density
self.mainbody_1vis.elasticity = self.elasticity
self.mainbody_1vis.friction = self.friction
self.mainbody_1vis.filter = pymunk.ShapeFilter(1)
y_offset = self.size[1]/2 - 10
self.pivotjoint = pymunk.constraints.PivotJoint(self.mainbody, self.mainbody_1, (self.pos[0] ,self.pos[1] - y_offset) )
self.rotatoryjoint = pymunk.constraints.RotaryLimitJoint(self.mainbody, self.mainbody_1, 0.17, 1.5)
self.bodies = [self.mainbody, self.mainbody_1] #"""Find a better implementation of this"""
space.add(self.mainbody, self.mainbody_vis, self.mainbody_1, self.mainbody_1vis, self.pivotjoint, self.rotatoryjoint)
def getState(self):
"""
Returns a list of tuple of [postion, velocity, rotation, angular_velocity] for each body in the organism
"""
data = []
for body in self.bodies:
#print(new_org.getShapes()[0].body.rotation_vector, new_org.getShapes()[0].body.angular_velocity, new_org.getShapes()[0].body.position, new_org.getShapes()[0].body.velocity)
data.append(body.position.x/self.width)
data.append(body.position.y/self.height)
data.append(body.velocity.x/self.width)
data.append(body.velocity.y/self.height)
data.append(body.rotation_vector.x)
data.append(body.rotation_vector.y)
data.append(body.angular_velocity)
#scaling required
return tuple(data)
def getFitness(self):
x = 0
y = 0
for body in self.bodies:
x += body.position.x
y += body.position.y
return ((x+y)/(len(self.bodies)*2))/self.width
def moveAI(self, ouput, force_multiplier = 1):
force_multiplier = force_multiplier * self.height * self.width
maxop = max(ouput)
maxopid = ouput.index(maxop)
if maxopid == 0:
self.mainbody_1vis.body.apply_impulse_at_local_point((-force_multiplier, 0), (0,0))
if maxopid == 1:
pass
#self.mainbody_1vis.body.apply_impulse_at_local_point((0, force_multiplier), (0,0))
if maxopid == 2:
self.mainbody_1vis.body.apply_impulse_at_local_point((force_multiplier, 0), (0,0))
if maxopid == 3:
pass
#self.mainbody_1vis.body.apply_impulse_at_local_point((0, -force_multiplier), (0,0))
if maxopid == 4:
self.mainbody_vis.body.apply_impulse_at_local_point((-force_multiplier, 0), (0,0))
if maxopid == 5:
pass
#self.mainbody_vis.body.apply_impulse_at_local_point((0, force_multiplier), (0,0))
if maxopid == 6:
self.mainbody_vis.body.apply_impulse_at_local_point((force_multiplier, 0), (0,0))
if maxopid == 7:
pass
#self.mainbody_vis.body.apply_impulse_at_local_point((0, -force_multiplier), (0,0))
def moveHuman(self, event, force_multiplier = 1):
force_multiplier = force_multiplier * self.height * self.width
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
self.mcdcainbody_1vis.body.apply_impulse_at_local_point((-force_multiplier, 0), (0,0))
if event.key == pygame.K_s:
self.mainbody_1vis.body.apply_impulse_at_local_point((0, force_multiplier), (0,0))
if event.key == pygame.K_d:
self.mainbody_1vis.body.apply_impulse_at_local_point((force_multiplier, 0), (0,0))
if event.key == pygame.K_w:
self.mainbody_1vis.body.apply_impulse_at_local_point((0, -force_multiplier), (0,0))
if event.key == pygame.K_LEFT:
self.mainbody_vis.body.apply_impulse_at_local_point((-force_multiplier, 0), (0,0))
if event.key == pygame.K_DOWN:
self.mainbody_vis.body.apply_impulse_at_local_point((0, force_multiplier), (0,0))
if event.key == pygame.K_RIGHT:
self.mainbody_vis.body.apply_impulse_at_local_point((force_multiplier, 0), (0,0))
if event.key == pygame.K_UP:
self.mainbody_vis.body.apply_impulse_at_local_point((0, -force_multiplier), (0,0))