4 #include "collision_grid.h"
5 #include "special/collision.h"
8 static const float DELTA = .001;
10 CollisionGrid::CollisionGrid(float newwidth, float newheight)
11 : width(newwidth), height(newheight), cell_width(128), cell_height(128)
13 cells_x = size_t(width / cell_width) + 1;
14 cells_y = size_t(height / cell_height) + 1;
15 grid.resize(cells_x * cells_y);
18 CollisionGrid::~CollisionGrid()
20 for(GridEntries::iterator i = grid.begin(); i != grid.end(); ++i) {
21 GridEntry* entry = *i;
23 GridEntry* nextentry = entry->next;
31 CollisionGrid::add_object(MovingObject* object)
34 // make sure the object isn't already in the grid
35 for(Objects::iterator i = objects.begin(); i != objects.end(); ++i) {
36 ObjectWrapper* wrapper = *i;
37 if(wrapper->object == object)
43 ObjectWrapper* wrapper = new ObjectWrapper;
44 wrapper->object = object;
45 wrapper->timestamp = 0;
46 wrapper->dest = object->bbox;
47 objects.push_back(wrapper);
48 wrapper->id = objects.size()-1;
50 const Rectangle& bbox = object->bbox;
51 for(float y = bbox.p1.y; y < bbox.p2.y; y += cell_height) {
52 for(float x = bbox.p1.x; x < bbox.p2.x; x += cell_width) {
53 int gridx = int(x / cell_width);
54 int gridy = int(y / cell_height);
55 if(gridx < 0 || gridy < 0
56 || gridx >= int(cells_x) || gridy >= int(cells_y)) {
57 std::cerr << "Object out of range: " << gridx << ", " << gridy << "\n";
60 GridEntry* entry = new GridEntry;
61 entry->object_wrapper = wrapper;
62 entry->next = grid[gridy*cells_x + gridx];
63 grid[gridy*cells_x + gridx] = entry;
69 CollisionGrid::remove_object(MovingObject* object)
71 ObjectWrapper* wrapper = 0;
72 for(Objects::iterator i = objects.begin(); i != objects.end(); ++i) {
73 if((*i)->object == object) {
81 const Rectangle& bbox = wrapper->dest;
82 for(float y = bbox.p1.y; y < bbox.p2.y; y += cell_height) {
83 for(float x = bbox.p1.x; x < bbox.p2.x; x += cell_width) {
84 int gridx = int(x / cell_width);
85 int gridy = int(y / cell_height);
86 if(gridx < 0 || gridy < 0
87 || gridx >= int(cells_x) || gridy >= int(cells_y)) {
88 std::cerr << "Object out of range: " << gridx << ", " << gridy << "\n";
91 remove_object_from_gridcell(gridy*cells_x + gridx, object);
99 CollisionGrid::move_object(MovingObject* object)
101 const Rectangle& bbox = object->bbox;
102 for(float y = bbox.p1.y; y < bbox.p2.y; y += cell_height) {
103 for(float x = bbox.p1.x; x < bbox.p2.x; x += cell_width) {
104 int gridx = int(x / cell_width);
105 int gridy = int(y / cell_height);
106 if(gridx < 0 || gridy < 0
107 || gridx >= int(cells_x) || gridy >= int(cells_y)) {
108 std::cerr << "Object out of range: " << gridx << ", " << gridy << "\n";
117 CollisionGrid::check_collisions()
119 for(Objects::iterator i = objects.begin(); i != objects.end(); ++i) {
120 ObjectWrapper* wrapper = *i;
121 MovingObject* object = wrapper->object;
122 if(!object->is_valid())
124 if(object->get_flags() & GameObject::FLAG_NO_COLLDET) {
125 object->bbox.move(object->movement);
126 object->movement = Vector(0, 0);
131 Sector::current()->collision_tilemap(object, 0);
133 collide_object(wrapper);
135 object->bbox.move(object->get_movement());
136 object->movement = Vector(0, 0);
141 CollisionGrid::collide_object(ObjectWrapper* wrapper)
143 static int timestamp = 0;
146 const Rectangle& bbox = wrapper->object->bbox;
147 for(float y = bbox.p1.y; y < bbox.p2.y; y += cell_height) {
148 for(float x = bbox.p1.x; x < bbox.p2.x; x += cell_width) {
149 int gridx = int(x / cell_width);
150 int gridy = int(y / cell_height);
151 if(gridx < 0 || gridy < 0
152 || gridx >= int(cells_x) || gridy >= int(cells_y)) {
153 std::cerr << "Object out of range: " << gridx << ", " << gridy << "\n";
157 for(GridEntry* entry = grid[gridy*cells_x + gridx]; entry;
158 entry = entry->next) {
159 ObjectWrapper* wrapper2 = entry->object_wrapper;
160 // only check each object once (even if it is in multiple cells)
161 if(wrapper2->timestamp == timestamp)
163 // don't collide with objects we already collided with
164 if(wrapper2->id <= wrapper->id)
167 wrapper->timestamp = timestamp;
168 collide_object_object(wrapper, wrapper2);
175 CollisionGrid::collide_object_object(ObjectWrapper* wrapper,
176 ObjectWrapper* wrapper2)
179 MovingObject* object1 = wrapper->object;
180 MovingObject* object2 = wrapper2->object;
182 Rectangle dest1 = object1->get_bbox();
183 dest1.move(object1->get_movement());
184 Rectangle dest2 = object2->get_bbox();
185 dest2.move(object2->get_movement());
187 Vector movement = object1->get_movement() - object2->get_movement();
188 if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) {
189 HitResponse response1 = object1->collision(*object2, hit);
191 HitResponse response2 = object2->collision(*object1, hit);
193 if(response1 != CONTINUE) {
194 if(response1 == ABORT_MOVE)
195 object1->movement = Vector(0, 0);
196 if(response2 == CONTINUE)
197 object2->movement += hit.normal * (hit.depth + DELTA);
198 } else if(response2 != CONTINUE) {
199 if(response2 == ABORT_MOVE)
200 object2->movement = Vector(0, 0);
201 if(response1 == CONTINUE)
202 object1->movement += -hit.normal * (hit.depth + DELTA);
204 object1->movement += -hit.normal * (hit.depth/2 + DELTA);
205 object2->movement += hit.normal * (hit.depth/2 + DELTA);
211 CollisionGrid::remove_object_from_gridcell(int gridcell, MovingObject* object)
213 GridEntry* lastentry = 0;
214 GridEntry* entry = grid[gridcell];
217 if(entry->object_wrapper->object == object) {
219 grid[gridcell] = entry->next;
221 lastentry->next = entry->next;
231 std::cerr << "Couldn't find object in cell.\n";