aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2016-01-25 11:12:31 +0100
committerraysan5 <raysan5@gmail.com>2016-01-25 11:12:31 +0100
commit3113a20390a1e4d81e9f832e7aa1d022afdb56d1 (patch)
treeadc2f2e480a0278e311fbd95e73cb3c6482657fb /src
parent41959eeae10d7d01fbd2abc19ccae4fc65aae031 (diff)
downloadraylib-3113a20390a1e4d81e9f832e7aa1d022afdb56d1.tar.gz
raylib-3113a20390a1e4d81e9f832e7aa1d022afdb56d1.zip
Added bounding box calculation
Diffstat (limited to 'src')
-rw-r--r--src/models.c29
-rw-r--r--src/raylib.h2
-rw-r--r--src/raymath.h26
3 files changed, 46 insertions, 11 deletions
diff --git a/src/models.c b/src/models.c
index e90f455a..2d78963e 100644
--- a/src/models.c
+++ b/src/models.c
@@ -1275,6 +1275,20 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vec
rlDisableTexture();
}
+// Draw a bounding box with wires
+void DrawBoundingBox(BoundingBox box)
+{
+ Vector3 size;
+
+ size.x = fabsf(box.max.x - box.min.x);
+ size.y = fabsf(box.max.y - box.min.y);
+ size.z = fabsf(box.max.z - box.min.z);
+
+ Vector3 center = { box.min.x + size.x/2.0f, box.min.y + size.y/2.0f, box.min.z + size.z/2.0f };
+
+ DrawCubeWires(center, size.x, size.y, size.z, GREEN);
+}
+
// Detect collision between two spheres
bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB)
{
@@ -1401,10 +1415,8 @@ bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox)
return collision;
}
-// TODO: Useful function to check collision area?
-//BoundingBox GetCollisionArea(BoundingBox box1, BoundingBox box2)
-
// Calculate mesh bounding box limits
+// NOTE: minVertex and maxVertex should be transformed by model transform matrix (position, scale, rotate)
BoundingBox CalculateBoundingBox(Mesh mesh)
{
// Get min and max vertex to construct bounds (AABB)
@@ -1413,15 +1425,10 @@ BoundingBox CalculateBoundingBox(Mesh mesh)
for (int i = 1; i < mesh.vertexCount; i++)
{
- // TODO: Compare min and max with previous vertex
- //minVertex = Vector3.Min(minVertex, mesh.vertices[i]);
- //maxVertex = Vector3.Max(maxVertex, mesh.vertices[i]);
+ minVertex = VectorMin(minVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] });
+ maxVertex = VectorMax(maxVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] });
}
-
- // NOTE: For OBB, transform mesh by model transform matrix
- //minVertex = VectorTransform(meshMin, mesh.transform);
- //maxVertex = VectorTransform(meshMax, mesh.transform);
-
+
// Create the bounding box
BoundingBox box;
box.min = minVertex;
diff --git a/src/raylib.h b/src/raylib.h
index 41fa3f7d..097c8865 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -765,10 +765,12 @@ void DrawModel(Model model, Vector3 position, float scale, Color tint);
void DrawModelEx(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color tint); // Draw a model with extended parameters
void DrawModelWires(Model model, Vector3 position, float scale, Color color); // Draw a model wires (with texture if set)
void DrawModelWiresEx(Model model, Vector3 position, float rotationAngle, Vector3 rotationAxis, Vector3 scale, Color tint); // Draw a model wires (with texture if set) with extended parameters
+void DrawBoundingBox(BoundingBox box); // Draw bounding box (wires)
void DrawBillboard(Camera camera, Texture2D texture, Vector3 center, float size, Color tint); // Draw a billboard texture
void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vector3 center, float size, Color tint); // Draw a billboard texture defined by sourceRec
+BoundingBox CalculateBoundingBox(Mesh mesh); // Calculate mesh bounding box limits
bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB); // Detect collision between two spheres
bool CheckCollisionBoxes(Vector3 minBBox1, Vector3 maxBBox1, Vector3 minBBox2, Vector3 maxBBox2); // Detect collision between two boxes
bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSphere, float radiusSphere); // Detect collision between box and sphere
diff --git a/src/raymath.h b/src/raymath.h
index f5448504..46fab356 100644
--- a/src/raymath.h
+++ b/src/raymath.h
@@ -126,6 +126,8 @@ RMDEF Vector3 VectorLerp(Vector3 v1, Vector3 v2, float amount); // Calculate lin
RMDEF Vector3 VectorReflect(Vector3 vector, Vector3 normal); // Calculate reflected vector to normal
RMDEF void VectorTransform(Vector3 *v, Matrix mat); // Transforms a Vector3 by a given Matrix
RMDEF Vector3 VectorZero(void); // Return a Vector3 init to zero
+RMDEF Vector3 VectorMin(Vector3 vec1, Vector3 vec2); // Return min value for each pair of components
+RMDEF Vector3 VectorMax(Vector3 vec1, Vector3 vec2); // Return max value for each pair of components
//------------------------------------------------------------------------------------
// Functions Declaration to work with Matrix
@@ -361,6 +363,30 @@ RMDEF Vector3 VectorZero(void)
return zero;
}
+// Return min value for each pair of components
+RMDEF Vector3 VectorMin(Vector3 vec1, Vector3 vec2)
+{
+ Vector3 result;
+
+ result.x = fminf(vec1.x, vec2.x);
+ result.y = fminf(vec1.y, vec2.y);
+ result.z = fminf(vec1.z, vec2.z);
+
+ return result;
+}
+
+// Return max value for each pair of components
+RMDEF Vector3 VectorMax(Vector3 vec1, Vector3 vec2)
+{
+ Vector3 result;
+
+ result.x = fmaxf(vec1.x, vec2.x);
+ result.y = fmaxf(vec1.y, vec2.y);
+ result.z = fmaxf(vec1.z, vec2.z);
+
+ return result;
+}
+
//----------------------------------------------------------------------------------
// Module Functions Definition - Matrix math
//----------------------------------------------------------------------------------