Function_SphereBoxCollision3d.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #define max(x, y) (((x) > (y)) ? (x) : (y))
  2. #define min(x, y) (((x) < (y)) ? (x) : (y))
  3. #include "raylib.h"
  4. #include <stdlib.h>
  5. #include <math.h>
  6. bool sphereboxcollision( float x1, float y1, float z1, float radius,
  7. float x2, float y2, float z2, float w, float h, float d);
  8. int main(void)
  9. {
  10. // Initialization
  11. //--------------------------------------------------------------------------------------
  12. const int screenWidth = 800;
  13. const int screenHeight = 450;
  14. InitWindow(screenWidth, screenHeight, "raylib example.");
  15. // We generate a checked image for texturing
  16. Image checked = GenImageChecked(2, 2, 1, 1, BLACK, DARKGRAY);
  17. Texture2D texture = LoadTextureFromImage(checked);
  18. UnloadImage(checked);
  19. Model model = { 0 };
  20. model = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  21. // Set checked texture as default diffuse component for all models material
  22. model.materials[0].maps[MAP_DIFFUSE].texture = texture;
  23. // Define the camera to look into our 3d world
  24. Camera camera = { { 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  25. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set camera mode
  26. Vector3 bulletpos;
  27. bulletpos = camera.position;
  28. Vector3 bulletinc;
  29. bulletinc.x = 0.05;
  30. bulletinc.y = 0.00;
  31. bulletinc.z = 0.0;
  32. Model bullet = { 0 };
  33. bullet = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  34. // Set checked texture as default diffuse component for all models material
  35. bullet.materials[0].maps[MAP_DIFFUSE].texture = texture;
  36. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  37. //--------------------------------------------------------------------------------------
  38. // Main game loop
  39. while (!WindowShouldClose()) // Detect window close button or ESC key
  40. {
  41. // Update
  42. //----------------------------------------------------------------------------------
  43. UpdateCamera(&camera); // Update internal camera and our camera
  44. // Shoot the bullet.
  45. if(IsKeyDown(KEY_SPACE)){
  46. bulletpos = camera.position;
  47. // Get the direction of where to shoot the bullet at.
  48. float x1 = camera.position.x;
  49. float y1 = camera.position.y;
  50. float z1 = camera.position.z;
  51. float x2 = camera.target.x;
  52. float y2 = camera.target.y;
  53. float z2 = camera.target.z;
  54. float delta_x = x2 - x1;
  55. float delta_y = y2 - y1;
  56. float delta_z = z2 - z1;
  57. float len = sqrt(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z);
  58. delta_x = delta_x / len;
  59. delta_y = delta_y / len;
  60. delta_z = delta_z / len;
  61. bulletinc.x = delta_x;
  62. bulletinc.y = delta_y;
  63. bulletinc.z = delta_z;
  64. }
  65. // update the bullet.
  66. bulletpos.x += bulletinc.x;
  67. bulletpos.y += bulletinc.y;
  68. bulletpos.z += bulletinc.z;
  69. //----------------------------------------------------------------------------------
  70. // Draw
  71. //----------------------------------------------------------------------------------
  72. BeginDrawing();
  73. ClearBackground(RAYWHITE);
  74. BeginMode3D(camera);
  75. // Draw a floor.
  76. for(int x=0;x<50;x++){
  77. for(int z=0;z<50;z++){
  78. Vector3 position;
  79. position.x = x;
  80. position.y = 0;
  81. position.z = z;
  82. DrawModel(model, position, 1.0f, WHITE);
  83. }
  84. }
  85. // Draw our bullet.
  86. DrawModel(bullet, bulletpos, 1.0f, WHITE);
  87. EndMode3D();
  88. DrawText("Press space and aim to shoot bullet.",100,100,30,YELLOW);
  89. DrawFPS(0,0);
  90. EndDrawing();
  91. //----------------------------------------------------------------------------------
  92. }
  93. // De-Initialization
  94. UnloadTexture(texture); // Unload texture
  95. // Unload models data (GPU VRAM)
  96. UnloadModel(model);
  97. UnloadModel(bullet);
  98. //--------------------------------------------------------------------------------------
  99. CloseWindow(); // Close window and OpenGL context
  100. //--------------------------------------------------------------------------------------
  101. return 0;
  102. }
  103. bool sphereboxcollision( float x1, float y1, float z1, float radius,
  104. float x2, float y2, float z2, float w, float h, float d) {
  105. // get box closest point to sphere center by clamping
  106. float x = max(x2, min(x1, x2+w));
  107. float y = max(y2, min(y1, y2+h));
  108. float z = max(z2, min(z1, z2+d));
  109. // this is the same as isPointInsideSphere
  110. float distance = sqrt( (x - x1) * (x - x1) +
  111. (y - y1) * (y - y1) +
  112. (z - z1) * (z - z1));
  113. return distance < radius;
  114. }