Working with cubes

I have what I’m told is a really simple problem, unfortunately I’ve never coded in openGL before so it’s a lot more difficult to me!

I need to have a few cubes in a window, which are able to be placed and removed. However, they cannot intersect (do I use collision detection for this) and if they are placed on top of one another, they must be placed in specific ways… ie a block cannot be placed on top when it “looks” like it would fall off. Any ideas on how to implement this? Examples, codes, tutorials… any would be appreciated. Thanks so much!

Try NeHe OpenGL turorials at

I tried that site a few times, and I didn’t find anything throughout the tutorials that really had anything to do with what I need. Maybe I’m not looking for the right thing but I don’t think there’s anything there for me.

If you’ve never coded in OpenGL before, then I would think the OpenGL tutorials there might be of some use.

As far as determining if a cube can be placed on top of another, thats easy. Exploit the symmetry of the cube. Simply check to see if the center of the higher cube projects downward onto the lower cube. This is assuming the “mass” of the cubes is uniformly distributed as well. It also assumes the cube is not placed in an unstable configuration, e.g. standing on one corner.

[This message has been edited by DFrey (edited 11-07-2001).]

First - this isn’t an OpenGL question. OpenGL is a graphics library, not a physics library. Not a criticism, just that you might get more useful feedback in a more relevant forum.

Second - unless there are assumptions or problem constraints you aren’t telling us, whoever told you this was a simple problem was lying. DFrey’s approach is fine as far as it goes, but there are any number of awkward cases it doesn’t cover. Think of tall stacks of blocks with a slight offset between each one, so that any two would form a stable configuration by themselves but the whole stack doesn’t. Or think of a block balanced on two or more blocks, none of which support it alone. It isn’t trivial.

True, in reality statics is an extremely complex problem to solve in all but the most trivial cases. I felt however, that for a beginner, concentrating on just two blocks in stable configurations would be a good starting point. Otherwise, you need to dive into Hamiltonians (or Lagrangians) and tensor analysis. Or fake it with springs.

[This message has been edited by DFrey (edited 11-07-2001).]

Agreed, it’s just that this sounded like a homework question and I’m sure there must have been other constraints in the problem statement. At a minimum, I suspect that all cubes are assumed to be the same size and to be axis-aligned, that the “floor” is a flat plane. Given those it’s probably manageable, though still distinctly nontrivial.