Replace GetRayCollisionGround with GetCollisionQuad
This commit is contained in:
parent
2995e20b3c
commit
072d11552c
|
|
@ -42,6 +42,12 @@ int main(void)
|
||||||
Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position
|
Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position
|
||||||
BoundingBox towerBBox = GetMeshBoundingBox(tower.meshes[0]); // Get mesh bounding box
|
BoundingBox towerBBox = GetMeshBoundingBox(tower.meshes[0]); // Get mesh bounding box
|
||||||
|
|
||||||
|
// Ground quad
|
||||||
|
Vector3 g0 = (Vector3){ -50.0f, 0.0f, -50.0f };
|
||||||
|
Vector3 g1 = (Vector3){ -50.0f, 0.0f, 50.0f };
|
||||||
|
Vector3 g2 = (Vector3){ 50.0f, 0.0f, 50.0f };
|
||||||
|
Vector3 g3 = (Vector3){ 50.0f, 0.0f, -50.0f };
|
||||||
|
|
||||||
// Test triangle
|
// Test triangle
|
||||||
Vector3 ta = (Vector3){ -25.0f, 0.5f, 0.0f };
|
Vector3 ta = (Vector3){ -25.0f, 0.5f, 0.0f };
|
||||||
Vector3 tb = (Vector3){ -4.0f, 2.5f, 1.0f };
|
Vector3 tb = (Vector3){ -4.0f, 2.5f, 1.0f };
|
||||||
|
|
@ -75,14 +81,15 @@ int main(void)
|
||||||
ray = GetMouseRay(GetMousePosition(), camera);
|
ray = GetMouseRay(GetMousePosition(), camera);
|
||||||
|
|
||||||
// Check ray collision against ground plane
|
// Check ray collision against ground plane
|
||||||
/*RayCollision groundHitInfo = GetCollisionRayQuad(ray, 0.0f);
|
RayCollision groundHitInfo = GetRayCollisionQuad(ray, g0, g1, g2, g3);
|
||||||
|
DrawPoint3D(g0, RED);
|
||||||
|
|
||||||
if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance))
|
if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance))
|
||||||
{
|
{
|
||||||
nearestHit = groundHitInfo;
|
nearestHit = groundHitInfo;
|
||||||
cursorColor = GREEN;
|
cursorColor = GREEN;
|
||||||
hitObjectName = "Ground";
|
hitObjectName = "Ground";
|
||||||
}*/
|
}
|
||||||
|
|
||||||
// Check ray collision against test triangle
|
// Check ray collision against test triangle
|
||||||
RayCollision triHitInfo = GetRayCollisionTriangle(ray, ta, tb, tc);
|
RayCollision triHitInfo = GetRayCollisionTriangle(ray, ta, tb, tc);
|
||||||
|
|
|
||||||
26
src/models.c
26
src/models.c
|
|
@ -3019,11 +3019,12 @@ RayCollision GetRayCollisionBox(Ray ray, BoundingBox box)
|
||||||
RayCollision result = { 0 };
|
RayCollision result = { 0 };
|
||||||
|
|
||||||
// Note: If ray.position is inside the box, the distance is negative (as if the ray was reversed)
|
// Note: If ray.position is inside the box, the distance is negative (as if the ray was reversed)
|
||||||
// Reversing ray.direction before will give use the correct result.
|
// Reversing ray.direction will give use the correct result.
|
||||||
bool insideBox =
|
bool insideBox =
|
||||||
ray.position.x > box.min.x && ray.position.x < box.max.x &&
|
ray.position.x > box.min.x && ray.position.x < box.max.x &&
|
||||||
ray.position.y > box.min.y && ray.position.y < box.max.y &&
|
ray.position.y > box.min.y && ray.position.y < box.max.y &&
|
||||||
ray.position.z > box.min.z && ray.position.z < box.max.z;
|
ray.position.z > box.min.z && ray.position.z < box.max.z;
|
||||||
|
|
||||||
if (insideBox) {
|
if (insideBox) {
|
||||||
ray.direction = Vector3Negate(ray.direction);
|
ray.direction = Vector3Negate(ray.direction);
|
||||||
}
|
}
|
||||||
|
|
@ -3140,6 +3141,7 @@ RayCollision GetRayCollisionModel(Ray ray, Model model)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get collision info between ray and triangle
|
// Get collision info between ray and triangle
|
||||||
|
// NOTE: The points are expected to be in counter-clockwise winding
|
||||||
// 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
|
||||||
RayCollision GetRayCollisionTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3)
|
RayCollision GetRayCollisionTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3)
|
||||||
{
|
{
|
||||||
|
|
@ -3197,26 +3199,14 @@ RayCollision GetRayCollisionTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get collision info between ray and ground plane (Y-normal plane)
|
// Get collision info between ray and quad
|
||||||
RayCollision GetRayCollisionGround(Ray ray, float groundHeight)
|
// NOTE: The points are expected to be in counter-clockwise winding
|
||||||
{
|
RayCollision GetRayCollisionQuad(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4) {
|
||||||
#define EPSILON 0.000001 // A small number
|
|
||||||
|
|
||||||
RayCollision result = { 0 };
|
RayCollision result = { 0 };
|
||||||
|
|
||||||
if (fabsf(ray.direction.y) > EPSILON)
|
result = GetRayCollisionTriangle(ray, p1, p2, p4);
|
||||||
{
|
|
||||||
float distance = (ray.position.y - groundHeight)/-ray.direction.y;
|
|
||||||
|
|
||||||
if (distance >= 0.0)
|
if (!result.hit) result = GetRayCollisionTriangle(ray, p2, p3, p4);
|
||||||
{
|
|
||||||
result.hit = true;
|
|
||||||
result.distance = distance;
|
|
||||||
result.normal = (Vector3){ 0.0, 1.0, 0.0 };
|
|
||||||
result.point = Vector3Add(ray.position, Vector3Scale(ray.direction, distance));
|
|
||||||
result.point.y = groundHeight;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user