supertux
collision_object.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_OBJECT_HPP
19 #define HEADER_SUPERTUX_COLLISION_COLLISION_OBJECT_HPP
20 
21 #include <stdint.h>
22 #include <memory>
23 #include <unordered_set>
24 
25 #include "collision/collision_group.hpp"
26 #include "collision/collision_hit.hpp"
27 #include "math/rectf.hpp"
28 
29 class CollisionListener;
31 class GameObject;
32 
34 {
35  friend class CollisionSystem;
36 
37 public:
38  CollisionObject(CollisionGroup group, CollisionListener& parent);
39 
41  void collision_solid(const CollisionHit& hit);
42 
46  bool collides(CollisionObject& other, const CollisionHit& hit) const;
47 
49  HitResponse collision(CollisionObject& other, const CollisionHit& hit);
50 
52  void collision_tile(uint32_t tile_attributes);
53 
56 
57  void notify_object_removal(CollisionObject* other);
58 
59  void set_ground_movement_manager(const std::shared_ptr<CollisionGroundMovementManager>& movement_manager)
60  {
61  m_ground_movement_manager = movement_manager;
62  }
63 
64  void clear_bottom_collision_list();
65 
66  bool is_unisolid() const { return m_unisolid; }
67  void set_unisolid(bool unisolid) { m_unisolid = unisolid; }
68 
70  const Rectf& get_bbox() const
71  {
72  return m_bbox;
73  }
74 
75  void set_movement(const Vector& movement)
76  {
77  m_movement = movement;
78  }
79 
80  void propagate_movement(const Vector& movement);
81 
82  const Vector& get_movement() const
83  {
84  return m_movement;
85  }
86 
90  void set_pos(const Vector& pos)
91  {
92  m_dest.move(pos - get_pos());
93  m_bbox.set_pos(pos);
94  }
95 
96  Vector get_pos() const
97  {
98  return m_bbox.p1();
99  }
100 
101  Vector get_pressure() const
102  {
103  return m_pressure;
104  }
105 
109  void move_to(const Vector& pos)
110  {
111  set_pos(pos);
112  }
113 
117  void set_width(float w)
118  {
119  m_dest.set_width(w);
120  m_bbox.set_width(w);
121  }
122 
126  void set_size(float w, float h)
127  {
128  m_dest.set_size(w, h);
129  m_bbox.set_size(w, h);
130  }
131 
132  CollisionGroup get_group() const
133  {
134  return m_group;
135  }
136 
137  bool is_valid() const;
138 
139  CollisionListener& get_listener()
140  {
141  return m_listener;
142  }
143 
144 private:
145  CollisionListener& m_listener;
146 
147 public:
151 
153  CollisionGroup m_group;
154 
155 private:
157  Vector m_movement;
158 
164  Rectf m_dest;
165 
170  bool m_unisolid;
171 
175  Vector m_pressure;
176 
179  std::unordered_set<CollisionObject*> m_objects_hit_bottom;
180 
181  std::shared_ptr<CollisionGroundMovementManager> m_ground_movement_manager;
182 
183 private:
184  CollisionObject(const CollisionObject&) = delete;
185  CollisionObject& operator=(const CollisionObject&) = delete;
186 };
187 
188 #endif
189 
190 /* EOF */
HitResponse collision(CollisionObject &other, const CollisionHit &hit)
this function is called when the object collided with any other object
Definition: collision_object.cpp:50
void collision_moving_object_bottom(CollisionObject &other)
called when this object, if (moving) static, has collided on its top with a moving object ...
Definition: collision_object.cpp:62
CollisionGroup m_group
The collision group.
Definition: collision_object.hpp:153
void set_size(float w, float h)
sets the moving object&#39;s bbox to a specific size.
Definition: collision_object.hpp:126
bool collides(CollisionObject &other, const CollisionHit &hit) const
when 2 objects collided, we will first call the pre_collision_check functions of both objects that ca...
Definition: collision_object.cpp:44
Definition: collision_listener.hpp:23
void set_width(float w)
sets the moving object&#39;s bbox to a specific width.
Definition: collision_object.hpp:117
const Rectf & get_bbox() const
returns the bounding box of the Object
Definition: collision_object.hpp:70
void move_to(const Vector &pos)
moves entire object to a specific position, including all points those the object has...
Definition: collision_object.hpp:109
Definition: collision_system.hpp:36
Rectf m_bbox
The bounding box of the object (as used for collision detection, this isn&#39;t necessarily the bounding ...
Definition: collision_object.hpp:150
void set_pos(const Vector &pos)
places the moving object at a specific position.
Definition: collision_object.hpp:90
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
This class is responsible for: Updating and drawing the object.
Definition: game_object.hpp:83
void collision_solid(const CollisionHit &hit)
this function is called when the object collided with something solid
Definition: collision_object.cpp:38
void collision_tile(uint32_t tile_attributes)
called when tiles with special attributes have been touched
Definition: collision_object.cpp:56
Definition: collision_object.hpp:33
This class collects data about a collision.
Definition: collision_hit.hpp:44