collision_system.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_SYSTEM_HPP
  18. #define HEADER_SUPERTUX_COLLISION_COLLISION_SYSTEM_HPP
  19. #include <vector>
  20. #include <memory>
  21. #include <variant>
  22. #include <stdint.h>
  23. #include "collision/collision.hpp"
  24. #include "supertux/tile.hpp"
  25. #include "math/fwd.hpp"
  26. class CollisionObject;
  27. class CollisionGroundMovementManager;
  28. class DrawingContext;
  29. class Rectf;
  30. class Sector;
  31. class CollisionSystem final
  32. {
  33. public:
  34. struct RaycastResult
  35. {
  36. bool is_valid; /**< true if raycast hit something */
  37. std::variant<const Tile*, CollisionObject*> hit; /**< tile/object that the raycast hit */
  38. Rectf box = {}; /**< hitbox of tile/object */
  39. };
  40. public:
  41. CollisionSystem(Sector& sector);
  42. void add(CollisionObject* object);
  43. void remove(CollisionObject* object);
  44. /** Draw collision shapes for debugging */
  45. void draw(DrawingContext& context);
  46. /** Checks for all possible collisions. And calls the
  47. collision_handlers, which the collision_objects provide for this
  48. case (or not). */
  49. void update();
  50. const std::shared_ptr<CollisionGroundMovementManager>& get_ground_movement_manager()
  51. {
  52. return m_ground_movement_manager;
  53. }
  54. bool is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid = false, uint32_t tiletype = Tile::SOLID) const;
  55. bool is_free_of_statics(const Rectf& rect, const CollisionObject* ignore_object, const bool ignoreUnisolid) const;
  56. bool is_free_of_movingstatics(const Rectf& rect, const CollisionObject* ignore_object) const;
  57. bool is_free_of_specifically_movingstatics(const Rectf& rect, const CollisionObject* ignore_object) const;
  58. RaycastResult get_first_line_intersection(const Vector& line_start,
  59. const Vector& line_end,
  60. bool ignore_objects,
  61. const CollisionObject* ignore_object) const;
  62. bool free_line_of_sight(const Vector& line_start, const Vector& line_end, bool ignore_objects, const CollisionObject* ignore_object) const;
  63. std::vector<CollisionObject*> get_nearby_objects(const Vector& center, float max_distance) const;
  64. private:
  65. /** Does collision detection of an object against all other static
  66. objects (and the tilemap) in the level. Collision response is
  67. done for the first hit in time. (other hits get ignored, the
  68. function should be called repeatedly to resolve those)
  69. returns true if the collision detection should be aborted for
  70. this object (because of ABORT_MOVE in the collision response or
  71. no collisions) */
  72. void collision_static(collision::Constraints* constraints,
  73. const Vector& movement, const Rectf& dest,
  74. CollisionObject& object);
  75. void collision_tilemap(collision::Constraints* constraints,
  76. const Vector& movement, const Rectf& dest,
  77. CollisionObject& object) const;
  78. uint32_t collision_tile_attributes(const Rectf& dest, const Vector& mov) const;
  79. void collision_object(CollisionObject* object1, CollisionObject* object2) const;
  80. void collision_static_constrains(CollisionObject& object);
  81. void get_hit_normal(const CollisionObject* object1, const CollisionObject* object2,
  82. CollisionHit& hit, Vector& normal) const;
  83. private:
  84. Sector& m_sector;
  85. std::vector<CollisionObject*> m_objects;
  86. std::shared_ptr<CollisionGroundMovementManager> m_ground_movement_manager;
  87. private:
  88. CollisionSystem(const CollisionSystem&) = delete;
  89. CollisionSystem& operator=(const CollisionSystem&) = delete;
  90. };
  91. #endif
  92. /* EOF */