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
// Display information about closest hit
RayHitInfo nearestHit = { 0 };
RayCollision collision = { 0 };
char *hitObjectName = "None";
nearestHit.distance = FLT_MAX;
nearestHit.hit = false;
collision.distance = FLT_MAX;
collision.hit = false;
Color cursorColor = WHITE;
// Get ray and test against ground, triangle, and mesh
ray = GetMouseRay(GetMousePosition(), camera);
// 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;
hitObjectName = "Ground";
}
// 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;
hitObjectName = "Triangle";
bary = Vector3Barycenter(nearestHit.position, ta, tb, tc);
bary = Vector3Barycenter(collision.point, ta, tb, tc);
hitTriangle = true;
}
else hitTriangle = false;
RayHitInfo meshHitInfo = { 0 };
RayCollision meshHitInfo = { 0 };
// 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;
// Check ray collision against model
// 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;
hitObjectName = "Mesh";
}
@ -128,7 +128,7 @@ int main(void)
// Draw the tower
// WARNING: If scale is different than 1.0f,
// not considered by GetCollisionRayModel()
// not considered by GetRayCollisionModel()
DrawModel(tower, towerPos, 1.0f, WHITE);
// Draw the test triangle
@ -140,17 +140,17 @@ int main(void)
if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME);
// 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);
DrawCubeWires(nearestHit.position, 0.3f, 0.3f, 0.3f, RED);
DrawCube(collision.point, 0.3f, 0.3f, 0.3f, cursorColor);
DrawCubeWires(collision.point, 0.3f, 0.3f, 0.3f, RED);
Vector3 normalEnd;
normalEnd.x = nearestHit.position.x + nearestHit.normal.x;
normalEnd.y = nearestHit.position.y + nearestHit.normal.y;
normalEnd.z = nearestHit.position.z + nearestHit.normal.z;
normalEnd.x = collision.point.x + collision.normal.x;
normalEnd.y = collision.point.y + collision.normal.y;
normalEnd.z = collision.point.z + collision.normal.z;
DrawLine3D(nearestHit.position, normalEnd, RED);
DrawLine3D(collision.point, normalEnd, RED);
}
DrawRay(ray, MAROON);
@ -162,21 +162,21 @@ int main(void)
// Draw some debug GUI text
DrawText(TextFormat("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK);
if (nearestHit.hit)
if (collision.hit)
{
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",
nearestHit.position.x,
nearestHit.position.y,
nearestHit.position.z), 10, ypos + 15, 10, BLACK);
collision.point.x,
collision.point.y,
collision.point.z), 10, ypos + 15, 10, BLACK);
DrawText(TextFormat("Hit Norm: %3.2f %3.2f %3.2f",
nearestHit.normal.x,
nearestHit.normal.y,
nearestHit.normal.z), 10, ypos + 30, 10, BLACK);
collision.normal.x,
collision.normal.y,
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);
}