4 #include "collision_grid.h"
5 #include "special/collision.h"
7 #include "collision_grid_iterator.h"
9 static const float DELTA = .001;
11 CollisionGrid::CollisionGrid(float newwidth, float newheight)
12 : width(newwidth), height(newheight), cell_width(128), cell_height(128),
15 cells_x = size_t(width / cell_width) + 1;
16 cells_y = size_t(height / cell_height) + 1;
17 grid.resize(cells_x * cells_y);
20 CollisionGrid::~CollisionGrid()
22 for(GridEntries::iterator i = grid.begin(); i != grid.end(); ++i) {
23 GridEntry* entry = *i;
25 GridEntry* nextentry = entry->next;
33 CollisionGrid::add_object(MovingObject* object)
36 // make sure the object isn't already in the grid
37 for(Objects::iterator i = objects.begin(); i != objects.end(); ++i) {
38 ObjectWrapper* wrapper = *i;
39 if(wrapper->object == object)
45 ObjectWrapper* wrapper = new ObjectWrapper;
46 wrapper->object = object;
47 wrapper->timestamp = 0;
48 wrapper->dest = object->bbox;
49 objects.push_back(wrapper);
50 wrapper->id = objects.size()-1;
52 const Rectangle& bbox = object->bbox;
53 for(float y = bbox.p1.y; y < bbox.p2.y; y += cell_height) {
54 for(float x = bbox.p1.x; x < bbox.p2.x; x += cell_width) {
55 int gridx = int(x / cell_width);
56 int gridy = int(y / cell_height);
57 if(gridx < 0 || gridy < 0
58 || gridx >= int(cells_x) || gridy >= int(cells_y)) {
59 std::cerr << "Object out of range: " << gridx << ", " << gridy << "\n";
62 GridEntry* entry = new GridEntry;
63 entry->object_wrapper = wrapper;
64 entry->next = grid[gridy*cells_x + gridx];
65 grid[gridy*cells_x + gridx] = entry;
71 CollisionGrid::remove_object(MovingObject* object)
73 ObjectWrapper* wrapper = 0;
74 for(Objects::iterator i = objects.begin(); i != objects.end(); ++i) {
75 if((*i)->object == object) {
83 const Rectangle& bbox = wrapper->dest;
84 for(float y = bbox.p1.y; y < bbox.p2.y; y += cell_height) {
85 for(float x = bbox.p1.x; x < bbox.p2.x; x += cell_width) {
86 int gridx = int(x / cell_width);
87 int gridy = int(y / cell_height);
88 if(gridx < 0 || gridy < 0
89 || gridx >= int(cells_x) || gridy >= int(cells_y)) {
90 std::cerr << "Object out of range: " << gridx << ", " << gridy << "\n";
93 remove_object_from_gridcell(gridy*cells_x + gridx, object);
101 CollisionGrid::move_object(MovingObject* object)
103 const Rectangle& bbox = object->bbox;
104 for(float y = bbox.p1.y; y < bbox.p2.y; y += cell_height) {
105 for(float x = bbox.p1.x; x < bbox.p2.x; x += cell_width) {
106 int gridx = int(x / cell_width);
107 int gridy = int(y / cell_height);
108 if(gridx < 0 || gridy < 0
109 || gridx >= int(cells_x) || gridy >= int(cells_y)) {
110 std::cerr << "Object out of range: " << gridx << ", " << gridy << "\n";
119 CollisionGrid::check_collisions()
121 CollisionGridIterator iter(*this, Sector::current()->get_active_region());
122 while(ObjectWrapper* wrapper = iter.next_wrapper()) {
123 MovingObject* object = wrapper->object;
124 if(!object->is_valid())
126 if(object->get_flags() & GameObject::FLAG_NO_COLLDET) {
127 object->bbox.move(object->movement);
128 object->movement = Vector(0, 0);
133 Sector::current()->collision_tilemap(object, 0);
135 collide_object(wrapper);
137 object->bbox.move(object->get_movement());
138 object->movement = Vector(0, 0);
143 CollisionGrid::collide_object(ObjectWrapper* wrapper)
145 iterator_timestamp++;
147 const Rectangle& bbox = wrapper->object->bbox;
148 for(float y = bbox.p1.y; y < bbox.p2.y; y += cell_height) {
149 for(float x = bbox.p1.x; x < bbox.p2.x; x += cell_width) {
150 int gridx = int(x / cell_width);
151 int gridy = int(y / cell_height);
152 if(gridx < 0 || gridy < 0
153 || gridx >= int(cells_x) || gridy >= int(cells_y)) {
154 std::cerr << "Object out of range: " << gridx << ", " << gridy << "\n";
158 for(GridEntry* entry = grid[gridy*cells_x + gridx]; entry;
159 entry = entry->next) {
160 ObjectWrapper* wrapper2 = entry->object_wrapper;
161 // only check each object once (even if it is in multiple cells)
162 if(wrapper2->timestamp == iterator_timestamp)
164 // don't collide with objects we already collided with
165 if(wrapper2->id <= wrapper->id)
168 wrapper->timestamp = iterator_timestamp;
169 collide_object_object(wrapper, wrapper2);
176 CollisionGrid::collide_object_object(ObjectWrapper* wrapper,
177 ObjectWrapper* wrapper2)
180 MovingObject* object1 = wrapper->object;
181 MovingObject* object2 = wrapper2->object;
183 Rectangle dest1 = object1->get_bbox();
184 dest1.move(object1->get_movement());
185 Rectangle dest2 = object2->get_bbox();
186 dest2.move(object2->get_movement());
188 Vector movement = object1->get_movement() - object2->get_movement();
189 if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
190 HitResponse response1 = object1->collision(*object2, hit);
192 HitResponse response2 = object2->collision(*object1, hit);
194 if(response1 != CONTINUE) {
195 if(response1 == ABORT_MOVE)
196 object1->movement = Vector(0, 0);
197 if(response2 == CONTINUE)
198 object2->movement += hit.normal * (hit.depth + DELTA);
199 } else if(response2 != CONTINUE) {
200 if(response2 == ABORT_MOVE)
201 object2->movement = Vector(0, 0);
202 if(response1 == CONTINUE)
203 object1->movement += -hit.normal * (hit.depth + DELTA);
205 object1->movement += -hit.normal * (hit.depth/2 + DELTA);
206 object2->movement += hit.normal * (hit.depth/2 + DELTA);
212 CollisionGrid::remove_object_from_gridcell(int gridcell, MovingObject* object)
214 GridEntry* lastentry = 0;
215 GridEntry* entry = grid[gridcell];
218 if(entry->object_wrapper->object == object) {
220 grid[gridcell] = entry->next;
222 lastentry->next = entry->next;
232 std::cerr << "Couldn't find object in cell.\n";