mirror of
				https://github.com/raysan5/raylib.git
				synced 2025-10-26 12:27:01 +00:00 
			
		
		
		
	Added GetCollisionRayModel
This commit is contained in:
		
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										47
									
								
								src/models.c
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								src/models.c
									
									
									
									
									
								
							| @@ -2008,6 +2008,53 @@ RayHitInfo GetCollisionRayMesh(Ray ray, Mesh *mesh) | |||||||
|     return result; |     return result; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // Get collision info between ray and model | ||||||
|  | // NOTE: This is an exact clone of GetCollisionRayMesh but applies transformation matrix from the model to the vertices | ||||||
|  | RayHitInfo GetCollisionRayModel(Ray ray, Model *model) | ||||||
|  | { | ||||||
|  |     RayHitInfo result = { 0 }; | ||||||
|  |  | ||||||
|  |     // If mesh doesn't have vertex data on CPU, can't test it. | ||||||
|  |     if (!model->mesh.vertices) return result; | ||||||
|  |  | ||||||
|  |     // model->mesh.triangleCount may not be set, vertexCount is more reliable | ||||||
|  |     int triangleCount = model->mesh.vertexCount/3; | ||||||
|  |  | ||||||
|  |     // Test against all triangles in mesh | ||||||
|  |     for (int i = 0; i < triangleCount; i++) | ||||||
|  |     { | ||||||
|  |         Vector3 a, b, c; | ||||||
|  |         Vector3 *vertdata = (Vector3 *)model->mesh.vertices; | ||||||
|  |  | ||||||
|  |         if (model->mesh.indices) | ||||||
|  |         { | ||||||
|  |             a = vertdata[model->mesh.indices[i*3 + 0]]; | ||||||
|  |             b = vertdata[model->mesh.indices[i*3 + 1]]; | ||||||
|  |             c = vertdata[model->mesh.indices[i*3 + 2]]; | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  |             a = vertdata[i*3 + 0]; | ||||||
|  |             b = vertdata[i*3 + 1]; | ||||||
|  |             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); | ||||||
|  |  | ||||||
|  |         if (triHitInfo.hit) | ||||||
|  |         { | ||||||
|  |             // Save the closest hit triangle | ||||||
|  |             if ((!result.hit) || (result.distance > triHitInfo.distance)) result = triHitInfo; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return result; | ||||||
|  | } | ||||||
|  |  | ||||||
| // 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) | RayHitInfo GetCollisionRayTriangle(Ray ray, Vector3 p1, Vector3 p2, Vector3 p3) | ||||||
|   | |||||||
| @@ -1053,6 +1053,7 @@ RLAPI bool CheckCollisionRaySphereEx(Ray ray, Vector3 spherePosition, float sphe | |||||||
|                                      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 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) | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 lumaio teon
					lumaio teon