Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- import math
- # Parse data into arrays
- areas = []
- vertices = []
- manipulators = []
- # SHIP TYPE 1
- # data = [
- # (1, 4, 3, 2), (4, 6, 8, 3), (2, 3, 8, 7), (1, 2, 7, 5),
- # (6, 5, 7, 8), (5, 6, 4, 1), (9, 10, 11, 12), (9, 12, 11, 10),
- # (13, 14, 15, 16), (13, 16, 15, 14)
- # ]
- # for d in data:
- # areas.append(d)
- # vertices_data = [
- # (-6, 6, -6, 1), (-6, -6, -6, 1), (6, -6, -6, 1), (6, 6, -6, 1),
- # (-6, 6, 6, 1), (6, 6, 6, 1), (-6, -6, 6, 1), (6, -6, 6, 1),
- # (-10, 0, -15, 1), (10, 0, -15, 1), (10, 0, -50, 2), (-10, 0, -50, 2),
- # (-10, 0, 15, 1), (-10, 0, 50, 3), (10, 0, 50, 3), (10, 0, 15, 1)
- # ]
- # Define ship data "p51"
- areas = [
- [13, 11, 12, 14], [11, 9, 6, 12], [10, 13, 14, 8], [10, 9, 11, 13],
- [14, 12, 6, 8], [9, 23, 5, 6], [24, 10, 8, 7], [5, 7, 8, 6],
- [24, 23, 9, 10], [6, 2, 1, 5], [5, 1, 2, 6], [4, 8, 7, 3],
- [3, 7, 8, 4], [28, 30, 29, 26], [30, 28, 27, 29], [25, 28, 26, 25],
- [28, 25, 27, 28], [23, 15, 17, 5], [16, 24, 7, 18], [7, 5, 17, 18],
- [16, 15, 23, 24], [31, 22, 15, 32], [22, 31, 32, 16], [22, 21, 20, 19],
- [19, 20, 21, 22], [15, 35, 33, 32], [32, 33, 35, 15], [36, 16, 32, 34],
- [34, 32, 16, 36]
- ]
- vertices_data = [
- [1.8, 50.4, -1.8, 2], [-9.9, 52.2, -1.8, 2], [1.8, -50.4, -1.8, 2],
- [-9.9, -52.2, -1.8, 2], [9, 5.4, -8.1, 1], [-16.2, 5.4, -8.1, 1],
- [9, -5.4, -8.1, 1], [-16.2, -5.4, -8.1, 1], [-11.7, 5.4, 5.4, 1],
- [-11.7, -5.4, 5.4, 1], [-33.3, 3.6, 4.5, 3], [-33.3, 3.6, -5.4, 3],
- [-33.3, -3.6, 4.5, 3], [-33.3, -3.6, -5.4, 3], [44.1, 1.8, 3.6, 3],
- [44.1, -1.8, 3.6, 3], [44.1, 1.8, -2.7, 3], [44.1, -1.8, -2.7, 3],
- [44.1, 0, -2.7, 3], [50.4, 0, 0, 3], [46.8, 0, 15.3, 3], [44.1, 0, 15.3, 3],
- [12.6, 5.4, 5.4, 1], [12.6, -5.4, 5.4, 1], [-11.7, 0, 5.4, 1],
- [-3.6, 5.4, 5.4, 1], [-3.6, -5.4, 5.4, 1], [-3.6, 0, 9, 1],
- [12.6, 0, 5.4, 1], [12.6, 0, 7.65, 1], [40.5, 0, 15.3, 3], [35.1, 0, 4.05, 3],
- [36.9, 18, 3.6, 3], [36.9, -18, 3.6, 3], [43.2, 16.2, 3.6, 3],
- [43.2, -16.2, 3.6, 3]
- ]
- # Parsing areas and vertices data
- areas_parsed = [[int(point) for point in area] for area in areas]
- vertices_parsed = [[float(point) for point in vertex] for vertex in vertices_data]
- #print("Areas:", areas_parsed)
- #print("Vertices:", vertices_parsed)
- for vd in vertices_data:
- vertices.append(vd[:3])
- manipulators.append(vd[3])
- # Pygame setup
- pygame.init()
- WIDTH, HEIGHT = 800, 600
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- clock = pygame.time.Clock()
- # Projection parameters
- fov = 90
- near = 0.1
- far = 1000
- aspect_ratio = WIDTH / HEIGHT
- # Rotation parameters
- angle_x = 0
- angle_y = 0
- angle_z = 0
- # Translation parameters
- camera_x = 0
- camera_y = 0
- camera_z = -30
- # Helper function to project 3D coordinates to 2D
- def project(coord):
- x, y, z = coord
- x = x * (fov / z)
- y = y * (fov / z)
- return int(x + WIDTH / 2), int(y + HEIGHT / 2)
- # Helper function to rotate a point around the X-axis
- def rotate_x(point, angle):
- x, y, z = point
- new_y = y * math.cos(angle) - z * math.sin(angle)
- new_z = y * math.sin(angle) + z * math.cos(angle)
- return x, new_y, new_z
- # Helper function to rotate a point around the Y-axis
- def rotate_y(point, angle):
- x, y, z = point
- new_x = x * math.cos(angle) + z * math.sin(angle)
- new_z = -x * math.sin(angle) + z * math.cos(angle)
- return new_x, y, new_z
- # Helper function to rotate a point around the Z-axis
- def rotate_z(point, angle):
- x, y, z = point
- new_x = x * math.cos(angle) - y * math.sin(angle)
- new_y = x * math.sin(angle) + y * math.cos(angle)
- return new_x, new_y, z
- # Main loop
- running = True
- while running:
- screen.fill((0, 0, 0))
- # Apply rotation and translation to vertices
- transformed_vertices = []
- for vertex in vertices:
- rotated = vertex
- rotated = rotate_x(rotated, angle_x)
- rotated = rotate_y(rotated, angle_y)
- rotated = rotate_z(rotated, angle_z)
- translated = (rotated[0] + camera_x, rotated[1] + camera_y, rotated[2] + camera_z)
- transformed_vertices.append(translated)
- # Draw lines between vertices
- for area in areas:
- for i in range(len(area)):
- start = transformed_vertices[area[i] - 1]
- end = transformed_vertices[area[(i + 1) % len(area)] - 1]
- # Check if start and end points are too close to the camera or the projection plane
- # have not successfully been trapped this bug
- #if start[2] <= 0.00001 or end[2] <= 0.00001:
- #continue
- #print(start)
- pygame.draw.line(screen, (255, 255, 255), project(start), project(end), 1)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- # Handle key presses for camera movement
- keys = pygame.key.get_pressed()
- if keys[pygame.K_w]:
- camera_z += 0.1
- if keys[pygame.K_s]:
- camera_z -= 0.1
- if keys[pygame.K_a]:
- camera_x -= 0.1
- if keys[pygame.K_d]:
- camera_x += 0.1
- if keys[pygame.K_r]:
- camera_y += 0.1
- if keys[pygame.K_f]:
- camera_y -= 0.1
- # Rotate the object
- if keys[pygame.K_LEFT]:
- angle_y += 0.02
- if keys[pygame.K_RIGHT]:
- angle_y -= 0.02
- if keys[pygame.K_UP]:
- angle_x += 0.02
- if keys[pygame.K_DOWN]:
- angle_x -= 0.02
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement