Function_BoxCollision3d.c 4.6 KB

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