Skip to content

Commit

Permalink
bug: fix main bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniavm committed Nov 5, 2024
2 parents a3a5bf7 + 2cb9652 commit 4b2a960
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 61 deletions.
38 changes: 9 additions & 29 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
def create_creatures(amount, space):
creatures = []
for i in range(amount):
creature = Creature(space)
creature: Creature = Creature(space)
# Add limbs to the creature, placing them above the ground
limb1 = creature.add_limb(100, 60, (300, 100), mass=1)
limb2 = creature.add_limb(100, 20, (350, 100), mass=1)
Expand All @@ -45,13 +45,13 @@ def create_creatures(amount, space):
limb2,
(50, 0),
(-25, 0),
rate=-2, tolerance=30)
rate=0, tolerance=30)
creature.add_motor(
limb2,
limb3,
(37, 0),
(-23, 0),
rate=2,
rate=0,
tolerance=50)

creatures.append(creature)
Expand All @@ -73,31 +73,22 @@ def create_population(population_size, creature: Creature):
return population

def main():

genetic_algorithm = GeneticAlgorithm()

# Initialize Pygame and Pymunk

pygame.init()
screen_width, screen_height = SCREEN_WIDTH, SCREEN_HEIGHT
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pymunk Rectangle Physics")
interface = Interface()

# Track whether physics is on or off
physics_on = False
physics_value = 0

# Set up the Pymunk space
space = pymunk.Space()
space.gravity = (0, 981) # Gravity pointing downward

environment = Environment(screen, space)
environment: Environment = Environment(screen, space)
environment.ground_type = GroundType.BASIC_GROUND

population_size = 10
creatures = create_creatures(population_size, space)
creature_instance = creatures[0]
creatures: list[Creature] = create_creatures(population_size, space)
creature_instance: Creature = creatures[0]
population = create_population(population_size, creature_instance)
neat_networks: list[NEATNetwork] = []
for genome in population:
Expand All @@ -117,24 +108,13 @@ def main():
print("Left arrow pressed")
if event.key == pygame.K_RIGHT:
print("Right arrow pressed")
if event.key == pygame.K_SPACE:
handle_physics()

space.step(physics_value)
space.step(1/60.0)
screen.fill((135, 206, 235))
environment.update()
environment.render()

#creature.set_joint_rates([random.random()*2, random.random()*2])
# Render the creature
creature.render(screen)

if not physics_on:
interface.add_button(limb_button)
else:
interface.remove_button(limb_button)
interface.render(screen)


# TODO: vision should be part of a creature, and not environment
inputs = np.array([environment.vision.get_near_periphery().x,
environment.vision.get_near_periphery().y,
Expand All @@ -151,7 +131,7 @@ def main():
inputs = np.append(inputs, limb.body.position.y)

outputs = neat_networks[index].forward(inputs)
creature.set_joint_rates(outputs)
#creature.set_joint_rates(outputs)

vision_y = round(creature_instance.limbs[0].body.position.y)
vision_x = round(creature_instance.limbs[0].body.position.x)
Expand Down
9 changes: 8 additions & 1 deletion src/agent_parts/creature.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@


class Creature:

limbs: list[Limb]
motors: list[MotorJoint]

def __init__(self, space):
"""Initialize a creature with an empty list of limbs and motors."""
self.space = space
Expand Down Expand Up @@ -46,6 +48,11 @@ def add_motor_on_limbs(self, limb_a: Limb, limb_b: Limb, position: tuple[float,
print("false")
return None

def local_to_global(self, limb: Limb, anchor: tuple[float,float]) -> tuple[float,float]:
"""Convert a local anchor point to a global anchor point."""
return limb.body.local_to_world(anchor)



def add_motor(self, limb_a: Limb, limb_b: Limb, anchor_a: tuple[float,float], anchor_b: tuple[float,float], rate = 0.0, tolerance = 30) -> MotorJoint|None:
"""Add a motor connecting two limbs."""
Expand Down
2 changes: 1 addition & 1 deletion src/agent_parts/limb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class Limb:
def __init__(self, space, width, height, position, mass=1, color=(0, 255, 0)):
def __init__(self, space, width, height, position, mass=3, color=(0, 255, 0)):
"""Initialize a limb as a rectangular body."""
self.width = width
self.height = height
Expand Down
2 changes: 1 addition & 1 deletion src/agent_parts/motorjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def render(self, screen, body_a, body_b):
# Draw a line connecting the two bodies
pygame.draw.circle(
surface=screen,
color=(255, 0, 0),
color=(255, 0, 0),
center=(int(pos_a_world.x),
int(pos_a_world.y)),
radius=3)
Expand Down
10 changes: 8 additions & 2 deletions src/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,14 @@ def update(
self.eye_position = eye_position
x1 = eye_position.x + self.x_offset
x2 = x1 + self.sight_width
y1=ground.get_y(x1+scroll_offset)
y2=ground.get_y(x2+scroll_offset)
try:
y1=ground.get_y(x1+scroll_offset)
except:
pass
try:
y2=ground.get_y(x2+scroll_offset)
except:
pass
if not y1 is None:
self.near_periphery = Point(x1, y1)
if not y2 is None:
Expand Down
29 changes: 2 additions & 27 deletions src/game_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,5 @@


class GameLoop:

def scene_graph(
self,
scene_node: RenderObject,
transformations: list
) -> None:

"""
A method that traverses the scene graph and applies '
transformations to the nodes.
Parameters:
----------
scene_node : RenderObject
The root node of the scene graph.
transformations : list
A list of transformations to apply to the nodes.
"""
for transformation in transformations:
transformation.apply(scene_node)

scene_node.render()

for child in scene_node.children:
self.scene_graph(child, transformations)




5 changes: 5 additions & 0 deletions src/ground.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ def get_first_shifted_point(self) -> tuple[int, int]:
scroll_offset = self.start_x-self.poly.body.position[0]
return (points[0][0] - scroll_offset, points[0][1])

def init_pymunk_polygon(self, space) -> None:
body = pymunk.Body(0, 0, 1)
poly = pymunk.Poly(body, self.points, radius=0.0)
space.add(body, poly)

class BasicGround(RenderObject, Ground, BasicSegment):
def __init__(self, screen: pg.display, space: pymunk.space, segment_width: int) -> None:
self.screen = screen
Expand Down

0 comments on commit 4b2a960

Please sign in to comment.