Alright,

I need help for a shooting game I'm working on.

I current have a box that is 3d ( x, y, z)

I'm trying to perform a check to see if my cursor is inside this 3D box.

I understand how to make it 2d by using the max and min values but that creates a 2D box which will say I'm inside the box when in reality I'm not.

Hope someone understand this.

Maybe ill draw a pic and post it.

Dani AI

Generated

Two practical, robust ways to test “is the mouse inside the 3D box (not the red area)” are: (A) cast a world-space pick ray from the mouse and intersect it with the box (transform the ray into the box’s local space — this is exactly the “un-rotate” idea mentioned), or (B) project the box faces to screen space and do a 2D point-in-polygon test for the visible face(s). Method A is generally preferred for accurate picking and depth/occlusion handling; method B is simple and fast when you only care about that object’s visible faces.

Ray -> OBB (recommended)

  1. Convert mouse (screenX, screenY) to normalized device coords and unproject two points (near/far) with inverse(view*proj) to get a world-space ray.
  2. Transform ray origin and direction by inverse(model) so the box is axis-aligned in local space.
  3. Use the slab-method to intersect the ray with the AABB (box min/max). If it hits, t gives the hit distance and you can compute the exact hit point and face.

Example slab test (C++-style pseudocode):

bool RayAABBIntersect(const Vec3& ro, const Vec3& rd, const Vec3& bmin, const Vec3& bmax, float& outT)
{
    const float EPS = 1e-8f;
    float tmin = 0.0f, tmax = 1e30f;
    for (int i=0;i<3;i++) {
        if (fabsf(rd[i]) < EPS) {
            if (ro[i] < bmin[i] || ro[i] > bmax[i]) return false;
            continue;
        }
        float ood = 1.0f/rd[i];
        float t1 = (bmin[i] - ro[i]) * ood;
        float t2 = (bmax[i] - ro[i]) * ood;
        if (t1 > t2) swap(t1,t2);
        tmin = t1 > tmin ? t1 : tmin;
        tmax = t2 < tmax ? t2 : tmax;
        if (tmin > tmax) return false;
    }
    outT = tmin;
    return outT >= 0.0f;
}

Screen-space polygon method

  1. Compute model-view-proj matrix, multiply each of the box face vertices, divide by w to get NDC, then viewport transform to screen coords.
  2. Determine which faces are front-facing (dot(faceNormalInViewSpace, viewDir) < 0).
  3. For each visible face project its quad and test the mouse point against two triangles (barycentric or cross-product sign).

Notes and gotchas

  • Watch API differences: OpenGL NDC z is [-1,1], Direct3D often uses [0,1]; screen Y may be flipped.
  • If other objects can occlude the box, either do full triangle-ray intersection (instead of AABB) or compare hit depth against the GPU depth buffer.
  • For many objects, do a cheap 2D-bounds pretest, then the ray/AABB test only for candidates.

Tying to the thread: use ’s “unrotate” trick by transforming the pick ray into object space, and then apply the slab test above for a concise, accurate solution.

Recommended Answers

All 6 Replies

Its the same as checking for a point in a 2D rectangle. But depending on the view of the person, you need to determine which face of the cube to check on.

see attachment for what i mean

I need to check if mouse is inside the box but not in the red area

- Rotate the world axis such that the cube has no rotation( i.e 'unrotate' the cube relative to the world axis)
- transform the point to be relative to the cube's position
- check if the relative point is in the un-rotated cube by doing simple bounding min-max check

Do you have any examples for this?

check this post

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.