started work on a collision grid class to speedup collision detection. Doesn't work...
[supertux.git] / src / collision_grid.h
1 #ifndef __COLLISION_GRID_H__
2 #define __COLLISION_GRID_H__
3
4 #include <vector>
5 #include "special/moving_object.h"
6
7 using namespace SuperTux;
8
9 /**
10  * A rectangular grid to keep track of all moving game objects. It allows fast
11  * queries for all objects in a rectangular area.
12  */
13 class CollisionGrid
14 {
15 public:
16   CollisionGrid(float width, float height);
17   ~CollisionGrid();
18
19   void add_object(MovingObject* object);
20   void remove_object(MovingObject* object);
21   void move_object(MovingObject* object);
22
23   void check_collisions();
24
25 private:
26   struct ObjectWrapper
27   {
28     MovingObject* object;
29     Rectangle dest;
30     /** (pseudo) timestamp. When reading from the grid the timestamp is
31      * changed so that you can easily avoid reading an object multiple times
32      * when it is in several cells that you check.
33      */
34     int timestamp;
35     /// index in the objects vector
36     int id;
37   };
38  
39   /** Element for the single linked list in each grid cell */
40   struct GridEntry
41   {
42     GridEntry* next;
43     ObjectWrapper* object_wrapper;
44   };
45
46   void remove_object_from_gridcell(int gridcell, MovingObject* object);
47   void collide_object(ObjectWrapper* wrapper);
48   void collide_object_object(ObjectWrapper* wrapper, ObjectWrapper* wrapper2);
49   
50   typedef std::vector<GridEntry*> GridEntries;
51   GridEntries grid;
52   typedef std::vector<ObjectWrapper*> Objects;
53   Objects objects;
54   size_t cells_x, cells_y;
55   float width;
56   float height;
57   float cell_width;
58   float cell_height;
59 };
60
61 extern CollisionGrid* bla;
62
63 #endif
64