Advertisement
575

5 - missile

575
Apr 22nd, 2023
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.57 KB | None | 0 0
  1. #pragma once
  2.  
  3.  
  4. #ifndef MISSILE_H
  5. #define MISSILE_H
  6.  
  7. #include <vector>
  8. #include <random>
  9. #include <chrono>
  10. #include <cmath>
  11. #include <numbers>
  12. #include "file_structs.h"
  13. #include "functions.h"
  14. #include "hit_zx.h"
  15. #include "hit_xy.h"
  16.  
  17.  
  18. struct missile_data {
  19.     coordinates coord_n;
  20.     velocity v_proj_n;
  21.  
  22.     double r;
  23.  
  24.     double yaw_angle;
  25.     double pitch_angle;
  26.  
  27.     missile_data(double r = 0.0, double yaw_angle = 0.0, double pitch_angle = 0.0)
  28.         : r(r), yaw_angle(yaw_angle), pitch_angle(pitch_angle)
  29.     {}
  30. };
  31.  
  32. struct fragment {
  33.     coordinates coord;
  34.     velocity velo;
  35.  
  36.     double mass;
  37.  
  38.     bool hitZX;
  39.     bool hitXY;
  40.     bool calculated;
  41.  
  42.     fragment(double mass = 0.0, bool hitZX = false, bool hitXY = false, bool calculated = false)
  43.         : mass(mass), hitZX(hitZX), hitXY(hitXY), calculated(calculated)
  44.     {}
  45. };
  46.  
  47. velocity MakeFragmentVelocity(coordinates T, double VeloValue) {
  48.     velocity res;
  49.     double length;
  50.     double multiplier;
  51.  
  52.     length = std::sqrt(std::pow(T.x, 2) + std::pow(T.y, 2) + std::pow(T.z, 2));
  53.     multiplier = VeloValue / length;
  54.  
  55.     res = T * multiplier;
  56.  
  57.     return res;
  58. }
  59.  
  60. std::vector <fragment> MakeFragments(missile_data M, target_data T, double InitialSpeed, int N) {
  61.     coordinates coord;
  62.     velocity velo, additional, target;
  63.     double phi;
  64.     std::vector <fragment> res;
  65.     fragment frag;
  66.  
  67.     unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  68.     std::mt19937 generator(seed);
  69.     std::uniform_real_distribution<double> distibX(-0.35, 0.35);
  70.     std::uniform_real_distribution<double> distibPhi(0.0, 1.0);
  71.  
  72.     additional = M.v_proj_n;
  73.    
  74.     target.x = T.v_obj;
  75.     target.y = 0.0;
  76.     target.z = 0.0;
  77.  
  78.     target = MakeRotationVelo(target, T.path_obj, T.pitch_obj);
  79.  
  80.     additional -= target;
  81.  
  82.  
  83.     for (int i = 0; i < N; ++i) {
  84.         frag.hitXY = false;
  85.         frag.hitZX = false;
  86.         frag.mass = 0.1;
  87.  
  88.         coord.x = distibX(generator);
  89.         phi = 2 * std::numbers::pi_v<double> * distibPhi(generator);
  90.         coord.y = M.r * std::sin(phi);
  91.         coord.z = M.r * std::cos(phi);
  92.  
  93.         coord = MakeRotation(coord, M.yaw_angle, M.pitch_angle);
  94.         velo = MakeFragmentVelocity(coord, InitialSpeed);
  95.         velo += additional;
  96.  
  97.         frag.coord = coord;
  98.         frag.velo = velo;
  99.         //std::cout << frag.velo.x << " " << frag.velo.y << " " << frag.velo.z << std::endl;
  100.         if (AngleBetweenVectors(frag.velo, T.basis_z) < 0.0)
  101.             frag.hitXY = true;
  102.  
  103.         if (AngleBetweenVectors(frag.velo, T.basis_y) < 0.0)
  104.             frag.hitZX = true;
  105.  
  106.         if (frag.hitXY || frag.hitZX)
  107.             res.push_back(frag);
  108.     }
  109.  
  110.     return res;
  111. }
  112.  
  113. coordinates PointOfImpact(fragment F, surface T) {
  114.     double t;
  115.     coordinates intersection;
  116.  
  117.     t = -(T.D + T.A * F.coord.x + T.B * F.coord.y + T.C * F.coord.z) /
  118.         (T.A * F.velo.x + T.B * F.velo.y + T.C * F.velo.z);
  119.  
  120.     intersection.x = F.coord.x + t * F.velo.x;
  121.     intersection.y = F.coord.y + t * F.velo.y;
  122.     intersection.z = F.coord.z + t * F.velo.z;
  123.  
  124.     return intersection;
  125. }
  126.  
  127. bool IsInRectangle(std::vector <coordinates> polygon_vertices, coordinates point) {
  128.     bool res;
  129.     double d1, d2, d3, d4;
  130.    
  131.     d1 = (polygon_vertices[1].x - polygon_vertices[0].x) * (point.z - polygon_vertices[0].z) - (polygon_vertices[1].z - polygon_vertices[0].z) * (point.x - polygon_vertices[0].x);
  132.     d2 = (polygon_vertices[2].x - polygon_vertices[1].x) * (point.z - polygon_vertices[1].z) - (polygon_vertices[2].z - polygon_vertices[1].z) * (point.x - polygon_vertices[1].x);
  133.     d3 = (polygon_vertices[3].x - polygon_vertices[2].x) * (point.z - polygon_vertices[2].z) - (polygon_vertices[3].z - polygon_vertices[2].z) * (point.x - polygon_vertices[2].x);
  134.     d4 = (polygon_vertices[0].x - polygon_vertices[3].x) * (point.z - polygon_vertices[3].z) - (polygon_vertices[0].z - polygon_vertices[3].z) * (point.x - polygon_vertices[3].x);
  135.  
  136.     if ((d1 < 0 && d2 < 0 && d3 < 0 && d4 < 0) ||
  137.         (d1 > 0 && d2 > 0 && d3 > 0 && d4 > 0))
  138.         res = true;
  139.     else
  140.         res = false;
  141.  
  142.     return res;
  143. }
  144.  
  145.  
  146. unsigned HitTargetZX(target_data T, coordinates point) {
  147.     unsigned res = 0;
  148.  
  149.     res = HitTargetBodyZX(T, point);
  150.  
  151.     if (!res)
  152.         res = HitTargetWingsZX(T, point);
  153.  
  154.     if (!res)
  155.         res = HitTargetEmpennageZX(T, point);
  156.  
  157.     if (!res)
  158.         res = HitTargetLargeRocketLeftZX(T, point);
  159.  
  160.     if (!res)
  161.         res = HitTargetLargeRocketRightZX(T, point);
  162.  
  163.     if (!res)
  164.         res = HitTargetPropZX(T, point);
  165.  
  166.     return res;
  167. }
  168.  
  169. unsigned HitTargetXY(target_data T, coordinates point) {
  170.     unsigned res = 0;
  171.  
  172.     res = HitTargetBodyXY(T, point);
  173.  
  174.     if (!res)
  175.         res = HitTargetEmpennageXYupper(T, point);
  176.  
  177.     if (!res)
  178.         res = HitTargetEmpennageXYlower(T, point);
  179.  
  180.     if (!res)
  181.         res = HitTargetCamXY(T, point);
  182.  
  183.     if (!res)
  184.         res = HitTargetPropXY(T, point);
  185.  
  186.     return res;
  187. }
  188.  
  189. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement