Collision detection is very simple with things like cubes or spheres.
you can use either sphere to sphere detection which is basically drawing an imaginary sphere around the cubes and then checking if distance between the two centre points of the spheres is less than the sphere radius. like so:-
float x, y, z;
x = SmallModel->GetX() - LargeModel->GetX();
y = SmallModel->GetY() - LargeModel->GetY();
z = SmallModel->GetZ() - LargeModel->GetZ();
float collisionDist = sqrt( x*x + y*y + z*z );
if (collisionDist < sphereRadius)
{
// Collision occurred…
}
or you can use bounding boxes which is more suited here as the above is not as accurate where as bounding boxes will be exact since you are using cubes and they are axis aligned.
The process here is also fairly simple: there is a collision if the min and max of each cube lies within each other. I.e. collision if:
Cube1XMin > Cube2XMin && Cube1XMax < Cube2XMax &&
Cube1YMin > Cube2YMin && Cube1YMax < Cube2YMax &&
Cube1ZMin > Cube2ZMin && Cube1ZMax < Cube2ZMax
so you will need to declare 6 variables for each cube as above and these min and max values are found by first knowing the size of the cube and secondly knowing the position of the cubes.
So if a cube is 10 * 10 * 10 and positioned at 100,0,100
then
xmin = 95
ymin = -5
zmin = 95
xmax = 105
ymax = 5
zmax = 105
find the values for the other cubes and run the above equation and you should find out if you have a collision.
For handling a collision you need to move the cubes back to the frame before and then maybe have them bounce back, i think it is possible to get stuck inside each other if you do not move them back by a frame first.
To save on CPU usage you can probably send the calculations to the GPU using shaders but thats a different topic