collision_object.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 CollisionGroundMovementManager;
  26. class MovingObject;
  27. class CollisionObject
  28. {
  29. friend class CollisionSystem;
  30. public:
  31. CollisionObject(CollisionGroup group, MovingObject& parent);
  32. /** this function is called when the object collided with something solid */
  33. void collision_solid(const CollisionHit& hit);
  34. /** when 2 objects collided, we will first call the
  35. pre_collision_check functions of both objects that can decide on
  36. how to react to the collision. */
  37. bool collides(CollisionObject& other, const CollisionHit& hit) const;
  38. /** this function is called when the object collided with any other object */
  39. HitResponse collision(CollisionObject& other, const CollisionHit& hit);
  40. /** called when tiles with special attributes have been touched */
  41. void collision_tile(uint32_t tile_attributes);
  42. /** called when this object, if (moving) static, has collided on its top with a moving object */
  43. void collision_moving_object_bottom(CollisionObject& other);
  44. void notify_object_removal(CollisionObject* other);
  45. inline void set_ground_movement_manager(const std::shared_ptr<CollisionGroundMovementManager>& movement_manager)
  46. {
  47. m_ground_movement_manager = movement_manager;
  48. }
  49. void clear_bottom_collision_list();
  50. inline bool is_unisolid() const { return m_unisolid; }
  51. inline void set_unisolid(bool unisolid) { m_unisolid = unisolid; }
  52. /** returns the bounding box of the Object */
  53. inline const Rectf& get_bbox() const
  54. {
  55. return m_bbox;
  56. }
  57. inline void set_movement(const Vector& movement)
  58. {
  59. m_movement = movement;
  60. }
  61. void propagate_movement(const Vector& movement);
  62. inline const Vector& get_movement() const
  63. {
  64. return m_movement;
  65. }
  66. /** places the moving object at a specific position. Be careful when
  67. using this function. There are no collision detection checks
  68. performed here so bad things could happen. */
  69. void set_pos(const Vector& pos)
  70. {
  71. m_dest.move(pos - get_pos());
  72. m_bbox.set_pos(pos);
  73. }
  74. inline Vector get_pos() const
  75. {
  76. return m_bbox.p1();
  77. }
  78. inline Vector get_pressure() const
  79. {
  80. return m_pressure;
  81. }
  82. /** moves entire object to a specific position, including all
  83. points those the object has, exactly like the object has
  84. spawned in that given pos instead.*/
  85. inline void move_to(const Vector& pos)
  86. {
  87. set_pos(pos);
  88. }
  89. /** sets the moving object's bbox to a specific width. Be careful
  90. when using this function. There are no collision detection
  91. checks performed here so bad things could happen. */
  92. void set_width(float w)
  93. {
  94. m_dest.set_width(w);
  95. m_bbox.set_width(w);
  96. }
  97. /** sets the moving object's bbox to a specific size. Be careful
  98. when using this function. There are no collision detection
  99. checks performed here so bad things could happen. */
  100. void set_size(float w, float h)
  101. {
  102. m_dest.set_size(w, h);
  103. m_bbox.set_size(w, h);
  104. }
  105. inline CollisionGroup get_group() const
  106. {
  107. return m_group;
  108. }
  109. bool is_valid() const;
  110. inline MovingObject& get_parent() { return m_parent; }
  111. private:
  112. MovingObject& m_parent;
  113. public:
  114. /** The bounding box of the object (as used for collision detection,
  115. this isn't necessarily the bounding box for graphics) */
  116. Rectf m_bbox;
  117. /** The collision group */
  118. CollisionGroup m_group;
  119. private:
  120. /** The movement that will happen till next frame */
  121. Vector m_movement;
  122. /** This is only here for internal collision detection use (don't touch this
  123. from outside collision detection code)
  124. This field holds the currently anticipated destination of the object
  125. during collision detection */
  126. Rectf m_dest;
  127. /** Determines whether the object is unisolid.
  128. Only bottom constraints to the top of the bounding box would be applied for objects,
  129. colliding with unisolid objects. */
  130. bool m_unisolid;
  131. /**
  132. * Contains the current pressure on this object
  133. */
  134. Vector m_pressure;
  135. /** Objects that were touching the top of this object at the last frame,
  136. if this object was static or moving static. */
  137. std::unordered_set<CollisionObject*> m_objects_hit_bottom;
  138. std::shared_ptr<CollisionGroundMovementManager> m_ground_movement_manager;
  139. private:
  140. CollisionObject(const CollisionObject&) = delete;
  141. CollisionObject& operator=(const CollisionObject&) = delete;
  142. };
  143. #endif
  144. /* EOF */