Bonjour,

J'ai une "grid" infinie (plan d'équation y = 0). Je cherche à obtenir les coordonnées (x, z) d'un point que je clique avec la souris.
J'utilise le code suivant :


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
            if (_OgldevMouse == OGLDEV_MOUSE_BUTTON_LEFT) {
                // Step 1: Convert mouse coordinates to Normalized Device Coordinates (NDC)
                float ndcX = ((2.0f * x) / WINDOW_WIDTH) - 1.0f;
                float ndcY = 1.0f - (2.0f * y) / WINDOW_HEIGHT; // Invert Y-axis
                Vector4f rayClip(ndcX, ndcY, -1.0f, 1.0f); // Ray in clip space
 
                std::cout << "mouse (raw) : " << x << ", " << y << std::endl;
                std::cout << "mouse (NDC) : " << ndcX << ", " << ndcY << std::endl;
 
                // Step 2: Unproject from clip space to view space
                Matrix4f projMatrix;
                projMatrix.InitPersProjTransform(m_persProjInfo);
                Matrix4f invProj = projMatrix.Inverse();
                Vector4f rayEye = invProj * rayClip;
                rayEye.z = -1.0f; // Set forward direction
                rayEye.w = 0.0f;  // This is a direction, not a point
 
                // Step 3: Unproject from view space to world space
                Matrix4f invView = m_pGameCamera->GetViewMatrix().Inverse();
                Vector3f rayWorld = (invView * rayEye).to3f();
 
                // Step 4: Define the grid plane (XZ plane at y = 0)
                Vector3f planeNormal(0.0f, 1.0f, 0.0f); // Normal of the XZ plane
                float planeD = 0.0f; // Plane equation: y = 0 -> 0*x + 1*y + 0*z = 0
 
                // Step 5: Compute ray-plane intersection
                Vector3f rayOrigin = m_pGameCamera->GetPos(); // Camera position
                float t = -(rayOrigin.Dot(planeNormal) + planeD) / (rayWorld.Dot(planeNormal));
 
                if (t > 0.0f) { // Intersection exists
                    Vector3f intersection = rayOrigin + rayWorld * t;
                    std::cout << "origin vector is        : " << rayOrigin.x << ", " << rayOrigin.y << ", " << rayOrigin.z << std::endl;
                    std::cout << "rayWorld vector is      : " << rayWorld.x << ", " << rayWorld.y << ", " << rayWorld.z << std::endl;
                    std::cout << "Mouse intersects grid at: " << intersection.x << ", " << intersection.y << ", " << intersection.z << std::endl;
 
                    // You can now use the intersection point for further logic
                } else {
                    std::cout << "No intersection with the grid." << std::endl;
                }
            }
        }
Malheureusement, ça ne fonctionne pas du tout... Les coordonnées de la souris sont correctes, mais le résultat final est faux (bien que la coordonnée "y" soit toujours nulle, donc correcte).

Voyez vous quelque chose qui cloche de façon immédiate dans ce code ?
A défaut, un conseil pour debugger ce genre de choses ?

Merci !