collision_object.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // 2018 Ingo Ruhnke <grumbel@gmail.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef HEADER_SUPERTUX_COLLISION_COLLISION_OBJECT_HPP
  18. #define HEADER_SUPERTUX_COLLISION_COLLISION_OBJECT_HPP
  19. #include <stdint.h>
  20. #include <memory>
  21. #include <unordered_set>
  22. #include "collision/collision_group.hpp"
  23. #include "collision/collision_hit.hpp"
  24. #include "math/rectf.hpp"
  25. class CollisionListener;
  26. class CollisionGroundMovementManager;
  27. class GameObject;
  28. class CollisionObject
  29. {
  30. friend class CollisionSystem;
  31. public:
  32. CollisionObject(CollisionGroup group, CollisionListener& parent);
  33. /** this function is called when the object collided with something solid */
  34. void collision_solid(const CollisionHit& hit);
  35. /** when 2 objects collided, we will first call the
  36. pre_collision_check functions of both objects that can decide on
  37. how to react to the collision. */
  38. bool collides(CollisionObject& other, const CollisionHit& hit) const;
  39. /** this function is called when the object collided with any other object */
  40. HitResponse collision(CollisionObject& other, const CollisionHit& hit);
  41. /** called when tiles with special attributes have been touched */
  42. void collision_tile(uint32_t tile_attributes);
  43. /** called when this object, if (moving) static, has collided on its top with a moving object */
  44. void collision_moving_object_bottom(CollisionObject& other);
  45. void notify_object_removal(CollisionObject* other);
  46. void set_ground_movement_manager(const std::shared_ptr<CollisionGroundMovementManager>& movement_manager)
  47. {
  48. m_ground_movement_manager = movement_manager;
  49. }
  50. void clear_bottom_collision_list();
  51. bool is_unisolid() const { return m_unisolid; }
  52. void set_unisolid(bool unisolid) { m_unisolid = unisolid; }
  53. /** returns the bounding box of the Object */
  54. const Rectf& get_bbox() const
  55. {
  56. return m_bbox;
  57. }
  58. void set_movement(const Vector& movement)
  59. {
  60. m_movement = movement;
  61. }
  62. void propagate_movement(const Vector& movement);
  63. const Vector& get_movement() const
  64. {
  65. return m_movement;
  66. }
  67. /** places the moving object at a specific position. Be careful when
  68. using this function. There are no collision detection checks
  69. performed here so bad things could happen. */
  70. void set_pos(const Vector& pos)
  71. {
  72. m_dest.move(pos - get_pos());
  73. m_bbox.set_pos(pos);
  74. }
  75. Vector get_pos() const
  76. {
  77. return m_bbox.p1();
  78. }
  79. Vector get_pressure() const
  80. {
  81. return m_pressure;
  82. }
  83. /** moves entire object to a specific position, including all
  84. points those the object has, exactly like the object has
  85. spawned in that given pos instead.*/
  86. void move_to(const Vector& pos)
  87. {
  88. set_pos(pos);
  89. }
  90. /** sets the moving object's bbox to a specific width. Be careful
  91. when using this function. There are no collision detection
  92. checks performed here so bad things could happen. */
  93. void set_width(float w)
  94. {
  95. m_dest.set_width(w);
  96. m_bbox.set_width(w);
  97. }
  98. /** sets the moving object's bbox to a specific size. Be careful
  99. when using this function. There are no collision detection
  100. checks performed here so bad things could happen. */
  101. void set_size(float w, float h)
  102. {
  103. m_dest.set_size(w, h);
  104. m_bbox.set_size(w, h);
  105. }
  106. CollisionGroup get_group() const
  107. {
  108. return m_group;
  109. }
  110. bool is_valid() const;
  111. CollisionListener& get_listener()
  112. {
  113. return m_listener;
  114. }
  115. private:
  116. CollisionListener& m_listener;
  117. public:
  118. /** The bounding box of the object (as used for collision detection,
  119. this isn't necessarily the bounding box for graphics) */
  120. Rectf m_bbox;
  121. /** The collision group */
  122. CollisionGroup m_group;
  123. private:
  124. /** The movement that will happen till next frame */
  125. Vector m_movement;
  126. /** This is only here for internal collision detection use (don't touch this
  127. from outside collision detection code)
  128. This field holds the currently anticipated destination of the object
  129. during collision detection */
  130. Rectf m_dest;
  131. /** Determines whether the object is unisolid.
  132. Only bottom constraints to the top of the bounding box would be applied for objects,
  133. colliding with unisolid objects. */
  134. bool m_unisolid;
  135. /**
  136. * Contains the current pressure on this object
  137. */
  138. Vector m_pressure;
  139. /** Objects that were touching the top of this object at the last frame,
  140. if this object was static or moving static. */
  141. std::unordered_set<CollisionObject*> m_objects_hit_bottom;
  142. std::shared_ptr<CollisionGroundMovementManager> m_ground_movement_manager;
  143. private:
  144. CollisionObject(const CollisionObject&) = delete;
  145. CollisionObject& operator=(const CollisionObject&) = delete;
  146. };
  147. #endif
  148. /* EOF */