|
@ -21,7 +21,7 @@ int main(void) |
|
|
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d picking"); |
|
|
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d picking"); |
|
|
|
|
|
|
|
|
// Define the camera to look into our 3d world |
|
|
// Define the camera to look into our 3d world |
|
|
Camera camera; |
|
|
|
|
|
|
|
|
Camera camera = { 0 }; |
|
|
camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position |
|
|
camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position |
|
|
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point |
|
|
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point |
|
|
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target) |
|
|
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target) |
|
@ -31,7 +31,7 @@ int main(void) |
|
|
Vector3 cubePosition = { 0.0f, 1.0f, 0.0f }; |
|
|
Vector3 cubePosition = { 0.0f, 1.0f, 0.0f }; |
|
|
Vector3 cubeSize = { 2.0f, 2.0f, 2.0f }; |
|
|
Vector3 cubeSize = { 2.0f, 2.0f, 2.0f }; |
|
|
|
|
|
|
|
|
Ray ray = {0.0f, 0.0f, 0.0f}; // Picking line ray |
|
|
|
|
|
|
|
|
Ray ray = { 0 }; // Picking line ray |
|
|
|
|
|
|
|
|
bool collision = false; |
|
|
bool collision = false; |
|
|
|
|
|
|
|
@ -49,12 +49,16 @@ int main(void) |
|
|
|
|
|
|
|
|
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) |
|
|
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) |
|
|
{ |
|
|
{ |
|
|
ray = GetMouseRay(GetMousePosition(), camera); |
|
|
|
|
|
|
|
|
|
|
|
// Check collision between ray and box |
|
|
|
|
|
collision = CheckCollisionRayBox(ray, |
|
|
|
|
|
(BoundingBox){(Vector3){ cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2 }, |
|
|
|
|
|
(Vector3){ cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2 }}); |
|
|
|
|
|
|
|
|
if (!collision) |
|
|
|
|
|
{ |
|
|
|
|
|
ray = GetMouseRay(GetMousePosition(), camera); |
|
|
|
|
|
|
|
|
|
|
|
// Check collision between ray and box |
|
|
|
|
|
collision = CheckCollisionRayBox(ray, |
|
|
|
|
|
(BoundingBox){(Vector3){ cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2 }, |
|
|
|
|
|
(Vector3){ cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2 }}); |
|
|
|
|
|
} |
|
|
|
|
|
else collision = false; |
|
|
} |
|
|
} |
|
|
//---------------------------------------------------------------------------------- |
|
|
//---------------------------------------------------------------------------------- |
|
|
|
|
|
|
|
|