Advertisement
Guest User

3D to 2D projection SpaceShip Design

a guest
May 14th, 2024
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.53 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import math
  4.  
  5. # Parse data into arrays
  6. areas = []
  7. vertices = []
  8. manipulators = []
  9.  
  10. # SHIP TYPE 1
  11. # data = [
  12. #     (1, 4, 3, 2), (4, 6, 8, 3), (2, 3, 8, 7), (1, 2, 7, 5),
  13. #     (6, 5, 7, 8), (5, 6, 4, 1), (9, 10, 11, 12), (9, 12, 11, 10),
  14. #     (13, 14, 15, 16), (13, 16, 15, 14)
  15. # ]
  16.  
  17. # for d in data:
  18. #     areas.append(d)
  19.  
  20. # vertices_data = [
  21. #     (-6, 6, -6, 1), (-6, -6, -6, 1), (6, -6, -6, 1), (6, 6, -6, 1),
  22. #     (-6, 6, 6, 1), (6, 6, 6, 1), (-6, -6, 6, 1), (6, -6, 6, 1),
  23. #     (-10, 0, -15, 1), (10, 0, -15, 1), (10, 0, -50, 2), (-10, 0, -50, 2),
  24. #     (-10, 0, 15, 1), (-10, 0, 50, 3), (10, 0, 50, 3), (10, 0, 15, 1)
  25. # ]
  26.  
  27. # Define ship data "p51"
  28. areas = [
  29.     [13, 11, 12, 14], [11, 9, 6, 12], [10, 13, 14, 8], [10, 9, 11, 13],
  30.     [14, 12, 6, 8], [9, 23, 5, 6], [24, 10, 8, 7], [5, 7, 8, 6],
  31.     [24, 23, 9, 10], [6, 2, 1, 5], [5, 1, 2, 6], [4, 8, 7, 3],
  32.     [3, 7, 8, 4], [28, 30, 29, 26], [30, 28, 27, 29], [25, 28, 26, 25],
  33.     [28, 25, 27, 28], [23, 15, 17, 5], [16, 24, 7, 18], [7, 5, 17, 18],
  34.     [16, 15, 23, 24], [31, 22, 15, 32], [22, 31, 32, 16], [22, 21, 20, 19],
  35.     [19, 20, 21, 22], [15, 35, 33, 32], [32, 33, 35, 15], [36, 16, 32, 34],
  36.     [34, 32, 16, 36]
  37. ]
  38.  
  39. vertices_data = [
  40.     [1.8, 50.4, -1.8, 2], [-9.9, 52.2, -1.8, 2], [1.8, -50.4, -1.8, 2],
  41.     [-9.9, -52.2, -1.8, 2], [9, 5.4, -8.1, 1], [-16.2, 5.4, -8.1, 1],
  42.     [9, -5.4, -8.1, 1], [-16.2, -5.4, -8.1, 1], [-11.7, 5.4, 5.4, 1],
  43.     [-11.7, -5.4, 5.4, 1], [-33.3, 3.6, 4.5, 3], [-33.3, 3.6, -5.4, 3],
  44.     [-33.3, -3.6, 4.5, 3], [-33.3, -3.6, -5.4, 3], [44.1, 1.8, 3.6, 3],
  45.     [44.1, -1.8, 3.6, 3], [44.1, 1.8, -2.7, 3], [44.1, -1.8, -2.7, 3],
  46.     [44.1, 0, -2.7, 3], [50.4, 0, 0, 3], [46.8, 0, 15.3, 3], [44.1, 0, 15.3, 3],
  47.     [12.6, 5.4, 5.4, 1], [12.6, -5.4, 5.4, 1], [-11.7, 0, 5.4, 1],
  48.     [-3.6, 5.4, 5.4, 1], [-3.6, -5.4, 5.4, 1], [-3.6, 0, 9, 1],
  49.     [12.6, 0, 5.4, 1], [12.6, 0, 7.65, 1], [40.5, 0, 15.3, 3], [35.1, 0, 4.05, 3],
  50.     [36.9, 18, 3.6, 3], [36.9, -18, 3.6, 3], [43.2, 16.2, 3.6, 3],
  51.     [43.2, -16.2, 3.6, 3]
  52. ]
  53.  
  54. # Parsing areas and vertices data
  55. areas_parsed = [[int(point) for point in area] for area in areas]
  56. vertices_parsed = [[float(point) for point in vertex] for vertex in vertices_data]
  57.  
  58. #print("Areas:", areas_parsed)
  59. #print("Vertices:", vertices_parsed)
  60.  
  61.  
  62. for vd in vertices_data:
  63.     vertices.append(vd[:3])
  64.     manipulators.append(vd[3])
  65.  
  66. # Pygame setup
  67. pygame.init()
  68. WIDTH, HEIGHT = 800, 600
  69. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  70. clock = pygame.time.Clock()
  71.  
  72. # Projection parameters
  73. fov = 90
  74. near = 0.1
  75. far = 1000
  76. aspect_ratio = WIDTH / HEIGHT
  77.  
  78. # Rotation parameters
  79. angle_x = 0
  80. angle_y = 0
  81. angle_z = 0
  82.  
  83. # Translation parameters
  84. camera_x = 0
  85. camera_y = 0
  86. camera_z = -30
  87.  
  88. # Helper function to project 3D coordinates to 2D
  89. def project(coord):
  90.     x, y, z = coord
  91.     x = x * (fov / z)
  92.     y = y * (fov / z)
  93.     return int(x + WIDTH / 2), int(y + HEIGHT / 2)
  94.  
  95. # Helper function to rotate a point around the X-axis
  96. def rotate_x(point, angle):
  97.     x, y, z = point
  98.     new_y = y * math.cos(angle) - z * math.sin(angle)
  99.     new_z = y * math.sin(angle) + z * math.cos(angle)
  100.     return x, new_y, new_z
  101.  
  102. # Helper function to rotate a point around the Y-axis
  103. def rotate_y(point, angle):
  104.     x, y, z = point
  105.     new_x = x * math.cos(angle) + z * math.sin(angle)
  106.     new_z = -x * math.sin(angle) + z * math.cos(angle)
  107.     return new_x, y, new_z
  108.  
  109. # Helper function to rotate a point around the Z-axis
  110. def rotate_z(point, angle):
  111.     x, y, z = point
  112.     new_x = x * math.cos(angle) - y * math.sin(angle)
  113.     new_y = x * math.sin(angle) + y * math.cos(angle)
  114.     return new_x, new_y, z
  115.  
  116. # Main loop
  117. running = True
  118. while running:
  119.     screen.fill((0, 0, 0))
  120.  
  121.     # Apply rotation and translation to vertices
  122.     transformed_vertices = []
  123.     for vertex in vertices:
  124.         rotated = vertex
  125.         rotated = rotate_x(rotated, angle_x)
  126.         rotated = rotate_y(rotated, angle_y)
  127.         rotated = rotate_z(rotated, angle_z)
  128.         translated = (rotated[0] + camera_x, rotated[1] + camera_y, rotated[2] + camera_z)
  129.         transformed_vertices.append(translated)
  130.  
  131.  
  132.     # Draw lines between vertices
  133.     for area in areas:
  134.         for i in range(len(area)):
  135.             start = transformed_vertices[area[i] - 1]
  136.             end = transformed_vertices[area[(i + 1) % len(area)] - 1]
  137.            
  138.             # Check if start and end points are too close to the camera or the projection plane
  139.             # have not successfully been trapped this bug
  140.             #if start[2] <= 0.00001 or end[2] <= 0.00001:
  141.                 #continue
  142.  
  143.             #print(start)
  144.             pygame.draw.line(screen, (255, 255, 255), project(start), project(end), 1)
  145.  
  146.     for event in pygame.event.get():
  147.         if event.type == pygame.QUIT:
  148.             running = False
  149.  
  150.     # Handle key presses for camera movement
  151.     keys = pygame.key.get_pressed()
  152.     if keys[pygame.K_w]:
  153.         camera_z += 0.1
  154.     if keys[pygame.K_s]:
  155.         camera_z -= 0.1
  156.     if keys[pygame.K_a]:
  157.         camera_x -= 0.1
  158.     if keys[pygame.K_d]:
  159.         camera_x += 0.1
  160.     if keys[pygame.K_r]:
  161.         camera_y += 0.1
  162.     if keys[pygame.K_f]:
  163.         camera_y -= 0.1
  164.         # Rotate the object
  165.  
  166.     if keys[pygame.K_LEFT]:
  167.         angle_y += 0.02
  168.     if keys[pygame.K_RIGHT]:
  169.         angle_y -= 0.02
  170.     if keys[pygame.K_UP]:
  171.         angle_x += 0.02
  172.     if keys[pygame.K_DOWN]:
  173.         angle_x -= 0.02
  174.  
  175.  
  176.     pygame.display.flip()
  177.     clock.tick(60)
  178.  
  179. pygame.quit()
  180. sys.exit()
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement