    

 ## On this page

  

 

 # utils

 Last update: 16.07.2025 

<a name="l00001"></a> 1 

<a name="l00005"></a> 5 

<a name="l00006"></a> 6 \#pragma once

<a name="l00007"></a> 7 

<a name="l00008"></a> 8 \#include &lt;cmath&gt;

<a name="l00009"></a> 9 

<a name="l00010"></a> 10 namespace [simulation\_framework](namespacesimulation__framework.xhtml)

<a name="l00011"></a> 11 {

<a name="l00012"></a> 12 namespace utils

<a name="l00013"></a> 13 {

<a name="l00014"></a> 14 

<a name="l00016"></a>[ 16](structsimulation__framework_1_1utils_1_1Point2d.xhtml) struct [Point2d](structsimulation__framework_1_1utils_1_1Point2d.xhtml)

<a name="l00017"></a> 17 {

<a name="l00018"></a> 18 double x;

<a name="l00019"></a> 19 double y;

<a name="l00020"></a> 20 };

<a name="l00021"></a> 21 

<a name="l00023"></a>[ 23](structsimulation__framework_1_1utils_1_1BoundingBox.xhtml) struct [BoundingBox](structsimulation__framework_1_1utils_1_1BoundingBox.xhtml)

<a name="l00024"></a> 24 {

<a name="l00025"></a> 25 double x; // X-coordinate of center

<a name="l00026"></a> 26 double y; // Y-coordinate of center

<a name="l00027"></a> 27 double width; // Width of the rectangle

<a name="l00028"></a> 28 double length; // Length of the rectangle

<a name="l00029"></a> 29 double yaw; // Yaw angle in radians

<a name="l00030"></a> 30 double velocity_x; // Velocity in X-Axis

<a name="l00031"></a> 31 double velocity_y; // Velocity in Y-Axis

<a name="l00032"></a> 32 };

<a name="l00033"></a> 33 

<a name="l00038"></a> 38 inline double Calculate2dVectorNorm(const [Point2d](structsimulation__framework_1_1utils_1_1Point2d.xhtml)&amp; point1, const [Point2d](structsimulation__framework_1_1utils_1_1Point2d.xhtml)&amp; point2)

<a name="l00039"></a> 39 {

<a name="l00040"></a> 40 const auto difference_x = point2.x - point1.x;

<a name="l00041"></a> 41 const auto difference_y = point2.y - point1.y;

<a name="l00042"></a> 42 return std::sqrt(difference_x * difference_x + difference_y * difference_y);

<a name="l00043"></a> 43 }

<a name="l00044"></a> 44 

<a name="l00050"></a> 50 inline bool ValueInRange(const double value, const double min, const double max)

<a name="l00051"></a> 51 {

<a name="l00052"></a> 52 return (value &gt;= min) &amp;&amp; (value &lt;= max);

<a name="l00053"></a> 53 }

<a name="l00054"></a> 54 

<a name="l00060"></a> 60 inline bool AreBoundingBoxesOverlapped(const BoundingBox&amp; box1, const BoundingBox&amp; box2)

<a name="l00061"></a> 61 {

<a name="l00062"></a> 62 bool x_overlap =

<a name="l00063"></a> 63 ValueInRange(box1.x, box2.x, box2.x + box2.width) || ValueInRange(box2.x, box1.x, box1.x + box1.width);

<a name="l00064"></a> 64 

<a name="l00065"></a> 65 bool y_overlap =

<a name="l00066"></a> 66 ValueInRange(box1.y, box2.y, box2.y + box2.length) || ValueInRange(box2.y, box1.y, box1.y + box1.length);

<a name="l00067"></a> 67 

<a name="l00068"></a> 68 return x_overlap &amp;&amp; y_overlap;

<a name="l00069"></a> 69 }

<a name="l00070"></a> 70 

<a name="l00076"></a> 76 inline bool CanPotentiallyHit(const BoundingBox&amp; box1, const BoundingBox&amp; box2)

<a name="l00077"></a> 77 {

<a name="l00078"></a> 78 // Transform box2's position and velocity into box1's coordinate system

<a name="l00079"></a> 79 auto dx = box2.x - box1.x;

<a name="l00080"></a> 80 auto dy = box2.y - box1.y;

<a name="l00081"></a> 81 

<a name="l00082"></a> 82 auto transformed_x = dx * std::cos(-box1.yaw) - dy * std::sin(-box1.yaw);

<a name="l00083"></a> 83 auto transformed_y = dx * std::sin(-box1.yaw) + dy * std::cos(-box1.yaw);

<a name="l00084"></a> 84 auto transformed_velocity_x = box2.velocity_x * std::cos(-box1.yaw) - box2.velocity_y * std::sin(-box1.yaw);

<a name="l00085"></a> 85 auto transformed_velocity_y = box2.velocity_x * std::sin(-box1.yaw) + box2.velocity_y * std::cos(-box1.yaw);

<a name="l00086"></a> 86 

<a name="l00087"></a> 87 auto d_vx = box1.velocity_x - transformed_velocity_x;

<a name="l00088"></a> 88 auto d_vy = box1.velocity_y - transformed_velocity_y;

<a name="l00089"></a> 89 

<a name="l00090"></a> 90 // Check if the dot product in x or y axis is positive

<a name="l00091"></a> 91 if (d_vx * transformed_x &gt; 0.0 || d_vy * transformed_y &gt; 0.0)

<a name="l00092"></a> 92 {

<a name="l00093"></a> 93 return true; // box2 is potentially hit by box1

<a name="l00094"></a> 94 }

<a name="l00095"></a> 95 

<a name="l00096"></a> 96 return false; // box2 is not in danger of being hit by box1

<a name="l00097"></a> 97 }

<a name="l00098"></a> 98 

<a name="l00099"></a> 99 } // namespace utils

<a name="l00100"></a> 100 } // namespace simulation\_framework