Applied raysan's refactor
Improved GetRayCollisionBox
This commit is contained in:
parent
e243f7ceba
commit
2995e20b3c
|
|
@ -65,7 +65,7 @@ int main(void)
|
|||
UpdateCamera(&camera); // Update camera
|
||||
|
||||
// Display information about closest hit
|
||||
RayCollisionInfo nearestHit = { 0 };
|
||||
RayCollision nearestHit = { 0 };
|
||||
char *hitObjectName = "None";
|
||||
nearestHit.distance = FLT_MAX;
|
||||
nearestHit.hit = false;
|
||||
|
|
@ -75,7 +75,7 @@ int main(void)
|
|||
ray = GetMouseRay(GetMousePosition(), camera);
|
||||
|
||||
// Check ray collision against ground plane
|
||||
/*RayCollisionInfo groundHitInfo = GetCollisionRayQuad(ray, 0.0f);
|
||||
/*RayCollision groundHitInfo = GetCollisionRayQuad(ray, 0.0f);
|
||||
|
||||
if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance))
|
||||
{
|
||||
|
|
@ -85,7 +85,7 @@ int main(void)
|
|||
}*/
|
||||
|
||||
// Check ray collision against test triangle
|
||||
RayCollisionInfo triHitInfo = GetCollisionRayTriangle(ray, ta, tb, tc);
|
||||
RayCollision triHitInfo = GetRayCollisionTriangle(ray, ta, tb, tc);
|
||||
|
||||
if ((triHitInfo.hit) && (triHitInfo.distance < nearestHit.distance))
|
||||
{
|
||||
|
|
@ -93,11 +93,11 @@ int main(void)
|
|||
cursorColor = ORANGE;
|
||||
hitObjectName = "Triangle";
|
||||
|
||||
bary = Vector3Barycenter(nearestHit.position, ta, tb, tc);
|
||||
bary = Vector3Barycenter(nearestHit.point, ta, tb, tc);
|
||||
}
|
||||
|
||||
// Check ray collision against test sphere
|
||||
RayCollisionInfo sphHitInfo = GetCollisionRaySphere(ray, sp, sr);
|
||||
RayCollision sphHitInfo = GetRayCollisionSphere(ray, sp, sr);
|
||||
|
||||
if ((sphHitInfo.hit) && (sphHitInfo.distance < nearestHit.distance)) {
|
||||
nearestHit = sphHitInfo;
|
||||
|
|
@ -106,18 +106,17 @@ int main(void)
|
|||
}
|
||||
|
||||
// Check ray collision against bounding box first, before trying the full ray-mesh test
|
||||
// Note: distance becomes negative if ray.position is inside the box!
|
||||
RayCollisionInfo boxHitInfo = GetCollisionRayBox(ray, towerBBox);
|
||||
RayCollision boxHitInfo = GetRayCollisionBox(ray, towerBBox);
|
||||
|
||||
if ((boxHitInfo.hit) && (boxHitInfo.distance < nearestHit.distance))
|
||||
{
|
||||
nearestHit = boxHitInfo;
|
||||
cursorColor = PURPLE;
|
||||
cursorColor = ORANGE;
|
||||
hitObjectName = "Box";
|
||||
|
||||
// Check ray collision against model
|
||||
// NOTE: It considers model.transform matrix!
|
||||
RayCollisionInfo meshHitInfo = GetCollisionRayModel(ray, tower);
|
||||
RayCollision meshHitInfo = GetRayCollisionModel(ray, tower);
|
||||
|
||||
if (meshHitInfo.hit)
|
||||
{
|
||||
|
|
@ -155,22 +154,18 @@ int main(void)
|
|||
// If we hit something, draw the cursor at the hit point
|
||||
if (nearestHit.hit)
|
||||
{
|
||||
DrawCube(nearestHit.position, 0.3f, 0.3f, 0.3f, cursorColor);
|
||||
DrawCubeWires(nearestHit.position, 0.3f, 0.3f, 0.3f, RED);
|
||||
DrawCube(nearestHit.point, 0.3f, 0.3f, 0.3f, cursorColor);
|
||||
DrawCubeWires(nearestHit.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 = nearestHit.point.x + nearestHit.normal.x;
|
||||
normalEnd.y = nearestHit.point.y + nearestHit.normal.y;
|
||||
normalEnd.z = nearestHit.point.z + nearestHit.normal.z;
|
||||
|
||||
DrawLine3D(nearestHit.position, normalEnd, RED);
|
||||
DrawLine3D(nearestHit.point, normalEnd, RED);
|
||||
}
|
||||
float scale = 10000;
|
||||
DrawLine3D(Vector3Add(ray.position, Vector3Scale(ray.direction, 10)), Vector3Add(ray.position, Vector3Scale(ray.direction, scale)), RED);
|
||||
Vector3 p1 = { 0, 0, 0 };
|
||||
Vector3 p2 = { 10, 10, 10 };
|
||||
DrawLine3D(ray.position, p1, RED);
|
||||
DrawRay(ray, MAROON); // TODO
|
||||
|
||||
DrawRay(ray, MAROON);
|
||||
|
||||
DrawGrid(10, 10.0f);
|
||||
|
||||
|
|
@ -184,19 +179,19 @@ int main(void)
|
|||
int ypos = 70;
|
||||
|
||||
DrawText(TextFormat("Distance: %3.2f", nearestHit.distance), 10, ypos, 10, BLACK);
|
||||
if (nearestHit.distance < 0) DrawText("(Inside Box)", 100, 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);
|
||||
nearestHit.point.x,
|
||||
nearestHit.point.y,
|
||||
nearestHit.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);
|
||||
|
||||
if (triHitInfo.hit) DrawText(TextFormat("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
|
||||
if (triHitInfo.hit && strcmp(hitObjectName, "Triangle") == 0)
|
||||
DrawText(TextFormat("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
|
||||
}
|
||||
|
||||
DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY);
|
||||
|
|
|
|||
81
src/models.c
81
src/models.c
|
|
@ -2979,10 +2979,10 @@ bool CheckCollisionBoxSphere(BoundingBox box, Vector3 center, float radius)
|
|||
return collision;
|
||||
}
|
||||
|
||||
// Detect collision between ray and sphere
|
||||
RayCollisionInfo GetCollisionRaySphere(Ray ray, Vector3 center, float radius)
|
||||
// Get collision info between ray and sphere
|
||||
RayCollision GetRayCollisionSphere(Ray ray, Vector3 center, float radius)
|
||||
{
|
||||
RayCollisionInfo result = { 0 };
|
||||
RayCollision result = { 0 };
|
||||
|
||||
Vector3 raySpherePos = Vector3Subtract(center, ray.position);
|
||||
float vector = Vector3DotProduct(raySpherePos, ray.direction);
|
||||
|
|
@ -2996,28 +2996,37 @@ RayCollisionInfo GetCollisionRaySphere(Ray ray, Vector3 center, float radius)
|
|||
result.distance = vector + sqrtf(d);
|
||||
|
||||
// Calculate collision point
|
||||
result.position = Vector3Add(ray.position, Vector3Scale(ray.direction, result.distance));
|
||||
result.point = Vector3Add(ray.position, Vector3Scale(ray.direction, result.distance));
|
||||
|
||||
// Calculate collision normal (pointing outwards)
|
||||
result.normal = Vector3Negate(Vector3Normalize(Vector3Subtract(result.position, center)));
|
||||
result.normal = Vector3Negate(Vector3Normalize(Vector3Subtract(result.point, center)));
|
||||
} else { // outside
|
||||
result.distance = vector - sqrtf(d);
|
||||
|
||||
// Calculate collision point
|
||||
result.position = Vector3Add(ray.position, Vector3Scale(ray.direction, result.distance));
|
||||
result.point = Vector3Add(ray.position, Vector3Scale(ray.direction, result.distance));
|
||||
|
||||
// Calculate collision normal (pointing inwards)
|
||||
result.normal = Vector3Normalize(Vector3Subtract(result.position, center));
|
||||
result.normal = Vector3Normalize(Vector3Subtract(result.point, center));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Detect collision between ray and bounding box
|
||||
// Note: Returns a negative distance if ray.position is inside the box!
|
||||
RayCollisionInfo GetCollisionRayBox(Ray ray, BoundingBox box)
|
||||
// Get collision info between ray and bounding box
|
||||
RayCollision GetRayCollisionBox(Ray ray, BoundingBox box)
|
||||
{
|
||||
RayCollisionInfo result = { 0 };
|
||||
RayCollision result = { 0 };
|
||||
|
||||
// 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.
|
||||
bool insideBox =
|
||||
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.z > box.min.z && ray.position.z < box.max.z;
|
||||
if (insideBox) {
|
||||
ray.direction = Vector3Negate(ray.direction);
|
||||
}
|
||||
|
||||
float t[11] = { 0 };
|
||||
|
||||
|
|
@ -3034,20 +3043,20 @@ RayCollisionInfo GetCollisionRayBox(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[7] = (float)fmin(fmin(fmax(t[0], t[1]), fmax(t[2], t[3])), fmax(t[4], t[5]));
|
||||
|
||||
result.distance = t[6];
|
||||
result.hit = !(t[7] < 0 || t[6] > t[7]);
|
||||
result.position = Vector3Add(ray.position, Vector3Scale(ray.direction, result.distance));
|
||||
result.distance = t[6];
|
||||
result.point = Vector3Add(ray.position, Vector3Scale(ray.direction, result.distance));
|
||||
|
||||
// get box center point
|
||||
// Get box center point
|
||||
result.normal = Vector3Lerp(box.min, box.max, 0.5f);
|
||||
// get vector center point->hit point
|
||||
result.normal = Vector3Subtract(result.position, result.normal);
|
||||
// scale vector to unit cube
|
||||
// Get vector center point->hit point
|
||||
result.normal = Vector3Subtract(result.point, result.normal);
|
||||
// Scale vector to unit cube
|
||||
// we use an additional .01 to fix numerical errors
|
||||
result.normal = Vector3Scale(result.normal, 2.01f);
|
||||
result.normal = Vector3Divide(result.normal, Vector3Subtract(box.max, box.min));
|
||||
// the relevant elemets of the vector are now slightly larger than 1f (or smaller than -1f)
|
||||
// and the others are somewhere between -1 and 1.
|
||||
// the relevant elemets of the vector are now slightly larger than 1.0f (or smaller than -1.0f)
|
||||
// and the others are somewhere between -1.0 and 1.0
|
||||
// casting to int is exactly our wanted normal!
|
||||
result.normal.x = (int)result.normal.x;
|
||||
result.normal.y = (int)result.normal.y;
|
||||
|
|
@ -3055,13 +3064,21 @@ RayCollisionInfo GetCollisionRayBox(Ray ray, BoundingBox box)
|
|||
|
||||
result.normal = Vector3Normalize(result.normal);
|
||||
|
||||
if (insideBox) {
|
||||
// Reset ray.direction
|
||||
ray.direction = Vector3Negate(ray.direction);
|
||||
// Fix result
|
||||
result.distance *= -1.0f;
|
||||
result.normal = Vector3Negate(result.normal);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Get collision info between ray and mesh
|
||||
RayCollisionInfo GetCollisionRayMesh(Ray ray, Mesh mesh, Matrix transform)
|
||||
RayCollision GetRayCollisionMesh(Ray ray, Mesh mesh, Matrix transform)
|
||||
{
|
||||
RayCollisionInfo result = { 0 };
|
||||
RayCollision result = { 0 };
|
||||
|
||||
// Check if mesh vertex data on CPU for testing
|
||||
if (mesh.vertices != NULL)
|
||||
|
|
@ -3091,7 +3108,7 @@ RayCollisionInfo GetCollisionRayMesh(Ray ray, Mesh mesh, Matrix transform)
|
|||
b = Vector3Transform(b, transform);
|
||||
c = Vector3Transform(c, transform);
|
||||
|
||||
RayCollisionInfo triHitInfo = GetCollisionRayTriangle(ray, a, b, c);
|
||||
RayCollision triHitInfo = GetRayCollisionTriangle(ray, a, b, c);
|
||||
|
||||
if (triHitInfo.hit)
|
||||
{
|
||||
|
|
@ -3104,13 +3121,13 @@ RayCollisionInfo GetCollisionRayMesh(Ray ray, Mesh mesh, Matrix transform)
|
|||
}
|
||||
|
||||
// Get collision info between ray and model
|
||||
RayCollisionInfo GetCollisionRayModel(Ray ray, Model model)
|
||||
RayCollision GetRayCollisionModel(Ray ray, Model model)
|
||||
{
|
||||
RayCollisionInfo result = { 0 };
|
||||
RayCollision result = { 0 };
|
||||
|
||||
for (int m = 0; m < model.meshCount; m++)
|
||||
{
|
||||
RayCollisionInfo meshHitInfo = GetCollisionRayMesh(ray, model.meshes[m], model.transform);
|
||||
RayCollision meshHitInfo = GetRayCollisionMesh(ray, model.meshes[m], model.transform);
|
||||
|
||||
if (meshHitInfo.hit)
|
||||
{
|
||||
|
|
@ -3124,14 +3141,14 @@ RayCollisionInfo GetCollisionRayModel(Ray ray, Model model)
|
|||
|
||||
// Get collision info between ray and triangle
|
||||
// NOTE: Based on https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
|
||||
RayCollisionInfo 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
|
||||
|
||||
Vector3 edge1, edge2;
|
||||
Vector3 p, q, tv;
|
||||
float det, invDet, u, v, t;
|
||||
RayCollisionInfo result = {0};
|
||||
RayCollision result = {0};
|
||||
|
||||
// Find vectors for two edges sharing V1
|
||||
edge1 = Vector3Subtract(p2, p1);
|
||||
|
|
@ -3174,18 +3191,18 @@ RayCollisionInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector
|
|||
result.hit = true;
|
||||
result.distance = t;
|
||||
result.normal = Vector3Normalize(Vector3CrossProduct(edge1, edge2));
|
||||
result.position = Vector3Add(ray.position, Vector3Scale(ray.direction, t));
|
||||
result.point = Vector3Add(ray.position, Vector3Scale(ray.direction, t));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Get collision info between ray and ground plane (Y-normal plane)
|
||||
RayCollisionInfo GetCollisionRayGround(Ray ray, float groundHeight)
|
||||
RayCollision GetRayCollisionGround(Ray ray, float groundHeight)
|
||||
{
|
||||
#define EPSILON 0.000001 // A small number
|
||||
|
||||
RayCollisionInfo result = { 0 };
|
||||
RayCollision result = { 0 };
|
||||
|
||||
if (fabsf(ray.direction.y) > EPSILON)
|
||||
{
|
||||
|
|
@ -3196,8 +3213,8 @@ RayCollisionInfo GetCollisionRayGround(Ray ray, float groundHeight)
|
|||
result.hit = true;
|
||||
result.distance = distance;
|
||||
result.normal = (Vector3){ 0.0, 1.0, 0.0 };
|
||||
result.position = Vector3Add(ray.position, Vector3Scale(ray.direction, distance));
|
||||
result.position.y = groundHeight;
|
||||
result.point = Vector3Add(ray.position, Vector3Scale(ray.direction, distance));
|
||||
result.point.y = groundHeight;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
18
src/raylib.h
18
src/raylib.h
|
|
@ -403,12 +403,12 @@ typedef struct Ray {
|
|||
} Ray;
|
||||
|
||||
// Raycast hit information
|
||||
typedef struct RayCollisionInfo {
|
||||
typedef struct RayCollision {
|
||||
bool hit; // Did the ray hit something?
|
||||
float distance; // Distance to nearest hit
|
||||
Vector3 position; // Position of nearest hit
|
||||
Vector3 point; // Point of nearest hit
|
||||
Vector3 normal; // Surface normal of hit
|
||||
} RayCollisionInfo;
|
||||
} RayCollision;
|
||||
|
||||
// Bounding box type
|
||||
typedef struct BoundingBox {
|
||||
|
|
@ -1451,12 +1451,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 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 RayCollisionInfo GetCollisionRaySphere(Ray ray, Vector3 center, float radius); // Get collision info between ray and sphere
|
||||
RLAPI RayCollisionInfo GetCollisionRayBox(Ray ray, BoundingBox box); // Get collision info between ray and box
|
||||
RLAPI RayCollisionInfo GetCollisionRayModel(Ray ray, Model model); // Get collision info between ray and model
|
||||
RLAPI RayCollisionInfo GetCollisionRayMesh(Ray ray, Mesh mesh, Matrix transform); // Get collision info between ray and mesh
|
||||
RLAPI RayCollisionInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3); // Get collision info between ray and triangle
|
||||
RLAPI RayCollisionInfo GetCollisionRayQuad(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4); // Get collision info between ray and quad
|
||||
RLAPI RayCollision GetRayCollisionSphere(Ray ray, Vector3 center, float radius); // Get collision info between ray and sphere
|
||||
RLAPI RayCollision GetRayCollisionBox(Ray ray, BoundingBox box); // Get collision info between ray and box
|
||||
RLAPI RayCollision GetRayCollisionModel(Ray ray, Model model); // Get collision info between ray and model
|
||||
RLAPI RayCollision GetRayCollisionMesh(Ray ray, Mesh mesh, Matrix transform); // Get collision info between ray and mesh
|
||||
RLAPI RayCollision GetRayCollisionTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3); // Get collision info between ray and triangle
|
||||
RLAPI RayCollision GetRayCollisionQuad(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4); // Get collision info between ray and quad
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Audio Loading and Playing Functions (Module: audio)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user