PixelChipCode

GameMaker Draw and Animate

Sep 29th, 2024 (edited)
88
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.06 KB | Source Code | 1 0
  1.  _____ _          _  _____ _     _        _____          _      
  2.  |  __ (_)        | |/ ____| |   (_)      / ____|        | |    
  3.  | |__) |__  _____| | |    | |__  _ _ __ | |     ___   __| | ___
  4.  |  ___/ \ \/ / _ \ | |    | '_ \| | '_ \| |    / _ \ / _` |/ _ \
  5.  | |   | |>  <  __/ | |____| | | | | |_) | |___| (_) | (_| |  __/
  6.  |_|   |_/_/\_\___|_|\_____|_| |_|_| .__/ \_____\___/ \__,_|\___|
  7.                                    | |                          
  8.                                    |_|      
  9.  
  10.  
  11. // Define the vertices of the cube in 3D space
  12. cube_vertices = [
  13.     // Each vertex is defined by its X, Y, and Z coordinates
  14.     [-1, -1, -1], // Vertex 0: back-bottom-left
  15.     [ 1, -1, -1], // Vertex 1: back-bottom-right
  16.     [ 1,  1, -1], // Vertex 2: back-top-right
  17.     [-1,  1, -1], // Vertex 3: back-top-left
  18.     [-1, -1,  1], // Vertex 4: front-bottom-left
  19.     [ 1, -1,  1], // Vertex 5: front-bottom-right
  20.     [ 1,  1,  1], // Vertex 6: front-top-right
  21.     [-1,  1,  1]  // Vertex 7: front-top-left
  22. ];
  23.  
  24. // Define the edges of the cube by specifying pairs of connected vertices
  25. cube_edges = [
  26.     [0, 1], [1, 2], [2, 3], [3, 0], // Back face edges: connecting vertices of the back face
  27.     [4, 5], [5, 6], [6, 7], [7, 4], // Front face edges: connecting vertices of the front face
  28.     [0, 4], [1, 5], [2, 6], [3, 7]  // Connecting edges between front and back faces
  29. ];
  30.  
  31. // Initialize the rotation angles for X and Y axes (in degrees)
  32. rotation_angle_x = 0; // Angle of rotation around the X-axis
  33. rotation_angle_y = 0; // Angle of rotation around the Y-axis
  34.  
  35. // Set the color for drawing the cube
  36. draw_set_color(c_white); // Set the drawing color to white for visibility
  37.  
  38. // Clear the canvas with a background color
  39. draw_clear_alpha(c_black, 1); // Clear the canvas with a black color (full opacity)
  40.  
  41. // Define the center of the canvas (where the cube will be centered)
  42. var center_x = 32; // X-coordinate of the center (assuming a 64x64 canvas size)
  43. var center_y = 32; // Y-coordinate of the center
  44.  
  45. // Define the rotation speed for X and Y axes
  46. var rotation_speed_x = 1.03; // Speed at which the cube rotates around the X-axis
  47. var rotation_speed_y = 1.05; // Speed at which the cube rotates around the Y-axis
  48.  
  49. // Update the rotation angles by adding the speed to create continuous rotation
  50. rotation_angle_x += rotation_speed_x; // Increase the rotation angle for X
  51. rotation_angle_y += rotation_speed_y; // Increase the rotation angle for Y
  52.  
  53. // Precalculate the sine and cosine values for both rotation angles to improve performance
  54. var cos_x = dcos(rotation_angle_x); // Cosine of the rotation angle around X
  55. var sin_x = dsin(rotation_angle_x); // Sine of the rotation angle around X
  56. var cos_y = dcos(rotation_angle_y); // Cosine of the rotation angle around Y
  57. var sin_y = dsin(rotation_angle_y); // Sine of the rotation angle around Y
  58.  
  59. // Loop through each edge of the cube to draw them
  60. for (var i = 0; i < array_length(cube_edges); i++) {
  61.     // Get the starting and ending vertex indices for the current edge
  62.     var start_idx = cube_edges[i][0]; // Index of the start vertex for this edge
  63.     var end_idx = cube_edges[i][1];   // Index of the end vertex for this edge
  64.    
  65.     // Retrieve the coordinates of the start and end vertices
  66.     var start_vertex = cube_vertices[start_idx]; // Coordinates of the start vertex
  67.     var end_vertex = cube_vertices[end_idx];     // Coordinates of the end vertex
  68.    
  69.     // Rotate the start vertex around the Y axis
  70.     var start_x_y = start_vertex[0] * cos_y - start_vertex[2] * sin_y; // X coordinate after Y rotation
  71.     var start_z_y = start_vertex[0] * sin_y + start_vertex[2] * cos_y; // Z coordinate after Y rotation
  72.  
  73.     // Rotate the end vertex around the Y axis
  74.     var end_x_y = end_vertex[0] * cos_y - end_vertex[2] * sin_y; // X coordinate after Y rotation
  75.     var end_z_y = end_vertex[0] * sin_y + end_vertex[2] * cos_y; // Z coordinate after Y rotation
  76.    
  77.     // Rotate the start vertex around the X axis
  78.     var start_y = start_vertex[1] * cos_x - start_z_y * sin_x; // Y coordinate after X rotation
  79.     var start_z = start_vertex[1] * sin_x + start_z_y * cos_x; // Z coordinate after X rotation
  80.  
  81.     // Rotate the end vertex around the X axis
  82.     var end_y = end_vertex[1] * cos_x - end_z_y * sin_x; // Y coordinate after X rotation
  83.     var end_z = end_vertex[1] * sin_x + end_z_y * cos_x; // Z coordinate after X rotation
  84.    
  85.     // Project the 3D coordinates to 2D space using a simple orthographic projection
  86.     var projected_start_x = center_x + start_x_y * 16; // Projected X coordinate of start vertex, scaled by 16
  87.     var projected_start_y = center_y + start_y * 16;   // Projected Y coordinate of start vertex, scaled by 16
  88.     var projected_end_x = center_x + end_x_y * 16;     // Projected X coordinate of end vertex, scaled by 16
  89.     var projected_end_y = center_y + end_y * 16;       // Projected Y coordinate of end vertex, scaled by 16
  90.    
  91.     // Draw a line between the projected start and end points
  92.     draw_line(projected_start_x, projected_start_y, projected_end_x, projected_end_y);
  93. }
  94.  
Tags: gameMaker
Advertisement
Add Comment
Please, Sign In to add comment