Exemplo n.º 1
0
 def calculatePerspective(self, location: pygame.Vector3):
     translation = pygame.Vector2(self.getLocation().x + self.getWidth() / 2, self.getLocation().y + self.getHeight() / 2)
     #ray = Ray(self.getFocus(), location)
     location = location - self.getLocation()
     ray = Ray(pygame.Vector3(0, 0, -self.__focus_distance),
               pygame.Vector3(location.dot(self.getHorisontal()), location.dot(self.getVertical()), location.dot(self.getFacing())))
     return ray.projection(self.__focus_distance) + translation
Exemplo n.º 2
0
    def __init__(self, position, velocity, min_speed, max_speed, max_force,
                 can_wrap):

        super().__init__()

        # set limits
        self.min_speed = min_speed
        self.max_speed = max_speed
        self.max_force = max_force

        # set position
        dimensions = len(position)
        assert (1 < dimensions < 4), "Invalid spawn position dimensions"

        if dimensions == 2:
            self.position = pg.Vector2(position)
            self.acceleration = pg.Vector2(0, 0)
            self.velocity = pg.Vector2(velocity)
        else:
            self.position = pg.Vector3(position)
            self.acceleration = pg.Vector3(0, 0, 0)
            self.velocity = pg.Vector3(velocity)

        self.heading = 0.0

        self.rect = self.image.get_rect(center=self.position)
Exemplo n.º 3
0
 def __init__(self,
              text: str,
              font_face: str,
              size: int = 1,
              referencePoint: ReferencePoint = ReferencePoint.top_left,
              location: pygame.Vector3 = pygame.Vector3(0, 0, 0),
              facing: pygame.Vector3 = pygame.Vector3(1, 0, 0),
              vertical: pygame.Vector3 = pygame.Vector3(0, 1, 0),
              colour: pygame.Color = pygame.Color(255, 255, 255),
              *args,
              **kwargs):
     super().__init__(location=location,
                      facing=facing,
                      vertical=vertical,
                      *args,
                      **kwargs)
     self.__fontFace = font_face
     self.__fontSize = size
     self.__font = None
     self.__underline = False
     self.__createFont()
     self.__text = text
     self.__colour = colour
     self.__renderedText = None
     self.__renderText()
     self.__referencePoint = referencePoint
Exemplo n.º 4
0
 def __init__(self, camera: Camera,
              location: pygame.Vector3 = pygame.Vector3(0, 0, 0), facing: pygame.Vector3 = pygame.Vector3(1, 0, 0),
              vertical: pygame.Vector3 = pygame.Vector3(0, 1, 0), colour: pygame.Color = pygame.Color(255, 255, 255), *args, **kwargs):
     self.__camera = camera
     super().__init__(function = lambda self: str(self.getReferencedObject("camera").getVelocity().magnitude()),
                      font_face = "freesansbold.ttf", size = 30, objectReferences = {"camera":camera},
                      location = location, facing = facing, vertical = vertical, colour = colour, *args, **kwargs)
Exemplo n.º 5
0
    def __init__(self, position, velocity, min_speed, max_speed, max_force,
                 can_wrap, color, alt_color):

        super().__init__()

        # set limits
        self.min_speed = min_speed
        self.max_speed = max_speed
        self.max_force = max_force

        # set position
        dimensions = len(position)
        assert (1 < dimensions < 4), "Invalid spawn position dimensions"

        if dimensions == 2:
            self.position = pg.Vector2(position)
            self.acceleration = pg.Vector2(0, 0)
            self.velocity = pg.Vector2(velocity)
        else:
            self.position = pg.Vector3(position)
            self.acceleration = pg.Vector3(0, 0, 0)
            self.velocity = pg.Vector3(velocity)

        self.heading = 0.0
        self.rect = self.image.get_rect(center=self.position)
        self.color = color
        self.flash_color = alt_color

        pg.draw.polygon(Vehicle.main_image, pg.Color(color), [(15, 5), (0, 2),
                                                              (0, 8)])
        pg.draw.polygon(Vehicle.alt_image, pg.Color(alt_color),
                        [(15, 5), (0, 2), (0, 8)])
Exemplo n.º 6
0
 def __init__(self,
              location: pygame.Vector3 = pygame.Vector3(0, 0, 0),
              facing: pygame.Vector3 = pygame.Vector3(1, 0, 0),
              vertical: pygame.Vector3 = pygame.Vector3(0, 1, 0),
              **kwargs):
     super().__init__(location=location,
                      facing=facing,
                      vertical=vertical,
                      **kwargs)
Exemplo n.º 7
0
    def update(self, delta_t):
        self.move_forewards(delta_t * self.speed)

        if self.getLocation().x >= 475:
            self.setLocation(pygame.Vector3(475, 25, 25))
            self.setFacing(self.getFacing() * -1)
        elif self.getLocation().x <= 25:
            self.setLocation(pygame.Vector3(25, 25, 25))
            self.setFacing(self.getFacing() * -1)
Exemplo n.º 8
0
 def __init__(self,
              location: pygame.Vector3 = pygame.Vector3(0, 0, 0),
              facing: pygame.Vector3 = pygame.Vector3(1, 0, 0),
              vertical: pygame.Vector3 = pygame.Vector3(0, 1, 0),
              **kwargs):
     super().__init__(**kwargs)
     self.__centre: pygame.Vector3 = location
     self.__facing: pygame.Vector3 = facing.normalize()
     self.__vertical: pygame.Vector3 = vertical.normalize()
Exemplo n.º 9
0
 def __init__(self,
              location=pygame.Vector3(25, 25, 25),
              facing=pygame.Vector3(1, 0, 0),
              vertical: pygame.Vector3 = pygame.Vector3(0, 1, 0),
              scale=50,
              colour=pygame.Color(100, 100, 100),
              speed=100,
              **kwargs):
     super().__init__(location, facing, vertical, scale, colour, speed,
                      **kwargs)
Exemplo n.º 10
0
 def __init__(self,
              location=pygame.Vector3(25, 25, 25),
              facing=pygame.Vector3(1, 0, 0),
              vertical: pygame.Vector3 = pygame.Vector3(0, 1, 0),
              scale=50,
              colour=pygame.Color(100, 100, 100),
              speed=100,
              **kwargs):
     super().__init__(location, facing, vertical, scale, colour, **kwargs)
     self.speed = speed  #np.abs(startSpeed)# Using np.abs returns an int32 not an int - causes an array to be produced when multyplying by a vector
Exemplo n.º 11
0
 def __init__(self, simulation, text: str, font_face: str, size: int = 1, referencePoint: ReferencePoint = ReferencePoint.top_left,
              location: pygame.Vector3 = pygame.Vector3(0, 0, 0), facing: pygame.Vector3 = pygame.Vector3(1, 0, 0),
              vertical: pygame.Vector3 = pygame.Vector3(0, 1, 0), colour: pygame.Color = pygame.Color(255, 255, 255), *args, **kwargs):
     super().__init__(text = text, font_face = font_face, size = size, referencePoint = referencePoint,
                      location = location, facing = facing, vertical = vertical, colour = colour, *args, **kwargs)
     self.__normalColour = colour
     self.__hoverColour = pygame.Color(int(colour.r / 2), int(colour.g / 2), int(colour.b / 2))
     self.__lastHovered = False
     self.onClick = Event()
     simulation.onClick += self.checkClick
Exemplo n.º 12
0
 def __init__(self,
              location=pygame.Vector3(25, 25, 25),
              facing=pygame.Vector3(1, 0, 0),
              width=50,
              height=50,
              colour=pygame.Color(100, 100, 100),
              speed=100,
              **kwargs):
     super().__init__(location, facing, width, height, colour, **kwargs)
     self.speed = speed
Exemplo n.º 13
0
 def __init__(self,
              location=pygame.Vector3(0, 0, 0),
              initial_velocity=pygame.Vector3(0, 0, 0),
              **kwargs):
     super().__init__(
         radius=6.96 * 10**8,  # The Sun's equatorial radius in m
         temperature=5700,  # Surface tempriture in K
         mass=1.989 * 10**30,  # Mass of the Sun in Kg
         initial_velocity=initial_velocity,
         initial_angular_velocity=360 / (
             60 * 60 * 24 * 25.05
         ),  # roatates (siderialy) once at the equator every 25.05 Earth days
         location=location,
         **kwargs)
Exemplo n.º 14
0
 def __init__(self,
              location: pygame.Vector3 = pygame.Vector3(0, 0, 0),
              facing: pygame.Vector3 = pygame.Vector3(1, 0, 0),
              vertical: pygame.Vector3 = pygame.Vector3(0, 1, 0),
              radius: float = 1,
              colour: pygame.Color = pygame.Color(255, 255, 255),
              **kwargs):
     super().__init__(location=location,
                      facing=facing,
                      vertical=vertical,
                      **kwargs)
     self.__points: list = points
     self.__radius = radius
     self.__colour = colour
Exemplo n.º 15
0
 def __init__(self, sun, location=None, initial_velocity=None, **kwargs):
     super().__init__(
         radius=3.3895 * 10**6,  # Equatorial radius in m
         mass=6.39 * 10**23,  # Mass in Kg
         parentStar=sun,
         initial_velocity=pygame.Vector3(0, 0, 2.4007 *
                                         10**4) if initial_velocity is None
         else initial_velocity,  # Orbital velocity in m/s
         initial_angular_velocity=360 /
         (88642.6848
          ),  # Roatates (siderialy) once at the equator every 23.56 hours
         location=pygame.Vector3(2.1739 * 10**11, 0, 0)
         if location is None else location,  # Distance from the Sun in m
         colour=pygame.Color(100, 40, 0),  # Arbitrary colour
         **kwargs)
Exemplo n.º 16
0
 def __init__(self, sun, location=None, initial_velocity=None, **kwargs):
     super().__init__(
         radius=1.737 * 10**6,  # Equatorial radius in m
         mass=5.972 * 10**24,  # Mass in Kg
         parentStar=sun,
         initial_velocity=pygame.Vector3(0, 0, 2.997930184 *
                                         10**4) if initial_velocity is None
         else initial_velocity,  # Orbital velocity in m/s
         initial_angular_velocity=360 /
         (60 * 60 * 23.56
          ),  # Roatates (siderialy) once at the equator every 23.56 hours
         location=pygame.Vector3(1.496 * 10**11, 0, 0)
         if location is None else location,  # Distance from the Sun in m
         colour=pygame.Color(0, 255, 0),  # Arbitrary colour
         **kwargs)
Exemplo n.º 17
0
 def __init__(self, sun, location=None, initial_velocity=None, **kwargs):
     super().__init__(
         radius=2.4397 * 10**6,  # Equatorial radius in m
         mass=4.8675 * 10**24,  # Mass in Kg
         parentStar=sun,
         initial_velocity=pygame.Vector3(0, 0, 3.502 *
                                         10**4) if initial_velocity is None
         else initial_velocity,  # Orbital velocity in m/s
         initial_angular_velocity=360 /
         (60 * 60 * 24 *
          243.025),  # Roatates (siderialy) once at the equator every ...
         location=pygame.Vector3(1.08208 * 10**11, 0, 0)
         if location is None else location,  # Distance from the Sun in m
         colour=pygame.Color(0, 0, 255),  # Arbitrary colour
         **kwargs)
Exemplo n.º 18
0
 def __init__(self, sun, location=None, initial_velocity=None, **kwargs):
     super().__init__(
         radius=6.0518 * 10**6,  # Equatorial radius in m
         mass=3.3011 * 10**23,  # Mass in Kg
         parentStar=sun,
         initial_velocity=pygame.Vector3(0, 0, 4.7362 *
                                         10**4) if initial_velocity is None
         else initial_velocity,  # Orbital velocity in m/s
         initial_angular_velocity=360 /
         (60 * 60 * 24 *
          58.646),  # Roatates (siderialy) once at the equator every ...
         location=pygame.Vector3(5.790905 * 10**10, 0, 0)
         if location is None else location,  # Distance from the Sun in m
         colour=pygame.Color(255, 150, 0),  # Arbitrary colour
         **kwargs)
Exemplo n.º 19
0
 def __init__(self, sun, location=None, initial_velocity=None, **kwargs):
     super().__init__(
         radius=2.462 * 10**7,  # Equatorial radius in m
         mass=1.024 * 10**26,  # Mass in Kg
         parentStar=sun,
         initial_velocity=pygame.Vector3(0, 0, 5.43 *
                                         10**3) if initial_velocity is None
         else initial_velocity,  # Orbital velocity in m/s
         initial_angular_velocity=360 /
         (57924
          ),  # Roatates (siderialy) once at the equator every 23.56 hours
         location=pygame.Vector3(4.476 * 10**12, 0, 0)
         if location is None else location,  # Distance from the Sun in m
         colour=pygame.Color(50, 50, 200),  # Arbitrary colour
         **kwargs)
Exemplo n.º 20
0
 def __init__(self, sun, location=None, initial_velocity=None, **kwargs):
     super().__init__(
         radius=2.536 * 10**7,  # Equatorial radius in m
         mass=8.681 * 10**25,  # Mass in Kg
         parentStar=sun,
         initial_velocity=pygame.Vector3(0, 0, 6.80 *
                                         10**3) if initial_velocity is None
         else initial_velocity,  # Orbital velocity in m/s
         initial_angular_velocity=360 /
         (61992
          ),  # Roatates (siderialy) once at the equator every 23.56 hours
         location=pygame.Vector3(2.962 * 10**12, 0, 0)
         if location is None else location,  # Distance from the Sun in m
         colour=pygame.Color(0, 255, 170),  # Arbitrary colour
         **kwargs)
Exemplo n.º 21
0
 def __init__(self, sun, location=None, initial_velocity=None, **kwargs):
     super().__init__(
         radius=71.492 * 10**6,  # Equatorial radius in m
         mass=1.8982 * 10**27,  # Mass in Kg
         parentStar=sun,
         initial_velocity=pygame.Vector3(0, 0, 12.6 *
                                         10**3) if initial_velocity is None
         else initial_velocity,  # Orbital velocity in m/s
         initial_angular_velocity=360 /
         (35730
          ),  # Roatates (siderialy) once at the equator every 23.56 hours
         location=pygame.Vector3(7.7607 * 10**11, 0, 0)
         if location is None else location,  # Distance from the Sun in m
         colour=pygame.Color(255, 100, 10),  # Arbitrary colour
         **kwargs)
Exemplo n.º 22
0
 def __init__(self, sun, location=None, initial_velocity=None, **kwargs):
     super().__init__(
         radius=1.188 * 10**6,  # Equatorial radius in m
         mass=1.303 * 10**22,  # Mass in Kg
         parentStar=sun,
         initial_velocity=pygame.Vector3(0, 0, 4.743 *
                                         10**3) if initial_velocity is None
         else initial_velocity,  # Orbital velocity in m/s
         initial_angular_velocity=360 /
         (552960
          ),  # Roatates (siderialy) once at the equator every 23.56 hours
         location=pygame.Vector3(5.906 * 10**12, 0, 0)
         if location is None else location,  # Distance from the Sun in m
         colour=pygame.Color(40, 0, 0),  # Arbitrary colour
         **kwargs)
Exemplo n.º 23
0
 def __init__(self,
              length: float = 1,
              point1: pygame.Vector3 = pygame.Vector3(0, 0, 0),
              point2: pygame.Vector3 = pygame.Vector3(1, 0, 0),
              colour: pygame.Color = pygame.Color(255, 255, 255),
              point1_is_midpoint: bool = False,
              **kwargs):
     facing = (point2 - point1).normalize()
     super().__init__(location=point1,
                      facing=facing,
                      vertical=pygame.Vector3(0, 0, 1).cross(facing),
                      **kwargs)
     self.__length = length
     self.__colour = colour
     self.__fromMidpoint = point1_is_midpoint
Exemplo n.º 24
0
def runExampleSim():
    # Create a simulation object
    sim = Simulation()

    # Alter the properties of the camera
    camera = sim.getCamera()
    camera.setLocation(pygame.Vector3(0, 0, -200))
    #camera.setLocation(pygame.Vector3(-camera.getWidth() / 2, -camera.getHeight() / 2, -200))
    camera.setFov(np.pi / 2)

    # Add new layers
    sim.addLayer("test_3D",
                 Layer_3D(pygame.Surface((500, 500), pygame.SRCALPHA)))

    # Add entities to the "defult" layer
    #sim.getLayer("defult").addEntity("test", HUDSimpleSquare())

    ## Add entities to the "test_3D" layer
    #layer_3D = sim.getLayer("test_3D")
    #layer_3D.addEntity("test_moving_object", MovingCube(pygame.Vector3(475, 25, 25), pygame.Vector3(-1, 0, 0), colour = pygame.Color(255, 0, 0, 175)))
    ##layer_3D.addEntity("test_moving_object", MovingCube(pygame.Vector3(475, 25, 25), pygame.Vector3(-1, 0, 0), colour = pygame.Color(255, 0, 0, 175)))
    #layer_3D.addEntity("test_static_object", BoringCube(pygame.Vector3(475, 25, 25), pygame.Vector3(-1, 0, 0), colour = pygame.Color(255, 0, 0, 175)))

    # Add entities to the "test_3D" layer
    layer_3D = sim.getLayer("test_3D")
    addBooringCube(layer_3D, (0, 0, 0), "0")
    addBooringCube(layer_3D, (50, 0, 0), "1")
    addBooringCube(layer_3D, (0, 50, 0), "2")
    addBooringCube(layer_3D, (50, 50, 0), "3")
    addBooringCube(layer_3D, (100, 0, 0), "4")
    addBooringCube(layer_3D, (100, 50, 0), "5")

    sim.run()
Exemplo n.º 25
0
def projection(vec3, camera, rot_x, rot_y, kite=False, rot_x_kite=0):
    cos_rx = cos(radians(rot_x))
    sin_rx = sin(radians(rot_x))
    cos_ry = cos(radians(rot_y))
    sin_ry = sin(radians(rot_y))
    cos_rx_k = cos(radians(rot_x_kite))
    sin_rx_k = sin(radians(rot_x_kite))
    x = vec3.x - camera.x
    y = vec3.y - camera.y
    z = vec3.z - camera.z

    x = x * cos_ry + z * sin_ry
    z = x * -sin_ry + z * cos_ry

    y = y * cos_rx + z * -sin_rx
    z = y * sin_rx + z * cos_rx

    if kite:
        x = x * cos_rx_k + z * sin_rx_k
        z = x * -sin_rx_k + z * cos_rx_k

    x = x * ((f * offset_x * 2) / (2 * s_x)) + y * skew
    y = y * ((f * offset_y * 2) / (2 * s_y))
    z = -z

    x = x / z + offset_x
    y = -y / z + offset_y
    return pygame.Vector3(x, y, z)
Exemplo n.º 26
0
 def render(self, surface: pygame.Surface):
     if self.__fromMidpoint:
         startPoint = (self.getLocation() - self.getFacing() *
                       self.__length / 2).elementwise() * pygame.Vector3(
                           surface.get_width(), surface.get_height(), 1)
         endpoint = (self.getLocation() + self.getFacing() * self.__length /
                     2).elementwise() * pygame.Vector3(
                         surface.get_width(), surface.get_height(), 1)
         pygame.draw.line(surface, self.__colour,
                          (startPoint.x, startPoint.y),
                          (endpoint.x, endpoint.y))
     else:
         endpoint = self.getLocation() + self.getFacing() * self.__length
         pygame.draw.line(surface, self.__colour,
                          (self.getLocation().x, self.getLocation().y),
                          (endpoint.x, endpoint.y))
Exemplo n.º 27
0
    def render(self,
               surface: pygame.Surface,
               camera: Camera,
               pixelsPerMeter=1):
        newVertices = []
        for i in range(len(self.__vertices)):
            newVertices.append(
                pygame.Vector3(self.__vertices[i].dot(self.getHorisontal()),
                               self.__vertices[i].dot(self.getVertical()),
                               self.__vertices[i].dot(self.getFacing())))
        for edge in self.__edges:
            if camera.isInView(self.getLocation() + newVertices[edge[0]] *
                               self.__scale) or camera.isInView(
                                   self.getLocation() +
                                   newVertices[edge[1]] * self.__scale):
                startPosition = (camera.calculatePerspective(
                    self.getLocation() + newVertices[edge[0]] *
                    self.__scale)).elementwise() * pixelsPerMeter
                endPosition = (camera.calculatePerspective(
                    self.getLocation() + newVertices[edge[1]] *
                    self.__scale)).elementwise() * pixelsPerMeter

                pygame.draw.line(
                    surface, self.__colour,
                    (startPosition.x, surface.get_height() - startPosition.y),
                    (endPosition.x, surface.get_height() - endPosition.y))
Exemplo n.º 28
0
 def __init__(self,
              vertices: list,
              edges: list,
              location: pygame.Vector3 = pygame.Vector3(0, 0, 0),
              facing: pygame.Vector3 = pygame.Vector3(1, 0, 0),
              vertical: pygame.Vector3 = pygame.Vector3(0, 1, 0),
              scale=1,
              colour: pygame.Color = pygame.Color(255, 255, 255),
              **kwargs):
     super().__init__(vertices=vertices,
                      edges=edges,
                      location=location,
                      facing=facing,
                      vertical=vertical,
                      scale=scale,
                      colour=colour,
                      **kwargs)
Exemplo n.º 29
0
    def __init__(self,
                 location: pygame.Vector3 = pygame.Vector3(0, 0, 0),
                 facing: pygame.Vector3 = pygame.Vector3(1, 0, 0),
                 vertical: pygame.Vector3 = pygame.Vector3(0, 1, 0),
                 scale=1,
                 colour: pygame.Color = pygame.Color(255, 255, 255),
                 **kwargs) -> Renderable_3DWireframe:
        vertices = [
            pygame.Vector3(0, 2 / 3, 0),  # top
            pygame.Vector3(2 / 3, 0, 0),  # front
            #pygame.Vector3(5/3, 0, 0),# front
            pygame.Vector3(0.5, 0, -1 / 3),  # back left
            pygame.Vector3(-0.5, 0, -1 / 3)
        ]  # back right

        edges = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]

        super().__init__(vertices=vertices,
                         edges=edges,
                         location=location,
                         facing=facing,
                         vertical=vertical,
                         scale=scale,
                         colour=colour,
                         **kwargs)
Exemplo n.º 30
0
    def __init__(self, position, velocity, min_speed, max_speed, max_force,
                 can_wrap):

        self.mass = max(rng.normal(scale=.05, loc=.1), 0.001)

        # self.size_mod = int(pow(self.mass, .5)) * 10
        # self.radius = self.size_mod / 2
        self.radius = pow(self.mass, 0.5) * mass_to_size_constant
        # print(size_mod)
        print(
            f"Center: {(int(self.radius), int(self.radius))}, mass {self.mass}"
        )
        self.image = pg.Surface((self.radius * 2, self.radius * 2),
                                pg.SRCALPHA)
        print(self.image)
        pg.draw.circle(surface=self.image,
                       color=pg.Color("White"),
                       center=(int(self.radius), int(self.radius)),
                       radius=self.radius,
                       width=circle_width)
        # self.image = pg.transform.scale(Vehicle.image, (self.size_mod, self.size_mod))
        super().__init__()
        # set limits
        self.min_speed = min_speed
        self.max_speed = max_speed
        self.max_force = max_force

        # set position
        dimensions = len(position)
        assert (1 < dimensions < 4), "Invalid spawn position dimensions"

        if dimensions == 2:
            self.position = pg.Vector2(position)
            self.acceleration = pg.Vector2(0, 0)
            self.velocity = pg.Vector2(velocity)
        else:
            self.position = pg.Vector3(position)
            self.acceleration = pg.Vector3(0, 0, 0)
            self.velocity = pg.Vector3(velocity)

        self.heading = 0.0

        self.rect = self.image.get_rect(center=self.position)
        # self.debug = True
        self.delete = False