# sphere and cube intersection

This topic is 4984 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello I have designed one sphere and a cube with the functions GlutwriteCube and to glutWriteShpere or to glutSolidSphere and glutSolidCube. Now I would have to calculate and to visualize the result of their intersection. who can help to make this me? thanks and sorry for my English

##### Share on other sites
In the case of an axis aligned box, you can use the following function:
bool BoundsDescriptor::sphereOverlapsAABB(const Tuple3f &sphrCen, float radius){  float s, d = 0;   for(int i=0 ; i<3 ; i++ )  {    if(sphrCen < minEndAABB )    {      s = sphrCen - minEndAABB;      d += s*s;     }    else       if(sphrCen > maxEndAABB)      {        s = sphrCen - maxEndAABB;        d += s*s;       }  }  return d <= radius*radius;}

where minEndAABB is located at cubeCenter - (halfWidth, halfHeight, halfDepth) and maxEndAABB is at cubeCenter + (halfWidth, halfHeight, halfDepth).

PS: Besides the fact that this method would only tell you whether your geometry overlaps, I predict this thread will be moved to the Math forums in 1, 2, 3...
[pig]

##### Share on other sites
..4 [grin]
moved as predicted [smile]

##### Share on other sites
I have found: Construtive solid geometry (CSG) but is not successful to find the code. someone can help me

##### Share on other sites
I have found: Construtive solid geometry (CSG) but is not successful to find the code. someone can help me

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 12
• 15
• 11
• 12
• ### Forum Statistics

• Total Topics
634153
• Total Posts
3015846
×