Thread: simple collision detection question

  1. #1
    Registered User
    Join Date
    Jun 2003

    simple collision detection question

    Hi, just wondering if I have a cube resting at (20,0,40) and I have an object moving towards it, how do I know that the object is actually side by side(close) the cube or directly over it? thanks!
    'The bigger they are, the harder they fall' ~Yang

  2. #2
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    IN general....
    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.
    // 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.
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  3. #3
    Join Date
    May 2004
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collision detection woes
    By joeprogrammer in forum Game Programming
    Replies: 11
    Last Post: 06-10-2006, 11:18 PM
  2. Some per poly collision detection code
    By BobMcGee123 in forum Game Programming
    Replies: 0
    Last Post: 01-31-2006, 06:37 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Replies: 4
    Last Post: 05-03-2002, 09:40 PM