From cfc4cc211af9e951d00bb23c58d3ba479cad625f Mon Sep 17 00:00:00 2001 From: Quentin BAZIN Date: Tue, 26 Feb 2013 00:07:14 +0100 Subject: [PATCH] All is working. --- TODO | 1 + include/cube.h | 1 + include/scene.h | 4 +-- source/biome.cpp | 10 ++++--- source/scene.cpp | 69 ++++++++++++++++++++++++++++-------------------- 5 files changed, 51 insertions(+), 34 deletions(-) diff --git a/TODO b/TODO index e69de29b..2f0fb22d 100755 --- a/TODO +++ b/TODO @@ -0,0 +1 @@ +- Improve vect3D diff --git a/include/cube.h b/include/cube.h index 54b28b07..9e704960 100644 --- a/include/cube.h +++ b/include/cube.h @@ -33,6 +33,7 @@ class Cube { int y() const { return m_y; } int z() const { return m_z; } + bool selected() const { return m_selected; } s8 selectedFace() const { return m_selectedFace; } private: diff --git a/include/scene.h b/include/scene.h index 8fac1ebb..dcf150cd 100644 --- a/include/scene.h +++ b/include/scene.h @@ -44,8 +44,8 @@ class Scene { void lockMouse(); void unlockMouse(); - static float intersectionLinePlane(vect3D normal, vect3D planePoint, vect3D lineOrigPoint, vect3D directionVector); - static float intersectionLineCube(int cubeX, int cubeY, int cubeZ, vect3D lineOrigPoint, vect3D directionVector); + static bool intersectionLinePlane(vect3D normal, vect3D planePoint, vect3D lineOrigPoint, vect3D directionVector, float *distance); + static bool intersectionLineCube(int cubeX, int cubeY, int cubeZ, vect3D lineOrigPoint, vect3D directionVector, float *distance, s8 *face); static void testCubes(std::vector cubes); static Player *player; diff --git a/source/biome.cpp b/source/biome.cpp index e31a947e..498c36b7 100644 --- a/source/biome.cpp +++ b/source/biome.cpp @@ -131,10 +131,12 @@ void Biome::draw() { void Biome::deleteCube(Cube *cube) { for(std::vector::iterator it = m_cubes.begin(); it != m_cubes.end() ; it++) { if((*it)->x() == cube->x() && (*it)->y() == cube->y() && (*it)->z() == cube->z()) { - m_map->map[MAP_POS(cube->x(), cube->y(), cube->z())] = 0; - delete (*it); - m_cubes.erase(it); - break; + if(cube->selected()) { + m_map->map[MAP_POS(cube->x(), cube->y(), cube->z())] = 0; + delete (*it); + m_cubes.erase(it); + break; + } } } } diff --git a/source/scene.cpp b/source/scene.cpp index 0c093477..c525a5eb 100644 --- a/source/scene.cpp +++ b/source/scene.cpp @@ -40,10 +40,10 @@ using namespace std; -float Scene::intersectionLinePlane(vect3D normal, vect3D planePoint, vect3D lineOrigPoint, vect3D directionVector) { +bool Scene::intersectionLinePlane(vect3D normal, vect3D planePoint, vect3D lineOrigPoint, vect3D directionVector, float *distance) { float p1 = directionVector.x * normal.x + directionVector.y * normal.y + directionVector.z * normal.z; // First point to be tested - if(p1 == 0) return -1; // Degenerate case + if(p1 == 0) return false; // Degenerate case vect3D u; // planePoint - lineOrigPoint @@ -70,22 +70,24 @@ float Scene::intersectionLinePlane(vect3D normal, vect3D planePoint, vect3D line float size = 0.5; if(v.x >= -size && v.x <= size && v.y >= -size && v.y <= size && v.z >= -size && v.z <= size) { - return k; + if(distance != NULL) *distance = k; + return true; } else { - return -1; + return false; } } -int face = -1; // Front right = 0 | Front left = 1 // Back right = 2 | Back left = 3 // Top = 4 | Bottom = 5 -float Scene::intersectionLineCube(int cubeX, int cubeY, int cubeZ, vect3D lineOrigPoint, vect3D directionVector) { +bool Scene::intersectionLineCube(int cubeX, int cubeY, int cubeZ, vect3D lineOrigPoint, vect3D directionVector, float *distance, s8 *face) { vect3D planePoint; vect3D normal; float k = 0; - float smallestK = -1; + float smallestK = FAR; + s8 nearestFace = -1; + bool result = false; // Front right planePoint.x = cubeX + 0.5; @@ -96,10 +98,10 @@ float Scene::intersectionLineCube(int cubeX, int cubeY, int cubeZ, vect3D lineOr normal.y = 1; normal.z = 0; - k = intersectionLinePlane(normal, planePoint, lineOrigPoint, directionVector); - if(k >= 0.0 && ((smallestK <= 0.0) || (smallestK > k))) { + result = intersectionLinePlane(normal, planePoint, lineOrigPoint, directionVector, &k); + if(result && (smallestK > k)) { smallestK = k; - face = 0; + nearestFace = 0; } // Front left @@ -111,10 +113,10 @@ float Scene::intersectionLineCube(int cubeX, int cubeY, int cubeZ, vect3D lineOr normal.y = 0; normal.z = 0; - k = intersectionLinePlane(normal, planePoint, lineOrigPoint, directionVector); - if(k >= 0.0 && ((smallestK <= 0.0) || (smallestK > k))) { + result = intersectionLinePlane(normal, planePoint, lineOrigPoint, directionVector, &k); + if(result && (smallestK > k)) { smallestK = k; - face = 1; + nearestFace = 1; } // Back left @@ -126,10 +128,10 @@ float Scene::intersectionLineCube(int cubeX, int cubeY, int cubeZ, vect3D lineOr normal.y = -1; normal.z = 0; - k = intersectionLinePlane(normal, planePoint, lineOrigPoint, directionVector); - if(k >= 0.0 && ((smallestK <= 0.0) || (smallestK > k))) { + result = intersectionLinePlane(normal, planePoint, lineOrigPoint, directionVector, &k); + if(result && (smallestK > k)) { smallestK = k; - face = 3; + nearestFace = 3; } // Back right @@ -141,10 +143,10 @@ float Scene::intersectionLineCube(int cubeX, int cubeY, int cubeZ, vect3D lineOr normal.y = 0; normal.z = 0; - k = intersectionLinePlane(normal, planePoint, lineOrigPoint, directionVector); - if(k >= 0.0 && ((smallestK <= 0.0) || (smallestK > k))) { + result = intersectionLinePlane(normal, planePoint, lineOrigPoint, directionVector, &k); + if(result && (smallestK > k)) { smallestK = k; - face = 2; + nearestFace = 2; } // Bottom @@ -156,10 +158,10 @@ float Scene::intersectionLineCube(int cubeX, int cubeY, int cubeZ, vect3D lineOr normal.y = 0; normal.z = -1; - k = intersectionLinePlane(normal, planePoint, lineOrigPoint, directionVector); - if(k >= 0.0 && ((smallestK <= 0.0) || (smallestK > k))) { + result = intersectionLinePlane(normal, planePoint, lineOrigPoint, directionVector, &k); + if(result && (smallestK > k)) { smallestK = k; - face = 5; + nearestFace = 5; } // Top @@ -171,13 +173,19 @@ float Scene::intersectionLineCube(int cubeX, int cubeY, int cubeZ, vect3D lineOr normal.y = 0; normal.z = 1; - k = intersectionLinePlane(normal, planePoint, lineOrigPoint, directionVector); - if(k >= 0.0 && ((smallestK <= 0.0) || (smallestK > k))) { + result = intersectionLinePlane(normal, planePoint, lineOrigPoint, directionVector, &k); + if(result && (smallestK > k)) { smallestK = k; - face = 4; + nearestFace = 4; } - return smallestK; + if(nearestFace < 0) { + return false; + } else { + if(distance != NULL) *distance = smallestK; + if(face != NULL) *face = nearestFace; + return true; + } } void Scene::testCubes(std::vector cubes) { @@ -195,14 +203,19 @@ void Scene::testCubes(std::vector cubes) { float distance = FAR; Cube *cube = NULL; + int face = -1; for(std::vector::iterator it = cubes.begin() ; it != cubes.end() ; it++) { (*it)->setSelected(false, -1); - float d = intersectionLineCube((*it)->x() + biome->x(), (*it)->y() + biome->y(), (*it)->z() + biome->z(), linePoint, directionVector); + float d = -1; + s8 f = -1; - if(d >= 0.0 && d < distance) { + bool result = intersectionLineCube((*it)->x() + biome->x(), (*it)->y() + biome->y(), (*it)->z() + biome->z(), linePoint, directionVector, &d, &f); + + if(result && (d < distance) && (d < 5)) { distance = d; cube = (*it); + face = f; } }