Merge pull request #521 from TheLumaio/master

Added GetCollisionRayModel
This commit is contained in:
Ray 2018-04-08 22:28:19 +02:00 committed by GitHub
commit 9e7dedf5af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 13 deletions

View File

@ -101,8 +101,8 @@ int main()
{ {
hitMeshBBox = true; hitMeshBBox = true;
// Check ray collision against mesh // Check ray collision against model
meshHitInfo = GetCollisionRayMesh(ray, &tower.mesh); meshHitInfo = GetCollisionRayModel(ray, &tower);
if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance)) if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance))
{ {

View File

@ -1966,28 +1966,28 @@ bool CheckCollisionRayBox(Ray ray, BoundingBox box)
return collision; return collision;
} }
// Get collision info between ray and mesh // Get collision info between ray and model
RayHitInfo GetCollisionRayMesh(Ray ray, Mesh *mesh) RayHitInfo GetCollisionRayModel(Ray ray, Model *model)
{ {
RayHitInfo result = { 0 }; RayHitInfo result = { 0 };
// If mesh doesn't have vertex data on CPU, can't test it. // If mesh doesn't have vertex data on CPU, can't test it.
if (!mesh->vertices) return result; if (!model->mesh.vertices) return result;
// mesh->triangleCount may not be set, vertexCount is more reliable // model->mesh.triangleCount may not be set, vertexCount is more reliable
int triangleCount = mesh->vertexCount/3; int triangleCount = model->mesh.vertexCount/3;
// Test against all triangles in mesh // Test against all triangles in mesh
for (int i = 0; i < triangleCount; i++) for (int i = 0; i < triangleCount; i++)
{ {
Vector3 a, b, c; Vector3 a, b, c;
Vector3 *vertdata = (Vector3 *)mesh->vertices; Vector3 *vertdata = (Vector3 *)model->mesh.vertices;
if (mesh->indices) if (model->mesh.indices)
{ {
a = vertdata[mesh->indices[i*3 + 0]]; a = vertdata[model->mesh.indices[i*3 + 0]];
b = vertdata[mesh->indices[i*3 + 1]]; b = vertdata[model->mesh.indices[i*3 + 1]];
c = vertdata[mesh->indices[i*3 + 2]]; c = vertdata[model->mesh.indices[i*3 + 2]];
} }
else else
{ {
@ -1995,6 +1995,10 @@ RayHitInfo GetCollisionRayMesh(Ray ray, Mesh *mesh)
b = vertdata[i*3 + 1]; b = vertdata[i*3 + 1];
c = vertdata[i*3 + 2]; c = vertdata[i*3 + 2];
} }
a = Vector3Transform(a, model->transform);
b = Vector3Transform(b, model->transform);
c = Vector3Transform(c, model->transform);
RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, a, b, c); RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, a, b, c);

View File

@ -1052,7 +1052,7 @@ RLAPI bool CheckCollisionRaySphere(Ray ray, Vector3 spherePosition, float sphere
RLAPI bool CheckCollisionRaySphereEx(Ray ray, Vector3 spherePosition, float sphereRadius, RLAPI bool CheckCollisionRaySphereEx(Ray ray, Vector3 spherePosition, float sphereRadius,
Vector3 *collisionPoint); // Detect collision between ray and sphere, returns collision point Vector3 *collisionPoint); // Detect collision between ray and sphere, returns collision point
RLAPI bool CheckCollisionRayBox(Ray ray, BoundingBox box); // Detect collision between ray and box RLAPI bool CheckCollisionRayBox(Ray ray, BoundingBox box); // Detect collision between ray and box
RLAPI RayHitInfo GetCollisionRayMesh(Ray ray, Mesh *mesh); // Get collision info between ray and mesh RLAPI RayHitInfo GetCollisionRayModel(Ray ray, Model *model); // Get collision info between ray and model
RLAPI RayHitInfo GetCollisionRayTriangle(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 RayHitInfo GetCollisionRayGround(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)