Advertisement
Hadlock

Sorta flight sim programmed by ChatGPT

Jun 11th, 2024
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 13.87 KB | None | 0 0
  1. use minifb::{Key, Window, WindowOptions};
  2. use std::time::{Duration, Instant};
  3. const WIDTH: usize = 800;
  4. const HEIGHT: usize = 600;
  5. const FRAME_RATE: u64 = 240;
  6.  
  7. fn main() {
  8.     // Create a window with blue background
  9.     let mut window = Window::new(
  10.         "3D Wireframe Cube",
  11.         WIDTH,
  12.         HEIGHT,
  13.         WindowOptions {
  14.             resize: true,
  15.             scale: minifb::Scale::X1,
  16.             ..Default::default()
  17.         },
  18.     )
  19.     .unwrap_or_else(|e| {
  20.         panic!("{}", e);
  21.     });
  22.     window.set_background_color(0xFF, 0xFF, 0xFF);
  23.  
  24.     // Cube vertices, edges, and initial positions
  25.     let mut vertices = [
  26.         [-1.0, -1.0, -1.0],
  27.         [1.0, -1.0, -1.0],
  28.         [1.0, 1.0, -1.0],
  29.         [-1.0, 1.0, -1.0],
  30.         [-1.0, -1.0, 1.0],
  31.         [1.0, -1.0, 1.0],
  32.         [1.0, 1.0, 1.0],
  33.         [-1.0, 1.0, 1.0],
  34.     ];
  35.  
  36.     // Cube edges
  37.     let edges = [
  38.         (0, 1),
  39.         (1, 2),
  40.         (2, 3),
  41.         (3, 0),
  42.         (4, 5),
  43.         (5, 6),
  44.         (6, 7),
  45.         (7, 4),
  46.         (0, 4),
  47.         (1, 5),
  48.         (2, 6),
  49.         (3, 7),
  50.     ];
  51.  
  52.     // Cube position
  53.     let mut cube_x = 0.0;
  54.     let mut cube_y = 0.0;
  55.     let mut cube_z = 0.0;
  56.  
  57.     // Square vertices and edges
  58.     let square_vertices = [
  59.         [-2.0, -4.0, -2.0],
  60.         [2.0, -4.0, -2.0],
  61.         [2.0, -4.0, 2.0],
  62.         [-2.0, -4.0, 2.0],
  63.     ];
  64.  
  65.     let square_edges = [
  66.         (0, 1),
  67.         (1, 2),
  68.         (2, 3),
  69.         (3, 0),
  70.     ];
  71.  
  72.     // Camera rotation angle
  73.     let mut angle = 180.0;
  74.  
  75.     // Camera position
  76.     let mut camera_x = 0.0;
  77.     let mut camera_y = 0.0;
  78.  
  79.     let frame_duration = Duration::from_secs_f64(1.0 / FRAME_RATE as f64);
  80.     let mut last_frame_time = Instant::now();
  81.  
  82.     while window.is_open() && !window.is_key_down(Key::Escape) {
  83.         let elapsed = last_frame_time.elapsed();
  84.         if elapsed < frame_duration {
  85.             std::thread::sleep(frame_duration - elapsed);
  86.         }
  87.         last_frame_time = Instant::now();
  88.  
  89.         let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
  90.         window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap();
  91.  
  92.         // Rotate the camera
  93.         //angle += 0.001;
  94.  
  95.         // Handle camera movement
  96.         if window.is_key_down(Key::Up) {
  97.             camera_y += 0.1;
  98.         }
  99.         if window.is_key_down(Key::Down) {
  100.             camera_y -= 0.1;
  101.         }
  102.         if window.is_key_down(Key::Left) {
  103.             camera_x -= 0.1;
  104.         }
  105.         if window.is_key_down(Key::Right) {
  106.             camera_x += 0.1;
  107.         }
  108.         if window.is_key_down(Key::Comma) {
  109.             angle -= 0.01; // Turn camera left
  110.         }
  111.         if window.is_key_down(Key::Period) {
  112.             angle += 0.01; // Turn camera right
  113.         }
  114.  
  115.         // Handle cube movement
  116.         if window.is_key_down(Key::W) {
  117.             cube_z += 0.1;
  118.         }
  119.         if window.is_key_down(Key::S) {
  120.             cube_z -= 0.1;
  121.         }
  122.         if window.is_key_down(Key::A) {
  123.             cube_x -= 0.1;
  124.         }
  125.         if window.is_key_down(Key::D) {
  126.             cube_x += 0.1;
  127.         }
  128.         if window.is_key_down(Key::Q) {
  129.             cube_y -= 0.1; // Move cube down
  130.         }
  131.         if window.is_key_down(Key::E) {
  132.             cube_y += 0.1; // Move cube up
  133.         }
  134.         if window.is_key_down(Key::J) {
  135.             // Rotate cube around the center of its z-axis
  136.             let center_x = (vertices[0][0] + vertices[6][0]) / 2.0;
  137.             let center_y = (vertices[0][1] + vertices[6][1]) / 2.0;
  138.             let center_z = (vertices[0][2] + vertices[6][2]) / 2.0;
  139.             let rotated_vertices = rotate_around_z(vertices, center_x, center_y, center_z, 0.01);
  140.             for i in 0..vertices.len() {
  141.                 vertices[i] = rotated_vertices[i];
  142.             }
  143.         }
  144.         if window.is_key_down(Key::L) {
  145.             // Rotate cube around the center of its z-axis
  146.             let center_x = (vertices[0][0] + vertices[6][0]) / 2.0;
  147.             let center_y = (vertices[0][1] + vertices[6][1]) / 2.0;
  148.             let center_z = (vertices[0][2] + vertices[6][2]) / 2.0;
  149.             let rotated_vertices = rotate_around_z(vertices, center_x, center_y, center_z, -0.01);
  150.             for i in 0..vertices.len() {
  151.                 vertices[i] = rotated_vertices[i];
  152.             }
  153.         }
  154.         if window.is_key_down(Key::I) {
  155.             // Rotate cube around the center of its x-axis
  156.             let center_x = (vertices[0][0] + vertices[6][0]) / 2.0;
  157.             let center_y = (vertices[0][1] + vertices[6][1]) / 2.0;
  158.             let center_z = (vertices[0][2] + vertices[6][2]) / 2.0;
  159.             let rotated_vertices = rotate_around_x(vertices, center_x, center_y, center_z, 0.01);
  160.             for i in 0..vertices.len() {
  161.                 vertices[i] = rotated_vertices[i];
  162.             }
  163.         }
  164.         if window.is_key_down(Key::K) {
  165.             // Rotate cube around the center of its x-axis
  166.             let center_x = (vertices[0][0] + vertices[6][0]) / 2.0;
  167.             let center_y = (vertices[0][1] + vertices[6][1]) / 2.0;
  168.             let center_z = (vertices[0][2] + vertices[6][2]) / 2.0;
  169.             let rotated_vertices = rotate_around_x(vertices, center_x, center_y, center_z, -0.01);
  170.             for i in 0..vertices.len() {
  171.                 vertices[i] = rotated_vertices[i];
  172.             }
  173.         }
  174.         if window.is_key_down(Key::U) {
  175.             // Rotate cube around the center of its y-axis
  176.             let center_x = (vertices[0][0] + vertices[6][0]) / 2.0;
  177.             let center_y = (vertices[0][1] + vertices[6][1]) / 2.0;
  178.             let center_z = (vertices[0][2] + vertices[6][2]) / 2.0;
  179.             let rotated_vertices = rotate_around_y(vertices, center_x, center_y, center_z, 0.01);
  180.             for i in 0..vertices.len() {
  181.                 vertices[i] = rotated_vertices[i];
  182.             }
  183.         }
  184.         if window.is_key_down(Key::O) {
  185.             // Rotate cube around the center of its y-axis
  186.             let center_x = (vertices[0][0] + vertices[6][0]) / 2.0;
  187.             let center_y = (vertices[0][1] + vertices[6][1]) / 2.0;
  188.             let center_z = (vertices[0][2] + vertices[6][2]) / 2.0;
  189.             let rotated_vertices = rotate_around_y(vertices, center_x, center_y, center_z, -0.01);
  190.             for i in 0..vertices.len() {
  191.                 vertices[i] = rotated_vertices[i];
  192.             }
  193.         }
  194.  
  195.         // Project and draw the cube edges
  196.         fn is_front_edge(i: usize, j: usize) -> bool {
  197.             // Define the indices of the front edges
  198.             let front_edges = vec![
  199.                 (0, 1), (1, 2), (2, 3), (3, 0), // Front face
  200.                 //(4, 5), (5, 6), (6, 7), (7, 4), // Back face
  201.                 //(0, 4), (1, 5), (2, 6), (3, 7), // Connecting edges
  202.             ];
  203.  
  204.             // Check if the given indices represent a front edge
  205.             front_edges.contains(&(i, j)) || front_edges.contains(&(j, i))
  206.         }
  207.         fn is_rear_edge(i: usize, j: usize) -> bool {
  208.             // Define the indices of the rear edges
  209.             let rear_edges = vec![
  210.                 (4, 5), (5, 6), (6, 7), (7, 4), // Rear face
  211.                 //(0, 1), (1, 2), (2, 3), (3, 0), // Front face
  212.                 //(0, 4), (1, 5), (2, 6), (3, 7), // Connecting edges
  213.             ];
  214.  
  215.             // Check if the given indices represent a rear edge
  216.             rear_edges.contains(&(i, j)) || rear_edges.contains(&(j, i))
  217.         }
  218.         fn is_bottom_edge(i: usize, j: usize) -> bool {
  219.             // Define the indices of the bottom edges
  220.             let bottom_edges = vec![
  221.                 (4, 5), (5, 6), (6, 7), (7, 4), // Bottom face
  222.                 //(0, 4), (1, 5), (2, 6), (3, 7), // Connecting edges
  223.             ];
  224.  
  225.             // Check if the given indices represent a bottom edge
  226.             bottom_edges.contains(&(i, j)) || bottom_edges.contains(&(j, i))
  227.         }
  228.  
  229.         for &(i, j) in &edges {
  230.             let p1 = project(vertices[i], angle, camera_x, camera_y, cube_x, cube_y, cube_z);
  231.             let p2 = project(vertices[j], angle, camera_x, camera_y, cube_x, cube_y, cube_z);
  232.             if is_front_edge(i, j) {
  233.                 draw_line_with_color(&mut buffer, p1, p2, WIDTH, 0x0000FF); // Set front cube edges to blue
  234.             } else {
  235.                 draw_line(&mut buffer, p1, p2, WIDTH);
  236.             }
  237.         }
  238.  
  239.         // Project and draw the square edges
  240.         for &(i, j) in &square_edges {
  241.             let p1 = project(square_vertices[i], angle, camera_x, camera_y, 0.0, 0.0, -3.0);
  242.             let p2 = project(square_vertices[j], angle, camera_x, camera_y, 0.0, 0.0, -3.0);
  243.             draw_line_with_color(&mut buffer, p1, p2, WIDTH, 0x00FF00); // Set square color to green
  244.         }
  245.  
  246.        
  247.  
  248.         // Draw the x-axis line (red)
  249.         let p1 = project([-1.0, 0.0, 0.0], angle, camera_x, camera_y, 0.0, 0.0, 0.0);
  250.         let p2 = project([1.0, 0.0, 0.0], angle, camera_x, camera_y, 0.0, 0.0, 0.0);
  251.         draw_line_with_color(&mut buffer, p1, p2, WIDTH, 0xFF0000);
  252.  
  253.         // Draw the y-axis line (dark green)
  254.         let p1 = project([0.0, -1.0, 0.0], angle, camera_x, camera_y, 0.0, 0.0, 0.0);
  255.         let p2 = project([0.0, 1.0, 0.0], angle, camera_x, camera_y, 0.0, 0.0, 0.0);
  256.         draw_line_with_color(&mut buffer, p1, p2, WIDTH, 0x006400);
  257.  
  258.         // Draw the z-axis line (cyan blue)
  259.         let p1 = project([0.0, 0.0, -1.0], angle, camera_x, camera_y, 0.0, 0.0, 0.0);
  260.         let p2 = project([0.0, 0.0, 1.0], angle, camera_x, camera_y, 0.0, 0.0, 0.0);
  261.         draw_line_with_color(&mut buffer, p1, p2, WIDTH, 0x00FFFF);
  262.  
  263.         window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap();
  264.     }
  265. }
  266.  
  267. // Project a 3D point onto a 2D plane
  268. fn project(
  269.     point: [f32; 3],
  270.     angle: f32,
  271.     camera_x: f32,
  272.     camera_y: f32,
  273.     cube_x: f32,
  274.     cube_y: f32,
  275.     cube_z: f32,
  276. ) -> (usize, usize) {
  277.     let x = point[0] + cube_x;
  278.     let y = point[1] + cube_y;
  279.     let z = point[2] + cube_z;
  280.  
  281.     let sin_a = angle.sin();
  282.     let cos_a = angle.cos();
  283.  
  284.     let x2 = x * cos_a - z * sin_a;
  285.     let y2 = y + camera_y;
  286.     let z2 = x * sin_a + z * cos_a;
  287.  
  288.     let scale = 2.0 / (z2 + 3.0);
  289.     let x3 = x2 * scale + camera_x;
  290.     let y3 = y2 * scale;
  291.  
  292.     let screen_x = (WIDTH as f32 / 2.0 + x3 * WIDTH as f32 / 4.0) as usize;
  293.     let screen_y = (HEIGHT as f32 / 2.0 - y3 * HEIGHT as f32 / 4.0) as usize;
  294.  
  295.     (screen_x, screen_y)
  296. }
  297.  
  298. fn draw_line(buffer: &mut Vec<u32>, p1: (usize, usize), p2: (usize, usize), width: usize) {
  299.     let (x1, y1) = p1;
  300.     let (x2, y2) = p2;
  301.  
  302.     let dx = (x2 as isize - x1 as isize).abs();
  303.     let dy = (y2 as isize - y1 as isize).abs();
  304.     let sx = if x1 < x2 { 1 } else { -1 };
  305.     let sy = if y1 < y2 { 1 } else { -1 };
  306.     let mut err = dx - dy;
  307.  
  308.     let mut x = x1 as isize;
  309.     let mut y = y1 as isize;
  310.  
  311.     while x != x2 as isize || y != y2 as isize {
  312.         if x >= 0 && x < width as isize && y >= 0 && y < HEIGHT as isize {
  313.             buffer[(y as usize) * width + (x as usize)] = 0xFFFFFF;
  314.         }
  315.  
  316.         let e2 = 2 * err;
  317.         if e2 > -dy {
  318.             err -= dy;
  319.             x += sx;
  320.         }
  321.         if e2 < dx {
  322.             err += dx;
  323.             y += sy;
  324.         }
  325.     }
  326. }
  327.  
  328. fn draw_line_with_color(buffer: &mut Vec<u32>, p1: (usize, usize), p2: (usize, usize), width: usize, color: u32) {
  329.     let (x1, y1) = p1;
  330.     let (x2, y2) = p2;
  331.  
  332.     let dx = (x2 as isize - x1 as isize).abs();
  333.     let dy = (y2 as isize - y1 as isize).abs();
  334.     let sx = if x1 < x2 { 1 } else { -1 };
  335.     let sy = if y1 < y2 { 1 } else { -1 };
  336.     let mut err = dx - dy;
  337.  
  338.     let mut x = x1 as isize;
  339.     let mut y = y1 as isize;
  340.  
  341.     while x != x2 as isize || y != y2 as isize {
  342.         if x >= 0 && x < width as isize && y >= 0 && y < HEIGHT as isize {
  343.             buffer[(y as usize) * width + (x as usize)] = color;
  344.         }
  345.  
  346.         let e2 = 2 * err;
  347.         if e2 > -dy {
  348.             err -= dy;
  349.             x += sx;
  350.         }
  351.         if e2 < dx {
  352.             err += dx;
  353.             y += sy;
  354.         }
  355.     }
  356. }
  357.  
  358. fn rotate_around_z(vertices: [[f32; 3]; 8], center_x: f32, center_y: f32, center_z: f32, angle: f32) -> [[f32; 3]; 8] {
  359.     let sin_a = angle.sin();
  360.     let cos_a = angle.cos();
  361.  
  362.     let mut rotated_vertices = [[0.0; 3]; 8];
  363.     for i in 0..vertices.len() {
  364.         let x = vertices[i][0] - center_x;
  365.         let y = vertices[i][1] - center_y;
  366.         let z = vertices[i][2] - center_z;
  367.  
  368.         rotated_vertices[i][0] = x * cos_a - y * sin_a + center_x;
  369.         rotated_vertices[i][1] = x * sin_a + y * cos_a + center_y;
  370.         rotated_vertices[i][2] = z + center_z;
  371.     }
  372.  
  373.     rotated_vertices
  374. }
  375.  
  376. fn rotate_around_x(vertices: [[f32; 3]; 8], center_x: f32, center_y: f32, center_z: f32, angle: f32) -> [[f32; 3]; 8] {
  377.     let sin_a = angle.sin();
  378.     let cos_a = angle.cos();
  379.  
  380.     let mut rotated_vertices = [[0.0; 3]; 8];
  381.     for i in 0..vertices.len() {
  382.         let x = vertices[i][0] - center_x;
  383.         let y = vertices[i][1] - center_y;
  384.         let z = vertices[i][2] - center_z;
  385.  
  386.         rotated_vertices[i][0] = x + center_x;
  387.         rotated_vertices[i][1] = y * cos_a - z * sin_a + center_y;
  388.         rotated_vertices[i][2] = y * sin_a + z * cos_a + center_z;
  389.     }
  390.  
  391.     rotated_vertices
  392. }
  393.  
  394. fn rotate_around_y(vertices: [[f32; 3]; 8], center_x: f32, center_y: f32, center_z: f32, angle: f32) -> [[f32; 3]; 8] {
  395.     let sin_a = angle.sin();
  396.     let cos_a = angle.cos();
  397.  
  398.     let mut rotated_vertices = [[0.0; 3]; 8];
  399.     for i in 0..vertices.len() {
  400.         let x = vertices[i][0] - center_x;
  401.         let y = vertices[i][1] - center_y;
  402.         let z = vertices[i][2] - center_z;
  403.  
  404.         rotated_vertices[i][0] = x * cos_a + z * sin_a + center_x;
  405.         rotated_vertices[i][1] = y + center_y;
  406.         rotated_vertices[i][2] = -x * sin_a + z * cos_a + center_z;
  407.     }
  408.  
  409.     rotated_vertices
  410. }
Tags: chatGPT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement