Added bounding box calculation

This commit is contained in:
raysan5 2016-01-25 11:12:31 +01:00
parent 41959eeae1
commit 3113a20390
3 changed files with 46 additions and 11 deletions

View File

@ -1275,6 +1275,20 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vec
rlDisableTexture(); 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 // Detect collision between two spheres
bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB) bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB)
{ {
@ -1401,10 +1415,8 @@ bool CheckCollisionRayBox(Ray ray, Vector3 minBBox, Vector3 maxBBox)
return collision; return collision;
} }
// TODO: Useful function to check collision area?
//BoundingBox GetCollisionArea(BoundingBox box1, BoundingBox box2)
// Calculate mesh bounding box limits // Calculate mesh bounding box limits
// NOTE: minVertex and maxVertex should be transformed by model transform matrix (position, scale, rotate)
BoundingBox CalculateBoundingBox(Mesh mesh) BoundingBox CalculateBoundingBox(Mesh mesh)
{ {
// Get min and max vertex to construct bounds (AABB) // 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++) for (int i = 1; i < mesh.vertexCount; i++)
{ {
// TODO: Compare min and max with previous vertex minVertex = VectorMin(minVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] });
//minVertex = Vector3.Min(minVertex, mesh.vertices[i]); maxVertex = VectorMax(maxVertex, (Vector3){ mesh.vertices[i*3], mesh.vertices[i*3 + 1], mesh.vertices[i*3 + 2] });
//maxVertex = Vector3.Max(maxVertex, mesh.vertices[i]);
} }
// NOTE: For OBB, transform mesh by model transform matrix
//minVertex = VectorTransform(meshMin, mesh.transform);
//maxVertex = VectorTransform(meshMax, mesh.transform);
// Create the bounding box // Create the bounding box
BoundingBox box; BoundingBox box;
box.min = minVertex; box.min = minVertex;

View File

@ -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 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 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 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 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 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 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 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 bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSphere, float radiusSphere); // Detect collision between box and sphere

View File

@ -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 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 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 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 // Functions Declaration to work with Matrix
@ -361,6 +363,30 @@ RMDEF Vector3 VectorZero(void)
return zero; 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 // Module Functions Definition - Matrix math
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------