WARNING: BREAKING: RENAMED: RayHitInfo to RayCollision #1781

- RENAMED: RayHitInfo to RayCollision
- RENAMED/REDESIGNED: Multiple Ray collision functions to be more consistent and return RayCollision data -WIP-
This commit is contained in:
Ray 2021-05-31 11:41:56 +02:00
parent 7bcb2ad4f1
commit 1c5de9721a
3 changed files with 85 additions and 103 deletions

View File

@ -63,53 +63,53 @@ int main(void)
UpdateCamera(&camera); // Update camera UpdateCamera(&camera); // Update camera
// Display information about closest hit // Display information about closest hit
RayHitInfo nearestHit = { 0 }; RayCollision collision = { 0 };
char *hitObjectName = "None"; char *hitObjectName = "None";
nearestHit.distance = FLT_MAX; collision.distance = FLT_MAX;
nearestHit.hit = false; collision.hit = false;
Color cursorColor = WHITE; Color cursorColor = WHITE;
// Get ray and test against ground, triangle, and mesh // Get ray and test against ground, triangle, and mesh
ray = GetMouseRay(GetMousePosition(), camera); ray = GetMouseRay(GetMousePosition(), camera);
// Check ray collision aginst ground plane // Check ray collision aginst ground plane
RayHitInfo groundHitInfo = GetCollisionRayGround(ray, 0.0f); RayCollision groundHitInfo = GetRayCollisionGround(ray, 0.0f);
if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance)) if ((groundHitInfo.hit) && (groundHitInfo.distance < collision.distance))
{ {
nearestHit = groundHitInfo; collision = groundHitInfo;
cursorColor = GREEN; cursorColor = GREEN;
hitObjectName = "Ground"; hitObjectName = "Ground";
} }
// Check ray collision against test triangle // Check ray collision against test triangle
RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, ta, tb, tc); RayCollision triHitInfo = GetRayCollisionTriangle(ray, ta, tb, tc);
if ((triHitInfo.hit) && (triHitInfo.distance < nearestHit.distance)) if ((triHitInfo.hit) && (triHitInfo.distance < collision.distance))
{ {
nearestHit = triHitInfo; collision = triHitInfo;
cursorColor = PURPLE; cursorColor = PURPLE;
hitObjectName = "Triangle"; hitObjectName = "Triangle";
bary = Vector3Barycenter(nearestHit.position, ta, tb, tc); bary = Vector3Barycenter(collision.point, ta, tb, tc);
hitTriangle = true; hitTriangle = true;
} }
else hitTriangle = false; else hitTriangle = false;
RayHitInfo meshHitInfo = { 0 }; RayCollision meshHitInfo = { 0 };
// Check ray collision against bounding box first, before trying the full ray-mesh test // Check ray collision against bounding box first, before trying the full ray-mesh test
if (CheckCollisionRayBox(ray, towerBBox)) if (GetRayCollisionBox(ray, towerBBox).hit)
{ {
hitMeshBBox = true; hitMeshBBox = true;
// Check ray collision against model // Check ray collision against model
// NOTE: It considers model.transform matrix! // NOTE: It considers model.transform matrix!
meshHitInfo = GetCollisionRayModel(ray, tower); meshHitInfo = GetRayCollisionModel(ray, tower);
if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance)) if ((meshHitInfo.hit) && (meshHitInfo.distance < collision.distance))
{ {
nearestHit = meshHitInfo; collision = meshHitInfo;
cursorColor = ORANGE; cursorColor = ORANGE;
hitObjectName = "Mesh"; hitObjectName = "Mesh";
} }
@ -128,7 +128,7 @@ int main(void)
// Draw the tower // Draw the tower
// WARNING: If scale is different than 1.0f, // WARNING: If scale is different than 1.0f,
// not considered by GetCollisionRayModel() // not considered by GetRayCollisionModel()
DrawModel(tower, towerPos, 1.0f, WHITE); DrawModel(tower, towerPos, 1.0f, WHITE);
// Draw the test triangle // Draw the test triangle
@ -140,17 +140,17 @@ int main(void)
if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME); if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME);
// If we hit something, draw the cursor at the hit point // If we hit something, draw the cursor at the hit point
if (nearestHit.hit) if (collision.hit)
{ {
DrawCube(nearestHit.position, 0.3f, 0.3f, 0.3f, cursorColor); DrawCube(collision.point, 0.3f, 0.3f, 0.3f, cursorColor);
DrawCubeWires(nearestHit.position, 0.3f, 0.3f, 0.3f, RED); DrawCubeWires(collision.point, 0.3f, 0.3f, 0.3f, RED);
Vector3 normalEnd; Vector3 normalEnd;
normalEnd.x = nearestHit.position.x + nearestHit.normal.x; normalEnd.x = collision.point.x + collision.normal.x;
normalEnd.y = nearestHit.position.y + nearestHit.normal.y; normalEnd.y = collision.point.y + collision.normal.y;
normalEnd.z = nearestHit.position.z + nearestHit.normal.z; normalEnd.z = collision.point.z + collision.normal.z;
DrawLine3D(nearestHit.position, normalEnd, RED); DrawLine3D(collision.point, normalEnd, RED);
} }
DrawRay(ray, MAROON); DrawRay(ray, MAROON);
@ -162,21 +162,21 @@ int main(void)
// Draw some debug GUI text // Draw some debug GUI text
DrawText(TextFormat("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK); DrawText(TextFormat("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK);
if (nearestHit.hit) if (collision.hit)
{ {
int ypos = 70; int ypos = 70;
DrawText(TextFormat("Distance: %3.2f", nearestHit.distance), 10, ypos, 10, BLACK); DrawText(TextFormat("Distance: %3.2f", collision.distance), 10, ypos, 10, BLACK);
DrawText(TextFormat("Hit Pos: %3.2f %3.2f %3.2f", DrawText(TextFormat("Hit Pos: %3.2f %3.2f %3.2f",
nearestHit.position.x, collision.point.x,
nearestHit.position.y, collision.point.y,
nearestHit.position.z), 10, ypos + 15, 10, BLACK); collision.point.z), 10, ypos + 15, 10, BLACK);
DrawText(TextFormat("Hit Norm: %3.2f %3.2f %3.2f", DrawText(TextFormat("Hit Norm: %3.2f %3.2f %3.2f",
nearestHit.normal.x, collision.normal.x,
nearestHit.normal.y, collision.normal.y,
nearestHit.normal.z), 10, ypos + 30, 10, BLACK); collision.normal.z), 10, ypos + 30, 10, BLACK);
if (hitTriangle) DrawText(TextFormat("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK); if (hitTriangle) DrawText(TextFormat("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
} }

View File

@ -2979,53 +2979,32 @@ bool CheckCollisionBoxSphere(BoundingBox box, Vector3 center, float radius)
return collision; return collision;
} }
// Detect collision between ray and sphere // Get collision info between ray and sphere
bool CheckCollisionRaySphere(Ray ray, Vector3 center, float radius) RayCollision GetRayCollisionSphere(Ray ray, Vector3 center, float radius)
{ {
bool collision = false; RayCollision collision = { 0 };
Vector3 raySpherePos = Vector3Subtract(center, ray.position); Vector3 raySpherePos = Vector3Subtract(center, ray.position);
float distance = Vector3Length(raySpherePos); float distance = Vector3Length(raySpherePos);
float vector = Vector3DotProduct(raySpherePos, ray.direction); float vector = Vector3DotProduct(raySpherePos, ray.direction);
float d = radius*radius - (distance*distance - vector*vector); float d = radius*radius - (distance*distance - vector*vector);
if (d >= 0.0f) collision = true; if (d >= 0.0f) collision.hit = true;
return collision;
}
// Detect collision between ray and sphere with extended parameters and collision point detection
bool CheckCollisionRaySphereEx(Ray ray, Vector3 center, float radius, Vector3 *collisionPoint)
{
bool collision = false;
Vector3 raySpherePos = Vector3Subtract(center, ray.position);
float distance = Vector3Length(raySpherePos);
float vector = Vector3DotProduct(raySpherePos, ray.direction);
float d = radius*radius - (distance*distance - vector*vector);
if (d >= 0.0f) collision = true;
// Check if ray origin is inside the sphere to calculate the correct collision point // Check if ray origin is inside the sphere to calculate the correct collision point
float collisionDistance = 0; if (distance < radius) collision.distance = vector + sqrtf(d);
else collision.distance = vector - sqrtf(d);
if (distance < radius) collisionDistance = vector + sqrtf(d);
else collisionDistance = vector - sqrtf(d);
// Calculate collision point // Calculate collision point
Vector3 cPoint = Vector3Add(ray.position, Vector3Scale(ray.direction, collisionDistance)); collision.point = Vector3Add(ray.position, Vector3Scale(ray.direction, collision.distance));
collisionPoint->x = cPoint.x;
collisionPoint->y = cPoint.y;
collisionPoint->z = cPoint.z;
return collision; return collision;
} }
// Detect collision between ray and bounding box // Get collision info between ray and box
bool CheckCollisionRayBox(Ray ray, BoundingBox box) RayCollision GetRayCollisionBox(Ray ray, BoundingBox box)
{ {
bool collision = false; RayCollision collision = { 0 };
float t[8] = { 0 }; float t[8] = { 0 };
t[0] = (box.min.x - ray.position.x)/ray.direction.x; t[0] = (box.min.x - ray.position.x)/ray.direction.x;
@ -3037,14 +3016,17 @@ bool CheckCollisionRayBox(Ray ray, BoundingBox box)
t[6] = (float)fmax(fmax(fmin(t[0], t[1]), fmin(t[2], t[3])), fmin(t[4], t[5])); t[6] = (float)fmax(fmax(fmin(t[0], t[1]), fmin(t[2], t[3])), fmin(t[4], t[5]));
t[7] = (float)fmin(fmin(fmax(t[0], t[1]), fmax(t[2], t[3])), fmax(t[4], t[5])); t[7] = (float)fmin(fmin(fmax(t[0], t[1]), fmax(t[2], t[3])), fmax(t[4], t[5]));
collision = !(t[7] < 0 || t[6] > t[7]); collision.hit = !(t[7] < 0 || t[6] > t[7]);
// TODO: Calculate other RayCollision data
return collision; return collision;
} }
// Get collision info between ray and mesh // Get collision info between ray and mesh
RayHitInfo GetCollisionRayMesh(Ray ray, Mesh mesh, Matrix transform) RayCollision GetRayCollisionMesh(Ray ray, Mesh mesh, Matrix transform)
{ {
RayHitInfo result = { 0 }; RayCollision collision = { 0 };
// Check if mesh vertex data on CPU for testing // Check if mesh vertex data on CPU for testing
if (mesh.vertices != NULL) if (mesh.vertices != NULL)
@ -3074,47 +3056,49 @@ RayHitInfo GetCollisionRayMesh(Ray ray, Mesh mesh, Matrix transform)
b = Vector3Transform(b, transform); b = Vector3Transform(b, transform);
c = Vector3Transform(c, transform); c = Vector3Transform(c, transform);
RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, a, b, c); RayCollision triHitInfo = GetRayCollisionTriangle(ray, a, b, c);
if (triHitInfo.hit) if (triHitInfo.hit)
{ {
// Save the closest hit triangle // Save the closest hit triangle
if ((!result.hit) || (result.distance > triHitInfo.distance)) result = triHitInfo; if ((!collision.hit) || (collision.distance > triHitInfo.distance)) collision = triHitInfo;
} }
} }
} }
return result;
return collision;
} }
// Get collision info between ray and model // Get collision info between ray and model
RayHitInfo GetCollisionRayModel(Ray ray, Model model) RayCollision GetRayCollisionModel(Ray ray, Model model)
{ {
RayHitInfo result = { 0 }; RayCollision collision = { 0 };
for (int m = 0; m < model.meshCount; m++) for (int m = 0; m < model.meshCount; m++)
{ {
RayHitInfo meshHitInfo = GetCollisionRayMesh(ray, model.meshes[m], model.transform); RayCollision meshHitInfo = GetRayCollisionMesh(ray, model.meshes[m], model.transform);
if (meshHitInfo.hit) if (meshHitInfo.hit)
{ {
// Save the closest hit mesh // Save the closest hit mesh
if ((!result.hit) || (result.distance > meshHitInfo.distance)) result = meshHitInfo; if ((!collision.hit) || (collision.distance > meshHitInfo.distance)) collision = meshHitInfo;
} }
} }
return result; return collision;
} }
// Get collision info between ray and triangle // Get collision info between ray and triangle
// NOTE: Based on https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm // NOTE: Based on https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3) RayCollision GetRayCollisionTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3)
{ {
#define EPSILON 0.000001 // A small number #define EPSILON 0.000001 // A small number
Vector3 edge1, edge2; RayCollision collision = { 0 };
Vector3 edge1 = { 0 };
Vector3 edge2 = { 0 };
Vector3 p, q, tv; Vector3 p, q, tv;
float det, invDet, u, v, t; float det, invDet, u, v, t;
RayHitInfo result = {0};
// Find vectors for two edges sharing V1 // Find vectors for two edges sharing V1
edge1 = Vector3Subtract(p2, p1); edge1 = Vector3Subtract(p2, p1);
@ -3127,7 +3111,7 @@ RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3)
det = Vector3DotProduct(edge1, p); det = Vector3DotProduct(edge1, p);
// Avoid culling! // Avoid culling!
if ((det > -EPSILON) && (det < EPSILON)) return result; if ((det > -EPSILON) && (det < EPSILON)) return collision;
invDet = 1.0f/det; invDet = 1.0f/det;
@ -3138,7 +3122,7 @@ RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3)
u = Vector3DotProduct(tv, p)*invDet; u = Vector3DotProduct(tv, p)*invDet;
// The intersection lies outside of the triangle // The intersection lies outside of the triangle
if ((u < 0.0f) || (u > 1.0f)) return result; if ((u < 0.0f) || (u > 1.0f)) return collision;
// Prepare to test v parameter // Prepare to test v parameter
q = Vector3CrossProduct(tv, edge1); q = Vector3CrossProduct(tv, edge1);
@ -3147,29 +3131,28 @@ RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3)
v = Vector3DotProduct(ray.direction, q)*invDet; v = Vector3DotProduct(ray.direction, q)*invDet;
// The intersection lies outside of the triangle // The intersection lies outside of the triangle
if ((v < 0.0f) || ((u + v) > 1.0f)) return result; if ((v < 0.0f) || ((u + v) > 1.0f)) return collision;
t = Vector3DotProduct(edge2, q)*invDet; t = Vector3DotProduct(edge2, q)*invDet;
if (t > EPSILON) if (t > EPSILON)
{ {
// Ray hit, get hit point and normal // Ray hit, get hit point and normal
result.hit = true; collision.hit = true;
result.distance = t; collision.distance = t;
result.hit = true; collision.normal = Vector3Normalize(Vector3CrossProduct(edge1, edge2));
result.normal = Vector3Normalize(Vector3CrossProduct(edge1, edge2)); collision.point = Vector3Add(ray.position, Vector3Scale(ray.direction, t));
result.position = Vector3Add(ray.position, Vector3Scale(ray.direction, t));
} }
return result; return collision;
} }
// Get collision info between ray and ground plane (Y-normal plane) // Get collision info between ray and ground plane (Y-normal plane)
RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight) RayCollision GetRayCollisionGround(Ray ray, float groundHeight)
{ {
#define EPSILON 0.000001 // A small number #define EPSILON 0.000001 // A small number
RayHitInfo result = { 0 }; RayCollision collision = { 0 };
if (fabsf(ray.direction.y) > EPSILON) if (fabsf(ray.direction.y) > EPSILON)
{ {
@ -3177,15 +3160,15 @@ RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight)
if (distance >= 0.0) if (distance >= 0.0)
{ {
result.hit = true; collision.hit = true;
result.distance = distance; collision.distance = distance;
result.normal = (Vector3){ 0.0, 1.0, 0.0 }; collision.normal = (Vector3){ 0.0, 1.0, 0.0 };
result.position = Vector3Add(ray.position, Vector3Scale(ray.direction, distance)); collision.point = Vector3Add(ray.position, Vector3Scale(ray.direction, distance));
result.position.y = groundHeight; collision.point.y = groundHeight;
} }
} }
return result; return collision;
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------

View File

@ -399,12 +399,12 @@ typedef struct Ray {
} Ray; } Ray;
// RayCollision, ray hit information // RayCollision, ray hit information
typedef struct RayHitInfo { typedef struct RayCollision {
bool hit; // Did the ray hit something? bool hit; // Did the ray hit something?
float distance; // Distance to nearest hit float distance; // Distance to nearest hit
Vector3 position; // Position of nearest hit Vector3 point; // Point of nearest hit
Vector3 normal; // Surface normal of hit Vector3 normal; // Surface normal of hit
} RayHitInfo; } RayCollision;
// BoundingBox // BoundingBox
typedef struct BoundingBox { typedef struct BoundingBox {
@ -1445,13 +1445,12 @@ RLAPI void DrawBillboardPro(Camera camera, Texture2D texture, Rectangle source,
RLAPI bool CheckCollisionSpheres(Vector3 center1, float radius1, Vector3 center2, float radius2); // Detect collision between two spheres RLAPI bool CheckCollisionSpheres(Vector3 center1, float radius1, Vector3 center2, float radius2); // Detect collision between two spheres
RLAPI bool CheckCollisionBoxes(BoundingBox box1, BoundingBox box2); // Detect collision between two bounding boxes RLAPI bool CheckCollisionBoxes(BoundingBox box1, BoundingBox box2); // Detect collision between two bounding boxes
RLAPI bool CheckCollisionBoxSphere(BoundingBox box, Vector3 center, float radius); // Detect collision between box and sphere RLAPI bool CheckCollisionBoxSphere(BoundingBox box, Vector3 center, float radius); // Detect collision between box and sphere
RLAPI bool CheckCollisionRaySphere(Ray ray, Vector3 center, float radius); // Detect collision between ray and sphere RLAPI RayCollision GetRayCollisionSphere(Ray ray, Vector3 center, float radius); // Get collision info between ray and sphere
RLAPI bool CheckCollisionRaySphereEx(Ray ray, Vector3 center, float radius, Vector3 *collisionPoint); // Detect collision between ray and sphere, returns collision point RLAPI RayCollision GetRayCollisionBox(Ray ray, BoundingBox box); // Get collision info between ray and box
RLAPI bool CheckCollisionRayBox(Ray ray, BoundingBox box); // Detect collision between ray and box RLAPI RayCollision GetRayCollisionMesh(Ray ray, Mesh mesh, Matrix transform); // Get collision info between ray and mesh
RLAPI RayHitInfo GetCollisionRayMesh(Ray ray, Mesh mesh, Matrix transform); // Get collision info between ray and mesh RLAPI RayCollision GetRayCollisionModel(Ray ray, Model model); // Get collision info between ray and model
RLAPI RayHitInfo GetCollisionRayModel(Ray ray, Model model); // Get collision info between ray and model RLAPI RayCollision GetRayCollisionTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3); // Get collision info between ray and triangle
RLAPI RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3); // Get collision info between ray and triangle RLAPI RayCollision GetRayCollisionGround(Ray ray, float groundHeight); // Get collision info between ray and ground plane (Y-normal plane)
RLAPI RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight); // Get collision info between ray and ground plane (Y-normal plane)
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Audio Loading and Playing Functions (Module: audio) // Audio Loading and Playing Functions (Module: audio)