Skip to content

Commit

Permalink
Merge branch 'development' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasfremming authored Nov 3, 2024
2 parents d9ab0ef + 555a557 commit bf3be20
Show file tree
Hide file tree
Showing 21 changed files with 1,244 additions and 583 deletions.
134 changes: 102 additions & 32 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,115 @@
from typing import Protocol
from enum import Enum
import numpy
import random

import pygame
import pymunk
from pygame.locals import *

from src.genome import Genome
from src.environment import Environment
from src.renderObject import RenderObject
from src.globals import SCREEN_WIDTH, SCREEN_HEIGHT
from src.environment import Environment, GroundType
from src.render_object import RenderObject
from src.agent_parts.rectangle import Point
from src.globals import (
SCREEN_WIDTH,
SCREEN_HEIGHT,
FONT_SIZE,
SEGMENT_WIDTH,
BLACK,
RED
)

from src.agent_parts.limb import Limb, LimbType, limb_factory
from src.agent_parts.creature import Creature, creature_factory
from src.agent_parts.rectangle import Rectangle, rectangle_factory, Point
from src.agent_parts.joint import Joint, joint_factory

if __name__ =="__main__":
import pygame
from src.agent_parts.creature import Creature

GRAVITY = 9.81
def main():
# 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")


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

rect = rectangle_factory(point, 50, 50)
limb = limb_factory(rect, 3, 2)
angle = 0,'',
joint = joint_factory(angle, Point(125, 125))
limb.addJoint(joint)
aurelius = creature_fatory(None, [limb])
pygame.init()
window = pygame.display.set_mode((800, 600))
a = 0
while active:

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

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)
limb3 = creature.add_limb(110, 60, (400, 100), mass=5)

# Add a motor between limbs
creature.add_motor(limb1, limb2, (50, 0), (-25, 0), rate=-2, tolerance=30)
creature.add_motor(limb2, limb3, (37, 0), (-23, 0), rate=2, tolerance=50)

clock = pygame.time.Clock()
vision_y = 100
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
active = False
window.fill((0, 0, 0))
pygame.draw.rect(window, (255, 0, 0), (0, 0, 800, 600))
aurelius.render(window)
a+= 0.001
for limb in aurelius.limblist:
limb.rotate(a)
pygame.display.flip()
pygame.time.delay(10)
if event.type == QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("Left arrow pressed")
if event.key == pygame.K_RIGHT:
print("Right arrow pressed")

space.step(1/60.0)

screen.fill((135, 206, 235))

environment.update()
environment.render()

vision_y += random.randint(-1, 1)

match environment.ground_type:
case GroundType.BASIC_GROUND:
environment.vision.update(
environment.screen,
Point(environment.starting_xx, vision_y),
environment.ground,
environment.offset)

case GroundType.PERLIN:
self.vision.update(
self.screen,
Point(self.starting_xx, vision_y),
self.ground,
0)
#creature.set_joint_rates([random.random()*2, random.random()*2])
# Render the creature
creature.render(screen)


pygame.quit()


clock.tick(60)

pygame.display.flip()

pygame.quit()


if __name__ == "__main__":
main()
# pygame.init()
# font = pygame.font.Font(pygame.font.get_default_font(), FONT_SIZE)

# space = pymunk.Space()
# space.gravity = (0, 981) # Gravity pointing downward

# # Create the screen
# screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# environment = Environment(screen, space)
# # Start the main loop
# environment.run()
90 changes: 90 additions & 0 deletions pymunkJointTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import sys, random
random.seed(1) # make the simulation the same each time, easier to debug
import pygame
import pymunk
import pymunk.pygame_util

def add_ball(space):
"""Add a ball to the given space at a random position"""
mass = 3
radius = 25
inertia = pymunk.moment_for_circle(mass, 0, radius, (0,0))
body = pymunk.Body(mass, inertia)
x = random.randint(120,300)
body.position = x, 50
shape = pymunk.Circle(body, radius, (0,0))
shape.friction = 1
space.add(body, shape)
return shape

def add_L(space):
"""Add a inverted L shape with two joints"""
rotation_center_body = pymunk.Body(body_type = pymunk.Body.STATIC)
rotation_center_body.position = (300,300)

rotation_limit_body = pymunk.Body(body_type = pymunk.Body.STATIC)
rotation_limit_body.position = (200,300)

body = pymunk.Body(10, 10000)
body.position = (300,300)
l1 = pymunk.Segment(body, (-150, 0), (255.0, 0.0), 5.0)
l2 = pymunk.Segment(body, (-150.0, 0), (-150.0, -50.0), 5.0)
l1.friction = 1
l2.friction = 1
l1.mass = 8
l2.mass = 1

rotation_center_joint = pymunk.PinJoint(body, rotation_center_body, (0,0), (0,0))
joint_limit = 25
rotation_limit_joint = pymunk.SlideJoint(body, rotation_limit_body, (-100,0), (0,0), 0, joint_limit)

space.add(l1, l2, body, rotation_center_joint, rotation_limit_joint)
return l1,l2

def main():
pygame.init()
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Joints. Just wait and the L will tip over")
clock = pygame.time.Clock()

space = pymunk.Space()
space.gravity = (0.0, 900.0)

lines = add_L(space)
balls = []
draw_options = pymunk.pygame_util.DrawOptions(screen)

ticks_to_next_ball = 10
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
sys.exit(0)

ticks_to_next_ball -= 1
if ticks_to_next_ball <= 0:
ticks_to_next_ball = 25
ball_shape = add_ball(space)
balls.append(ball_shape)

screen.fill((255,255,255))

balls_to_remove = []
for ball in balls:
if ball.body.position.y > 550:
balls_to_remove.append(ball)

for ball in balls_to_remove:
space.remove(ball, ball.body)
balls.remove(ball)

space.debug_draw(draw_options)

space.step(1/50.0)

pygame.display.flip()
clock.tick(50)

if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# """Init file for src folder"""

# from . import Enviroment, RenderObject, agent_parts
# from . import agent_parts #Enviroment, RenderObject,

# __all__ = ["Enviroment", "RenderObject", "agent_parts"]
# __all__ = ["agent_parts"] #"Enviroment", "RenderObject",
18 changes: 6 additions & 12 deletions src/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
from enum import Enum
import tensorflow as tf

from src.enviroment import Enviroment
from src.renderObject import RenderObject
from src.genome import Genome
from src.agent_parts.creature import Creature
class LimbType(Enum): """ Summary: Enum for the different types of limbs.
"""
FOOT = 1
LEG = 2
LIMB = 3
from src.environment import Environment
from src.render_object import RenderObject

from src.agent_parts_old.limb import Limb, LimbType, limb_factory
from src.agent_parts_old.creature import Creature, creature_factory
from CrawlAI.src.agent_parts.rectangle import Rectangle, rectangle_factory


class Agent(ABC):
Expand Down Expand Up @@ -53,7 +51,3 @@ def get_enviroment_state(self, env) -> tf.Tensor:
"""
pass





Loading

0 comments on commit bf3be20

Please sign in to comment.