how to deduct tuples // checking cuboid intersection

I have a task to do a coarse check if cuboids intersect by assuming they are spheres. So I have cuboid class with three arrays stored as x, y, z coordinates of each of 8 points:

1
2
3
4
5
6
7
  Cuboid::Cuboid()

    : _x_coords(std::array<float, 8>())

    , _y_coords(std::array<float, 8>())

    , _z_coords(std::array<float, 8>())


I wrote two functions to get the geometrical center of a cuboid and to get the sphere radius (minimal possible sphere around the cuboid):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
std::tuple<float, float, float> Cuboid::getGeometricalCenter() const {

    float cx = (_x_coords[1] - _x_coords[0]) / 2 ;

    float cy = (_y_coords[1] - _y_coords[3]) / 2 ;

    float cz = (_z_coords[4] - _z_coords[0]) / 2;

return std::make_tuple(cx, cy, cz);


float Cuboid::getSphereRadius() const {

    float cx = _x_coords[1] - _x_coords[0];

    float cy = _y_coords[1] - _y_coords[3];

    float cz = _z_coords[4] - _z_coords[0];

    float radius = sqrt(cx * cx + cy * cy + cz * cz);

return radius;


Now I am trying to compare if the distance between geometrical centers is larger than sum of sphere radius's but I know that I cannot simply deduce tuple from tuple:

1
2
3
if (getGeometricalCenter() - other.getGeometricalCenter() > (getSphereRadius()+other.getSphereRadius()))

  return false;


I appreciate much if someone could help me with that if statement for tuples. Also if anyone has additional comments if I did smth wrong please correct me , thank you very much
you are making this too hard, maybe?
you have 3 distances. object a to object b centers. object a's radius. object b's radius.
distances are not 'tuples'. distances are a single value. Do you know the 3-d distance formula? Its just like 2-d, just one more value... sqrt (x*x+y*y+z*z) where x is x1-x2 and so on... right?

is the a to b distance less than arad + brad?
if yes, you need to check the precise collisions, if not, you can skip the harder check.

but it gets better, if your data is what I am thinking it is... ?
Ill ask it again. is the radius of a and b in some known range that is relatively small?

If so, read on!
you need to do this problem in 2-d and then upscale to 3-d once you understand it better.
in 2-d, if you have a bunch of circles with a radius between 1 and 3 in a 1000x1000 box, for example, and say you sorted all the circles by their distance from the center of the box, 0,0? Get that image in your head, and now think: can 2 things collide if they are say, 10 units apart? No, they can't. So if you have in hand, a new circle that you want to create, you know where it is. there are 2 rings from the universe's 0,0 coordinate, one at your new guy-3 and one at new guy +3 radius rings. Any collisions MUST be in this area. With your list sorted by radius from the universe center, there are a group of circles that could be collisions in that band and the rest can be ignored. This combines 2 of the techniques we talked about, one is slicing your space up and the other is checking collisions with spheres. With the two, you have a great many you don't check at all, a few that you check with quick sphere checks, and a very few left from that which you check with the full correct algorithm for precise collisions. I think, with you talking about billions, you need to approach it this way, using both techniques.
Last edited on
Topic archived. No new replies allowed.