If a 3D box(a bounding box) knows it's position, it's width, it's height and it's depth, what would be the most efficient(can run in real time) way of detecting if a 3D co-ordinate is inside that box?
I already have something in place to check for it but I am wondering if there is anything more efficient,

vPosition is the position of the 3D Coordinate
GetPosition( ) returns the position of the 3D box

bool CBoundingBox::IsInside( VEC vPosition )
{
	return ( ( vPosition.x > GetPosition( ).x && vPosition.x < GetPosition( ).x + m_fWidth ) ||
	( vPosition.y < GetPosition( ).y && vPosition.y > GetPosition( ).y - m_fHeight ) ||
	( vPosition.z > GetPosition( ).z && vPosition.z < GetPosition( ).z + m_fWidth ) );
}

Dani AI

Generated

The cheapest, simplest correct test for a non-rotated box is a three-component interval check: the point must lie between the box minima and maxima on X, Y and Z. Precompute and store either min/max or center+half-extents once (not inside the tight loop) so each test is just a few numeric ops and three comparisons.

A compact, branch-friendly variant uses center and half-extents. Example (conceptual):

bool PointInAABB(const Vec3& p, const Vec3& center, const Vec3& half)
{
    Vec3 d = { fabsf(p.x - center.x), fabsf(p.y - center.y), fabsf(p.z - center.z) };
    return d.x <= half.x && d.y <= half.y && d.z <= half.z;
}

Notes and gotchas relevant to and :

  • Make sure you require all three axis checks (logical AND), and that you compare against the correct size for each axis (width vs depth). Decide whether boundaries count as inside (<=) or not (<).
  • Avoid repeated calls like GetPosition() inside the test; read the box transform once and reuse it.
  • If boxes can rotate, keep the same fast test by transforming the point into box-local space (local = R^T * (point - center)) and then apply the same half-extent check. For interacting boxes or rotated-box overlap tests use the Separating Axis Theorem rather than a distance-based (circle/sphere) shortcut.

If many boxes/points are tested per frame, add a broad phase: uniform grid, sweep-and-prune, octree/BVH or cheap bounding-sphere pretests to cut the number of AABB checks. For extreme throughput consider SIMD/vectorized math or multithreading on batches of points.

For reference on common approaches: Axis-aligned bounding box and Separating axis theorem.

Recommended Answers

All 3 Replies

Are the boxes rotated, will they ever rotate?

at the moment they aren't, and i don't think i will, i might just rotate the model inside the box and always keep the box the same rotation.

If the bounding box does not rotate, then I'm thinking you can
compare the distance between the box. Just like you would in a circle to
circle collision test. What do you think. I can't really test it out, but I am guessing you can.

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.