Playing around with it some more to see how a move would work.

main
Zed A. Shaw 1 month ago
parent 98baa13264
commit 98993481b0
  1. 42
      scratchpad/collider.cpp

@ -1,6 +1,9 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include <fmt/core.h>
using namespace fmt;
struct Point { struct Point {
int x, y; int x, y;
@ -22,13 +25,17 @@ struct PointHash {
}; };
class SpatialHashTable { class SpatialHashTable {
public: public:
SpatialHashTable() {} SpatialHashTable() {}
void insert(Object* obj) { void insert(Object* obj) {
table[obj->position].push_back(obj); table[obj->position].push_back(obj);
} }
void remove(Object* obj) {
table.erase(obj->position);
}
std::vector<Object*> getNearbyObjects(Point position) { std::vector<Object*> getNearbyObjects(Point position) {
std::vector<Object*> result; std::vector<Object*> result;
Point cell = position; Point cell = position;
@ -47,7 +54,7 @@ public:
return result; return result;
} }
private: private:
std::unordered_map<Point, std::vector<Object*>, PointHash> table; std::unordered_map<Point, std::vector<Object*>, PointHash> table;
}; };
@ -62,8 +69,37 @@ int main() {
hashTable.insert(&bomb); hashTable.insert(&bomb);
std::vector<Object*> nearby = hashTable.getNearbyObjects({24, 24}); std::vector<Object*> nearby = hashTable.getNearbyObjects({24, 24});
for (Object* obj : nearby) {
println("{},{}", obj->position.x, obj->position.y);
}
println("AFTER MOVE");
// now attempt a move
hashTable.remove(&bomb);
bomb.position.x += 1;
bomb.position.y += 1;
hashTable.insert(&bomb);
nearby = hashTable.getNearbyObjects({24, 24});
for (Object* obj : nearby) {
println("{},{}", obj->position.x, obj->position.y);
}
println("AFTER MOVE BACK");
// now attempt a move
hashTable.remove(&bomb);
bomb.position.x -= 3;
bomb.position.y -= 2;
hashTable.insert(&bomb);
nearby = hashTable.getNearbyObjects({24, 24});
for (Object* obj : nearby) { for (Object* obj : nearby) {
std::cout << obj->position.x << ", " << obj->position.y << std::endl; println("{},{}", obj->position.x, obj->position.y);
} }
return 0; return 0;

Loading…
Cancel
Save