simple collision detection question

This is a discussion on simple collision detection question within the Game Programming forums, part of the General Programming Boards category; Hi, just wondering if I have a cube resting at (20,0,40) and I have an object moving towards it, how ...

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.

firstly in order to do the best physics, you really have to do overlap tests because there's no way to test exactly where two spinning objects intersect without spending many clock cycles. this is how all rigid body physics simulations work under the hood. the problem is that one frame, they are a fine distance away, and then the next frame the objects can actually pass through each other if they are going fast enough or the frame time is too large! they dont overlap, but they *should* have collided.

just something to know because you may notice it even with simple collision detections.