1 | /******************************************************************************************* |
2 | * |
3 | * raylib [core] example - Picking in 3d mode |
4 | * |
5 | * This example has been created using raylib 1.3 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Copyright (c) 2015 Ramon Santamaria (@raysan5) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | |
14 | int main(void) |
15 | { |
16 | // Initialization |
17 | //-------------------------------------------------------------------------------------- |
18 | const int screenWidth = 800; |
19 | const int screenHeight = 450; |
20 | |
21 | InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d picking" ); |
22 | |
23 | // Define the camera to look into our 3d world |
24 | Camera camera = { 0 }; |
25 | camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position |
26 | camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point |
27 | camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target) |
28 | camera.fovy = 45.0f; // Camera field-of-view Y |
29 | camera.type = CAMERA_PERSPECTIVE; // Camera mode type |
30 | |
31 | Vector3 cubePosition = { 0.0f, 1.0f, 0.0f }; |
32 | Vector3 cubeSize = { 2.0f, 2.0f, 2.0f }; |
33 | |
34 | Ray ray = { 0 }; // Picking line ray |
35 | |
36 | bool collision = false; |
37 | |
38 | SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode |
39 | |
40 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
41 | //-------------------------------------------------------------------------------------- |
42 | |
43 | // Main game loop |
44 | while (!WindowShouldClose()) // Detect window close button or ESC key |
45 | { |
46 | // Update |
47 | //---------------------------------------------------------------------------------- |
48 | UpdateCamera(&camera); // Update camera |
49 | |
50 | if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) |
51 | { |
52 | if (!collision) |
53 | { |
54 | ray = GetMouseRay(GetMousePosition(), camera); |
55 | |
56 | // Check collision between ray and box |
57 | collision = CheckCollisionRayBox(ray, |
58 | (BoundingBox){(Vector3){ cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2 }, |
59 | (Vector3){ cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2 }}); |
60 | } |
61 | else collision = false; |
62 | } |
63 | //---------------------------------------------------------------------------------- |
64 | |
65 | // Draw |
66 | //---------------------------------------------------------------------------------- |
67 | BeginDrawing(); |
68 | |
69 | ClearBackground(RAYWHITE); |
70 | |
71 | BeginMode3D(camera); |
72 | |
73 | if (collision) |
74 | { |
75 | DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, RED); |
76 | DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, MAROON); |
77 | |
78 | DrawCubeWires(cubePosition, cubeSize.x + 0.2f, cubeSize.y + 0.2f, cubeSize.z + 0.2f, GREEN); |
79 | } |
80 | else |
81 | { |
82 | DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY); |
83 | DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY); |
84 | } |
85 | |
86 | DrawRay(ray, MAROON); |
87 | DrawGrid(10, 1.0f); |
88 | |
89 | EndMode3D(); |
90 | |
91 | DrawText("Try selecting the box with mouse!" , 240, 10, 20, DARKGRAY); |
92 | |
93 | if(collision) DrawText("BOX SELECTED" , (screenWidth - MeasureText("BOX SELECTED" , 30)) / 2, screenHeight * 0.1f, 30, GREEN); |
94 | |
95 | DrawFPS(10, 10); |
96 | |
97 | EndDrawing(); |
98 | //---------------------------------------------------------------------------------- |
99 | } |
100 | |
101 | // De-Initialization |
102 | //-------------------------------------------------------------------------------------- |
103 | CloseWindow(); // Close window and OpenGL context |
104 | //-------------------------------------------------------------------------------------- |
105 | |
106 | return 0; |
107 | } |
108 | |