World2D.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #pragma once
  2. #include <functional>
  3. #include <queue>
  4. #include "Array.h"
  5. #include "Mat3.h"
  6. #include "Point.h"
  7. #include "Rect2.h"
  8. namespace Framework
  9. {
  10. typedef Vec2<float> Vertex;
  11. class Image;
  12. struct WorldInfo
  13. {
  14. float airResistance;
  15. bool hasSize;
  16. bool circular;
  17. Point size;
  18. };
  19. class Object2D : public virtual ReferenceCounter
  20. {
  21. protected:
  22. std::queue<std::function<void()>> actions;
  23. Vertex position;
  24. Vertex speed;
  25. float rSpeed;
  26. float rotation;
  27. float size;
  28. bool collision;
  29. public:
  30. DLLEXPORT Object2D();
  31. DLLEXPORT virtual ~Object2D();
  32. //! Passes a void function pointer to an action that should be executed
  33. //! once by the main thread. (Happens after tick)
  34. DLLEXPORT void postAction(std::function<void()> action);
  35. //! Adds a thrust in the propagation direction of the explosion to
  36. //! the object's movement \param worldPos The position of the explosion
  37. //! origin \param intensity The intensity of the explosion
  38. DLLEXPORT void explosion(Vertex worldPos, float intensity);
  39. //! Applies an impulse to the object that affects both speed and
  40. //! rotation speed \param start The start position of the impulse
  41. //! in the world \param speed The speed of the impulse in the world
  42. //! \param strength Strength of the impact
  43. DLLEXPORT virtual void impuls(
  44. Vertex start, Vertex speed, float strength = 1.f);
  45. //! Sets the speed of the object in the world
  46. //! \param speed Number of coordinates traveled per second
  47. DLLEXPORT void setSpeed(Vertex speed);
  48. //! Sets the speed of the object in the world
  49. //! \param x Number of x coordinates traveled per second
  50. //! \param y Number of y coordinates traveled per second
  51. DLLEXPORT void setSpeed(float x, float y);
  52. //! Sets the position of the object in the world
  53. //! \param pos The position in world coordinates
  54. DLLEXPORT void setPosition(Vertex pos);
  55. //! Sets the position of the object in the world
  56. //! \param x The X position in world coordinates
  57. //! \param y The Y position in world coordinates
  58. DLLEXPORT void setPosition(float x, float y);
  59. //! Sets the rotation speed in radians per second
  60. //! \param ds The new rotation speed
  61. DLLEXPORT void setDrehungSpeed(float ds);
  62. //! Sets the counter-clockwise rotation of the model
  63. //! \param drehung The angle in radians
  64. DLLEXPORT void setDrehung(float drehung);
  65. //! Adds to the current rotation angle
  66. //! \param drehung The angle in radians to add
  67. DLLEXPORT void addDrehung(float drehung);
  68. //! Sets the scaling of the model
  69. //! \param size The scaling factor
  70. DLLEXPORT void setSize(float size);
  71. //! Adds a value to the scaling
  72. //! \param size The value to add to the scaling
  73. DLLEXPORT void addSize(float size);
  74. //! Sets whether other objects can collide with this object
  75. //! \param handle 0 if no collisions exist
  76. DLLEXPORT void setCollision(bool handle);
  77. //! Checks if a collision with another object exists and adjusts
  78. //! the speeds of both objects accordingly
  79. DLLEXPORT virtual bool handleCollision(Object2D* obj);
  80. //! Processes elapsed time and updates the position and rotation
  81. //! of the object in the world \param zeit The elapsed time in seconds
  82. DLLEXPORT virtual bool tick(const WorldInfo& info, double zeit);
  83. //! Draws the object into an image
  84. //! \param kamMat The camera matrix that converts a point from
  85. //! world coordinates to screen coordinates \param zRObj The image
  86. //! to draw into \param ignoreTransparentFlag if 1, collisions with
  87. //! transparent polygons are also considered
  88. virtual void render(
  89. Mat3<float>& kamMat, Image& zRObj, const char* ignoreTransparentFlag)
  90. = 0;
  91. //! Returns whether a point is inside the object
  92. //! \param p The point
  93. //! \param ignoreTransparentFlag if 1, collisions with transparent
  94. //! polygons are also considered
  95. DLLEXPORT virtual bool isPointInside(
  96. Vertex p, bool ignoreTransparentFlag = 0) const;
  97. //! Checks whether a line is inside the object
  98. //! \param a The start point of the line
  99. //! \param b The end point of the line
  100. //! \param ignoreTransparentFlag if 1, collisions with transparent
  101. //! polygons are also considered
  102. DLLEXPORT virtual bool isLineInside(
  103. Vertex a, Vertex b, bool ignoreTransparentFlag = 0) const;
  104. //! Checks whether the object intersects with another
  105. //! \param zObj A pointer to the other object without increased reference
  106. //! counter \param sp A pointer to a point where the intersection
  107. //! point is stored \param end 0 if all corners of both objects should be
  108. //! checked. 1 if only the points of this model should be searched
  109. //! in the other
  110. DLLEXPORT virtual bool isModelInside(const Object2D* zObj,
  111. Vertex* sp = 0,
  112. bool end = 0,
  113. bool ignoreTransparent = 0) const;
  114. //! Returns a matrix that converts a point from object coordinates
  115. //! to screen coordinates
  116. DLLEXPORT Mat3<float> getObjectMatrix() const;
  117. //! Returns a matrix that converts a point from screen coordinates
  118. //! to object coordinates
  119. DLLEXPORT Mat3<float> getInverseObjectMatrix() const;
  120. //! Converts a point from world coordinates to object coordinates
  121. //! \param worldPos The position of the point in the world
  122. DLLEXPORT Vertex getObjectPos(Vertex worldPos) const;
  123. //! Converts a direction from world coordinates to object coordinates
  124. //! \param worldDir The direction in world coordinates
  125. DLLEXPORT Vertex getObjectDir(Vertex worldDir) const;
  126. //! Converts a point from object coordinates to world coordinates
  127. //! \param worldPos The position of the point in object coordinates
  128. DLLEXPORT Vertex getWorldPos(Vertex objectPos) const;
  129. //! Converts a direction from object coordinates to world coordinates
  130. //! \param worldDir The direction in object coordinates
  131. DLLEXPORT Vertex getWorldDir(Vertex objectDir) const;
  132. //! Returns the speed of the object
  133. DLLEXPORT Vertex getSpeed() const;
  134. //! Returns the position of the object
  135. DLLEXPORT Vertex getPosition() const;
  136. //! Returns the rotation speed of the object
  137. DLLEXPORT float getDrehungSpeed() const;
  138. //! Returns the rotation of the object
  139. DLLEXPORT float getDrehung() const;
  140. //! Returns the scaling of the object
  141. DLLEXPORT float getSize() const;
  142. //! Returns a bounding box containing all points of the object
  143. //! (in world coordinates)
  144. DLLEXPORT virtual Rect2<float> getBoundingBox() const = 0;
  145. //! Determines the hit point of a ray emitted from pos in the
  146. //! direction dir. \param pos The support vector of the line
  147. //! \param dir The direction vector of the line
  148. //! \param hitPoint A reference to the variable where the
  149. //! intersection point should be stored \return 1 if an
  150. //! intersection point exists
  151. DLLEXPORT virtual bool calcHitPoint(
  152. Vertex pos, Vertex dir, Vertex& hitpoint) const;
  153. //! Determines the area of the object perpendicular to the
  154. //! movement vector
  155. DLLEXPORT virtual float getLuftWiederstand() const;
  156. //! Returns the mass of the object
  157. DLLEXPORT virtual float getMasse() const;
  158. //! Returns whether other objects can collide with this object
  159. //! \return 0 if no collisions exist
  160. DLLEXPORT bool canCollide();
  161. };
  162. class World2D : public virtual ReferenceCounter
  163. {
  164. private:
  165. RCArray<Object2D>* objects;
  166. WorldInfo info;
  167. void render(Mat3<float>& kamMat,
  168. Point size,
  169. Image& zRObj,
  170. int xOffset,
  171. int yOffset,
  172. const char* kamName);
  173. public:
  174. DLLEXPORT World2D();
  175. DLLEXPORT ~World2D();
  176. DLLEXPORT void setAirResistance(float resistance);
  177. DLLEXPORT void setSize(int width, int height);
  178. DLLEXPORT void setSize(bool hasSize);
  179. DLLEXPORT void setCircular(bool circular);
  180. DLLEXPORT Object2D* zObjectAt(
  181. int x, int y, bool ignoreTransparentFlag = 0);
  182. DLLEXPORT Object2D* getObjectAt(
  183. int x, int y, bool ignoreTransparentFlag = 0);
  184. DLLEXPORT void addObject(Object2D* obj);
  185. DLLEXPORT void removeObject(Object2D* zObj);
  186. DLLEXPORT void removeAll();
  187. DLLEXPORT void explosion(
  188. Vertex worldPos, float intensity, float maxRad);
  189. DLLEXPORT void impuls(Vertex worldPos, Vertex worldDir);
  190. DLLEXPORT bool tick(double zeit);
  191. DLLEXPORT void render(
  192. Mat3<float>& kamMat, Point size, Image& zRObj, const char* kamName);
  193. DLLEXPORT const WorldInfo& getWorldInfo() const;
  194. DLLEXPORT ArrayIterator<Object2D*> getMembers();
  195. };
  196. } // namespace Framework