supertux
collision_system.hpp
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 
18 #ifndef HEADER_SUPERTUX_COLLISION_COLLISION_SYSTEM_HPP
19 #define HEADER_SUPERTUX_COLLISION_COLLISION_SYSTEM_HPP
20 
21 #include <vector>
22 #include <memory>
23 #include <variant>
24 #include <stdint.h>
25 
26 #include "collision/collision.hpp"
27 #include "supertux/tile.hpp"
28 #include "math/fwd.hpp"
29 
30 class CollisionObject;
32 class DrawingContext;
33 class Rectf;
34 class Sector;
35 
36 class CollisionSystem final
37 {
38 public:
40  {
41  bool is_valid;
42  std::variant<const Tile*, CollisionObject*> hit;
43  Rectf box = {};
44  };
45 
46 public:
47  CollisionSystem(Sector& sector);
48 
49  void add(CollisionObject* object);
50  void remove(CollisionObject* object);
51 
53  void draw(DrawingContext& context);
54 
58  void update();
59 
60  const std::shared_ptr<CollisionGroundMovementManager>& get_ground_movement_manager()
61  {
62  return m_ground_movement_manager;
63  }
64 
65  bool is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid = false, uint32_t tiletype = Tile::SOLID) const;
66  bool is_free_of_statics(const Rectf& rect, const CollisionObject* ignore_object, const bool ignoreUnisolid) const;
67  bool is_free_of_movingstatics(const Rectf& rect, const CollisionObject* ignore_object) const;
68  bool is_free_of_specifically_movingstatics(const Rectf& rect, const CollisionObject* ignore_object) const;
69 
70 
71  RaycastResult get_first_line_intersection(const Vector& line_start,
72  const Vector& line_end,
73  bool ignore_objects,
74  const CollisionObject* ignore_object) const;
75  bool free_line_of_sight(const Vector& line_start, const Vector& line_end, bool ignore_objects, const CollisionObject* ignore_object) const;
76 
77  std::vector<CollisionObject*> get_nearby_objects(const Vector& center, float max_distance) const;
78 
79 private:
88  void collision_static(collision::Constraints* constraints,
89  const Vector& movement, const Rectf& dest,
90  CollisionObject& object);
91 
92  void collision_tilemap(collision::Constraints* constraints,
93  const Vector& movement, const Rectf& dest,
94  CollisionObject& object) const;
95 
96  uint32_t collision_tile_attributes(const Rectf& dest, const Vector& mov) const;
97 
98  void collision_object(CollisionObject* object1, CollisionObject* object2) const;
99 
100  void collision_static_constrains(CollisionObject& object);
101 
102  void get_hit_normal(const CollisionObject* object1, const CollisionObject* object2,
103  CollisionHit& hit, Vector& normal) const;
104 
105 private:
106  Sector& m_sector;
107 
108  std::vector<CollisionObject*> m_objects;
109 
110  std::shared_ptr<CollisionGroundMovementManager> m_ground_movement_manager;
111 
112 private:
113  CollisionSystem(const CollisionSystem&) = delete;
114  CollisionSystem& operator=(const CollisionSystem&) = delete;
115 };
116 
117 #endif
118 
119 /* EOF */
solid tile that is indestructible by Tux
Definition: tile.hpp:41
Definition: collision_system.hpp:39
void draw(DrawingContext &context)
Draw collision shapes for debugging.
Definition: collision_system.cpp:71
bool is_valid
true if raycast hit something
Definition: collision_system.hpp:41
std::variant< const Tile *, CollisionObject * > hit
tile/object that the raycast hit
Definition: collision_system.hpp:42
Definition: collision_system.hpp:36
Represents one of (potentially) multiple, separate parts of a Level.
Definition: sector.hpp:61
void update()
Checks for all possible collisions.
Definition: collision_system.cpp:568
This class takes care of moving objects that have collided on top of other moving objects or on top o...
Definition: collision_movement_manager.hpp:32
Definition: rectf.hpp:31
Definition: collision.hpp:31
Rectf box
hitbox of tile/object
Definition: collision_system.hpp:43
Definition: collision_object.hpp:33
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
This class collects data about a collision.
Definition: collision_hit.hpp:44