IN general....
Code:
bool collide(ObjectX, ObjectY, boxcenter, width) {
if((objectX >= boxcenter-(width/2) && (objectX <= boxcenter+(width/2)) {
// so far, you are within the x boundry
if((objectY >= boxcenter-(height/2) && (objectY <= boxcenter+(height/2)) {
// now you are also in the y boundry
if((objectZ >= boxcenter-(depth/2) && (objectZ <= boxcenter+(depth/2)) {
// COLLISION!
}
}
}
}
Now that will tell you if you actually collide with the box.
If you are only checking one box, that code is fine enough itself, how ever if you are checking a huge virtual enviornment full of things you need to do some extra work.
A: Partition the world.
That is to say, you need to divide your world up into pieces, each piece has a list of objects that are in it and then you compare where the Object is and what piece it is in. When you know what piece it is in, you only check collision with all the things located in that piece.
B: Distance check
That is to say, if you have an array of world objects and you want to check any one object colliding with another, you take your first object and choose a minimal distance. Then checkk collisions for objects that are relatively close.
Code:
// psuedo
Distance = sqrt((x2-x1)^2 + (y2-y1)^2)
// sqrt's are costly function to perform so instead you can cheat the system
distanceSquared = (x2-x1)^2 + (y2-y1)^2
// this way you avoid the sqrt but you ahve same functionality, just workin with a bigger number (better)
for(int i=0; i < worldObjectCount; i++) {
if distance between checking object <= distanceSquared (arbitrary picked value)
collide(objectX, objectY, worldObjectCenter, width);
}
The last code segment is very psuedo, you need to develop it yourself, its pretty easy though.