Advertisement
Berserk88

Region of scr_fire_cannon

Aug 29th, 2019
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #region // create shell object and initialize its direction and theta
  2. shell = instance_create(shell_x,
  3.                         shell_y,
  4.                         shell_object);
  5.  
  6. with (shell) {
  7.    
  8.     // assign bullet owner to ensure it doesn't hit self
  9.     owner = other.owner;
  10.    
  11.     // give bullet its direction
  12.     image_angle = other.image_angle;
  13.     direction = image_angle;
  14.  
  15.     // for calculating travel distances, get origin and target x, y, z locations
  16.     origin_y = y;
  17.     origin_x = x;
  18.     origin_z = other.z;
  19.    
  20.     target_x = other.desired_point[0];
  21.     target_y = other.desired_point[1];
  22.     target_z = other.desired_point[2];
  23.    
  24.     // set target distance
  25.     target_distance = point_distance(x, y, other.desired_point[0], other.desired_point[1]);
  26.    
  27.     // make sure distance isn't greater than maximum
  28.     if (target_distance > other.maximum_range) target_distance = other.maximum_range;  
  29.    
  30.     show_debug_message("Target distance: " + string(target_distance)); 
  31.  
  32.     // shells total velocity in all axises
  33.     shell_velocity = other.shell_velocity;
  34.    
  35.     // use for testing purposes, delete when formulas work
  36.     target_z = 40;
  37.    
  38.     // convert velocity from pixels per tick to pixels per second
  39.     var v = shell_velocity * room_speed; // convert speed per tick to speed per second
  40.    
  41.     // v to the power of 2
  42.     var v_2 = power(v, 2);
  43.    
  44.     // v to the power of 4
  45.     var v_4 = power(v, 4);
  46.    
  47.     // gravity
  48.     var g = 9.8 * 10;
  49.    
  50.     // displacement to target in vertical and horizontal
  51.     var dis_h = target_distance;
  52.     var dis_v = (target_z - origin_z);
  53.     var dis_h_2 = power(dis_h, 2);
  54.        
  55.     // calculate potential angles, one will be high, one will be low
  56.     var theta_0 = radtodeg(arctan(  (v_2 - sqrt(v_4 - g * ( g * dis_h_2 + 2 * dis_v * v_2)) ) / (g * dis_h)   ));
  57.     var theta_1 = radtodeg(arctan(  (v_2 + sqrt(v_4 - g * ( g * dis_h_2 + 2 * dis_v * v_2)) ) / (g * dis_h)   ));
  58.        
  59.     // set to angle closest to 0
  60.     if (abs(theta_1) < abs(theta_0)) theta = theta_1;
  61.     else theta = theta_0;
  62.    
  63.     show_debug_message("Theta is: " + string(theta) + " based on a theta_0 of: " + string(theta_0) +
  64.         " and theta_1 of: " + string(theta_1));
  65.    
  66.     // horizontal velocity, x and y axis
  67.     velocity_h = shell_velocity * radtodeg(cos(degtorad(theta)));
  68.    
  69.     // vertical velocity, z axis
  70.     velocity_v = shell_velocity * radtodeg(sin(degtorad(theta)));
  71. }
  72.  
  73. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement