grip.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // grip.h
  19. // Project: Nostril (aka Postal)
  20. //
  21. ////////////////////////////////////////////////////////////////////////////////
  22. #ifndef GRIP_H
  23. #define GRIP_H
  24. #include <math.h>
  25. #include "RSPiX.h"
  26. #include "camera.h"
  27. // A CGrip object is typically used to control the movement of a CCamera object.
  28. //
  29. // The name "grip" is taken from the movie industry, where the person who moves
  30. // around the platform the camera sits upon is called the "grip". The camera-
  31. // man is responsible for other camera adjustments such as pan, tilt and zoom.
  32. // In our case, the camera doesn't have those other adjustments, so the grip is
  33. // solely responsible for keeping the camera positioned correctly.
  34. //
  35. // The basic function of the grip is to move the camera so as to track some
  36. // specified "target" in the scene. Rather than just keeping the target within
  37. // the camera's view, it is usually desirable to define a smaller area within
  38. // the view, and to keep the target within (or outside of) that area. This
  39. // area is called the grip's "target zone".
  40. class CGrip
  41. {
  42. //---------------------------------------------------------------------------
  43. // Types, enums, etc.
  44. //---------------------------------------------------------------------------
  45. //---------------------------------------------------------------------------
  46. // Variables
  47. //---------------------------------------------------------------------------
  48. public:
  49. short m_sGripX; // Grip's current x coord
  50. short m_sGripY; // Grip's current y coord
  51. short m_sMinMoveX; // Minimum move in x direction
  52. short m_sMinMoveY; // Minimum move in y direction
  53. short m_sMaxMoveX; // Maximum move in x direction
  54. short m_sMaxMoveY; // Maximum move in y direction
  55. short m_sAlignX; // Required x alignment (1 allows any alignment)
  56. short m_sAlignY; // Required y alignment (1 allows any alignment)
  57. short m_sZoneX;
  58. short m_sZoneY;
  59. short m_sZoneR;
  60. // short m_sZoneW;
  61. // short m_sZoneH;
  62. // bool m_bAlwaysInView; // Always keep target in view, regardless of
  63. // any other settings
  64. // bool m_bUseZone;
  65. bool m_bKeepInsideZone;
  66. // bool m_bKeepOutsideZone;
  67. CCamera* m_pCamera; // Camera being controlled
  68. //---------------------------------------------------------------------------
  69. // Public functions
  70. //---------------------------------------------------------------------------
  71. public:
  72. // Default (and only) constructor
  73. CGrip();
  74. // Destructor
  75. ~CGrip();
  76. // Set camera to control
  77. void SetCamera(
  78. CCamera* pCamera); // In: Camera to control
  79. // Set tracking parameters
  80. void SetParms(
  81. short sZoneR, // In: Zone radius
  82. short sMinMoveX, // In: Minimum move in x direction
  83. short sMinMoveY, // In: Minimum move in y direction
  84. short sMaxMoveX, // In: Maximum move in x direction
  85. short sMaxMoveY, // In: Maximum move in y direction
  86. short sAlignX, // In: Required x alignment (1 allows any alignment)
  87. short sAlignY, // In: Required y alignment (1 allows any alignment)
  88. bool bKeepInsideZone) // In: True to keep inside zone, false otherwise
  89. {
  90. m_sZoneR = sZoneR;
  91. m_sMinMoveX = sMinMoveX;
  92. m_sMinMoveY = sMinMoveY;
  93. m_sMaxMoveX = sMaxMoveX;
  94. m_sMaxMoveY = sMaxMoveY;
  95. m_sAlignX = sAlignX;
  96. m_sAlignY = sAlignY;
  97. m_bKeepInsideZone = bKeepInsideZone;
  98. }
  99. // Reset target, which foces camera to be moved so the target is centered
  100. void ResetTarget(
  101. short sTargetX, // In: Target's x coord
  102. short sTargetY, // In: Target's y coord
  103. short sTargetR) // In: Target's radius
  104. {
  105. m_sZoneX = sTargetX;
  106. m_sZoneY = sTargetY;
  107. short sUpperLeftX = m_sZoneX - (m_pCamera->m_sViewW / 2);
  108. short sUpperLeftY = m_sZoneY - (m_pCamera->m_sViewH / 2);
  109. m_pCamera->SetViewPos(sUpperLeftX, sUpperLeftY);
  110. }
  111. // Track specified target coordinates
  112. void TrackTarget(
  113. short sTargetX, // In: Target's x coord
  114. short sTargetY, // In: Target's y coord
  115. short sTargetR) // In: Target's radius
  116. {
  117. if (m_bKeepInsideZone)
  118. KeepInside(sTargetX, sTargetY, sTargetR);
  119. // else
  120. // KeepOutside(sTargetx, sTargetY);
  121. }
  122. void KeepInside(
  123. short sTargetX, // In: Target's center x coord
  124. short sTargetY, // In: Target's center y coord
  125. short sTargetR) // In: Target's radius
  126. {
  127. // Calculate distance from center of zone to furthest edge of target
  128. double dx = sTargetX - m_sZoneX;
  129. double dy = sTargetY - m_sZoneY;
  130. double dDistance = sqrt((dx * dx) + (dy * dy)) + (double)sTargetR;
  131. // If distance is greater than zone's radius, then we need to move the camera
  132. if (dDistance > m_sZoneR)
  133. {
  134. // Calculate minimum amount we need to move
  135. double dMoveBy = dDistance - (double)m_sZoneR;
  136. // Calculate angle of movement
  137. short sDeg = rspATan(dy, dx);
  138. // Calculate camera x and y movement
  139. short sMoveX = (short)(rspCos(sDeg) * dMoveBy);
  140. short sMoveY = (short)(rspSin(sDeg) * dMoveBy);
  141. // if ((sMoveX >= m_sMinMoveX) && (sMoveY >= m_sMinMoveY))
  142. // {
  143. // if (sMoveX > m_sMaxMoveX)
  144. // sMoveX = m_sMaxMoveX;
  145. // if (sMoveY > m_sMaxMoveY)
  146. // sMoveY = m_sMaxMoveY;
  147. short sModX;
  148. if (sMoveX >= 0)
  149. sModX = sMoveX % m_sAlignX;
  150. else
  151. sModX = (-sMoveX) % m_sAlignX;
  152. if (sModX)
  153. sMoveX -= sModX;
  154. short sModY;
  155. if (sMoveY >= 0)
  156. sModY = sMoveY % m_sAlignY;
  157. else
  158. sModY = (-sMoveY) % m_sAlignY;
  159. if (sModY)
  160. sMoveY -= sModY;
  161. m_sZoneX += sMoveX;
  162. m_sZoneY += sMoveY;
  163. short sUpperLeftX = m_sZoneX - (m_pCamera->m_sViewW / 2);
  164. if (sUpperLeftX < 0)
  165. sUpperLeftX = 0;
  166. short sUpperLeftY = m_sZoneY - (m_pCamera->m_sViewH / 2);
  167. if (sUpperLeftY < 0)
  168. sUpperLeftY = 0;
  169. m_pCamera->SetViewPos(sUpperLeftX, sUpperLeftY);
  170. // }
  171. }
  172. }
  173. //---------------------------------------------------------------------------
  174. // Helper functions
  175. //---------------------------------------------------------------------------
  176. protected:
  177. };
  178. #endif //GRIP_H
  179. ////////////////////////////////////////////////////////////////////////////////
  180. // EOF
  181. ////////////////////////////////////////////////////////////////////////////////