Browse Source

renamed collision boxes to box

Kajetan Johannes Hammerle 2 years ago
parent
commit
0c3c3553af

+ 3 - 3
common/Block.cpp

@@ -2,7 +2,7 @@
 #include "utils/Array.h"
 #include "utils/ArrayList.h"
 
-static Array<ArrayList<CollisionBox, 1>, Block::MAX> collisionBoxes;
+static Array<ArrayList<Box, 1>, Block::MAX> collisionBoxes;
 
 void Block::init() {
     for(int i = 1; i < collisionBoxes.getLength(); i++) {
@@ -10,8 +10,8 @@ void Block::init() {
     }
 }
 
-void Block::addBoxes(Id block, List<CollisionBox>& list, const Vector3& pos) {
-    for(const CollisionBox& box : collisionBoxes[block]) {
+void Block::addBoxes(Id block, List<Box>& list, const Vector3& pos) {
+    for(const Box& box : collisionBoxes[block]) {
         list.add(box.offset(pos));
     }
 }

+ 2 - 2
common/Block.h

@@ -1,7 +1,7 @@
 #ifndef BLOCK_H
 #define BLOCK_H
 
-#include "common/utils/CollisionBox.h"
+#include "common/Box.h"
 #include "utils/List.h"
 #include "utils/Types.h"
 
@@ -10,7 +10,7 @@ namespace Block {
     typedef uint16 Id;
 
     void init();
-    void addBoxes(Id block, List<CollisionBox>& list, const Vector3& pos);
+    void addBoxes(Id block, List<Box>& list, const Vector3& pos);
 }
 
 #endif

+ 33 - 0
common/Box.cpp

@@ -0,0 +1,33 @@
+#include "common/Box.h"
+
+Box::Box(const Vector3& min, const Vector3& max) : min(min), max(max) {
+}
+
+Box::Box(const Vector3& size) : max(size) {
+}
+
+Box Box::offset(const Vector3& offset) const {
+    return Box(offset + min, offset + max);
+}
+
+bool Box::collidesWith(const Box& other) const {
+    return max[0] > other.min[0] && min[0] < other.max[0] &&
+           max[1] > other.min[1] && min[1] < other.max[1] &&
+           max[2] > other.min[2] && min[2] < other.max[2];
+}
+
+Box Box::expand(const Vector3& offset) const {
+    Vector3 add(offset[0] * (offset[0] >= 0.0), offset[1] * (offset[1] >= 0.0),
+                offset[2] * (offset[2] >= 0.0));
+    Vector3 sub(offset[0] * (offset[0] <= 0.0), offset[1] * (offset[1] <= 0.0),
+                offset[2] * (offset[2] <= 0.0));
+    return Box(min + sub, max + add);
+}
+
+const Vector3& Box::getMin() const {
+    return min;
+}
+
+const Vector3& Box::getMax() const {
+    return max;
+}

+ 29 - 0
common/Box.h

@@ -0,0 +1,29 @@
+#ifndef COLLISION_BOX_H
+#define COLLISION_BOX_H
+
+#include "math/Vector.h"
+#include "utils/StringBuffer.h"
+
+class Box {
+    Vector3 min;
+    Vector3 max;
+
+    Box(const Vector3& min, const Vector3& max);
+
+public:
+    Box(const Vector3& size);
+
+    Box offset(const Vector3& offset) const;
+    bool collidesWith(const Box& other) const;
+    Box expand(const Vector3& offset) const;
+
+    const Vector3& getMin() const;
+    const Vector3& getMax() const;
+
+    template<int L>
+    void toString(StringBuffer<L>& s) const {
+        s.append("Box(").append(min).append(", ").append(max).append(")");
+    }
+};
+
+#endif

+ 2 - 2
common/entities/Entity.cpp

@@ -36,8 +36,8 @@ void Entity::move(const Vector3& v) {
     velocity = v;
 }
 
-CollisionBox Entity::getCollisionBox() const {
-    return CollisionBox(size).offset(position);
+Box Entity::getBox() const {
+    return Box(size).offset(position);
 }
 
 bool Entity::isOnGround() const {

+ 2 - 2
common/entities/Entity.h

@@ -1,7 +1,7 @@
 #ifndef ENTITY_H
 #define ENTITY_H
 
-#include "common/utils/CollisionBox.h"
+#include "common/Box.h"
 #include "math/Quaternion.h"
 
 struct Entity {
@@ -28,7 +28,7 @@ struct Entity {
     void addForce(const Vector3& force);
     const Vector3& getVelocity() const;
     void move(const Vector3& v);
-    CollisionBox getCollisionBox() const;
+    Box getBox() const;
     bool isOnGround() const;
 
     void setPosition(const Vector3& pos);

+ 0 - 34
common/utils/CollisionBox.cpp

@@ -1,34 +0,0 @@
-#include "common/utils/CollisionBox.h"
-
-CollisionBox::CollisionBox(const Vector3& min, const Vector3& max)
-    : min(min), max(max) {
-}
-
-CollisionBox::CollisionBox(const Vector3& size) : max(size) {
-}
-
-CollisionBox CollisionBox::offset(const Vector3& offset) const {
-    return CollisionBox(offset + min, offset + max);
-}
-
-bool CollisionBox::collidesWith(const CollisionBox& other) const {
-    return max[0] > other.min[0] && min[0] < other.max[0] &&
-           max[1] > other.min[1] && min[1] < other.max[1] &&
-           max[2] > other.min[2] && min[2] < other.max[2];
-}
-
-CollisionBox CollisionBox::expand(const Vector3& offset) const {
-    Vector3 add(offset[0] * (offset[0] >= 0.0), offset[1] * (offset[1] >= 0.0),
-                offset[2] * (offset[2] >= 0.0));
-    Vector3 sub(offset[0] * (offset[0] <= 0.0), offset[1] * (offset[1] <= 0.0),
-                offset[2] * (offset[2] <= 0.0));
-    return CollisionBox(min + sub, max + add);
-}
-
-const Vector3& CollisionBox::getMin() const {
-    return min;
-}
-
-const Vector3& CollisionBox::getMax() const {
-    return max;
-}

+ 0 - 33
common/utils/CollisionBox.h

@@ -1,33 +0,0 @@
-#ifndef COLLISION_BOX_H
-#define COLLISION_BOX_H
-
-#include "math/Vector.h"
-#include "utils/StringBuffer.h"
-
-class CollisionBox {
-    Vector3 min;
-    Vector3 max;
-
-    CollisionBox(const Vector3& min, const Vector3& max);
-
-public:
-    CollisionBox(const Vector3& size);
-
-    CollisionBox offset(const Vector3& offset) const;
-    bool collidesWith(const CollisionBox& other) const;
-    CollisionBox expand(const Vector3& offset) const;
-
-    const Vector3& getMin() const;
-    const Vector3& getMax() const;
-
-    template<int L>
-    void toString(StringBuffer<L>& s) const {
-        s.append("CollisionBox(")
-            .append(min)
-            .append(", ")
-            .append(max)
-            .append(")");
-    }
-};
-
-#endif

+ 8 - 8
common/world/World.cpp

@@ -37,7 +37,7 @@ void World::removeEntity(Entity* e) {
     }
 }
 
-List<CollisionBox> World::getBoxes(const CollisionBox& box) const {
+List<Box> World::getBoxes(const Box& box) const {
     int minX = floorf(box.getMin()[0]);
     int minY = floorf(box.getMin()[1]);
     int minZ = floorf(box.getMin()[2]);
@@ -45,7 +45,7 @@ List<CollisionBox> World::getBoxes(const CollisionBox& box) const {
     int maxY = floorf(box.getMax()[1]);
     int maxZ = floorf(box.getMax()[2]);
 
-    List<CollisionBox> boxes;
+    List<Box> boxes;
     for(int x = minX; x <= maxX; x++) {
         for(int y = minY; y <= maxY; y++) {
             for(int z = minZ; z <= maxZ; z++) {
@@ -70,8 +70,8 @@ void World::tick() {
 }
 
 Vector3 World::limitMove(const Entity& e, Vector3 move) const {
-    CollisionBox box = e.getCollisionBox();
-    List<CollisionBox> boxes = getBoxes(box.expand(move));
+    Box box = e.getBox();
+    List<Box> boxes = getBoxes(box.expand(move));
     if(boxes.getLength() == 0) {
         return move;
     }
@@ -90,8 +90,8 @@ Vector3 World::limitMove(const Entity& e, Vector3 move) const {
             realMove[0] += move[0];
             move[0] = 0.0f;
         }
-        CollisionBox moved = box.offset(realMove);
-        for(const CollisionBox& box : boxes) {
+        Box moved = box.offset(realMove);
+        for(const Box& box : boxes) {
             if(box.collidesWith(moved)) {
                 move[0] = 0.0f;
                 realMove = old;
@@ -111,7 +111,7 @@ Vector3 World::limitMove(const Entity& e, Vector3 move) const {
             move[1] = 0.0f;
         }
         moved = box.offset(realMove);
-        for(const CollisionBox& box : boxes) {
+        for(const Box& box : boxes) {
             if(box.collidesWith(moved)) {
                 move[1] = 0.0f;
                 realMove = old;
@@ -131,7 +131,7 @@ Vector3 World::limitMove(const Entity& e, Vector3 move) const {
             move[2] = 0.0f;
         }
         moved = box.offset(realMove);
-        for(const CollisionBox& box : boxes) {
+        for(const Box& box : boxes) {
             if(box.collidesWith(moved)) {
                 move[2] = 0.0f;
                 realMove = old;

+ 1 - 1
common/world/World.h

@@ -28,7 +28,7 @@ public:
     Vector3 limitMove(const Entity& e, Vector3 move) const;
 
 private:
-    List<CollisionBox> getBoxes(const CollisionBox& box) const;
+    List<Box> getBoxes(const Box& box) const;
 };
 
 #endif

+ 1 - 1
meson.build

@@ -6,7 +6,7 @@ src_common = [
     'common/world/BlockStorage.cpp',
     'common/world/World.cpp', 
     'common/world/HighMap.cpp',
-    'common/utils/CollisionBox.cpp',
+    'common/Box.cpp',
     'common/entities/Entity.cpp',
     'common/network/toserver/PlayerUpdatePacket.cpp',
     'common/network/toclient/EntityUpdatePacket.cpp',