Results 1 to 18 of 18

Thread: Collision detection.

  1. #1

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720

    Collision detection.

    Alright, I have 2 boxes (C code). I want box A, to check if it collided with box B (the bounding box), and if so, stop it.

    Right now, I have a function that returns 0 or 1, but the way I check for collision is, the function accepts and X, Y, and the 4 points of the bounding box, it will check the bounding box to see if the X, Y is in it, and return 1 or 0 depending on what it is. Is their a better way of doing this?
    asdf

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    if box A inside B then you just need to test the left side, rightside top and bottom side values independently
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    When you say, side, do you mean the whole side of the box, or a point? if you mean the whole side, how would I check that?.. right now I check the 4 points (corners) of box A. But I want to determine which side is touching.
    asdf

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    are the points formed in a rectangle and aligned with the axis?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    Well, to determine which sides are in box B, all you have to do is think about it. (refer to drawing as you read)

    If A.4 is in box B, then sides A.2--A.4 and A.3--A.4 have to be in B.
    If A.3 is in box B, then sides A.1--A.3 and A.4--A.3 have to be in B.
    etc...

    I hope that's what you were asking. If not, I'll try and help out more.
    Attached Images Attached Images  
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843
    1. Check to see if the rightmost line of the leftmost rectabngle is over the leftmost line of the rightmost rectangle.

    ex: in the utterly horrible drawing you would check line A2A4 and line B1B3 if they cross. That is, if A2A4 is more "right" than A1B3

    2. check to see if the topmost line of the bottommost rectangle is over the bottommost line of the topmost rectable

    ex: if B1B2 is over A3A4

    if both conditions are tru, then you have a collision. If one or none conditions are true, then you okay, keep shooting those asteroids.


    if one box is inside another, then... this don't work so well, does it?
    Hell, I don't know if it works any better than what you have anyway.
    Merry Christmas

  7. #7
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    If these boxes could collide at any direction, then you need to check to see if any of the 4 points of your target box is within the bounding box (I am assuming that if the point is within the box, then there is a collision. If it is the opposite, then just NOT may logic.)

    Checking if a single point is within the box 4 times is perfectly valid, but you could cut down on the function calls (unless if you inlined the function) by passing two boxes.

    If you have a code chunk, it could possibly be optimized on the C / C++ thread.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  8. #8
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking hmmm

    remember that you could have one box completely inside the other.

    This means that if you just check the 4 points of one box, if the other is smaller and completely inside it u'll fail to get a collision.

    In short - > Check the 4 points of one box and ONE of the other. This will guarentee that if you have one completely inside the other, it will still register a hit.
    sql_lall

  9. #9
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    remember that you could have one box completely inside the other.

    This means that if you just check the 4 points of one box, if the other is smaller and completely inside it u'll fail to get a collision.

    In short - > Check the 4 points of one box and ONE of the other. This will guarentee that if you have one completely inside the other, it will still register a hit.
    How is that different from what I said?
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  10. #10
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: hmmm

    Originally posted by sql_lall
    remember that you could have one box completely inside the other.

    This means that if you just check the 4 points of one box, if the other is smaller and completely inside it u'll fail to get a collision.

    In short - > Check the 4 points of one box and ONE of the other. This will guarentee that if you have one completely inside the other, it will still register a hit.
    You'd need to check all four corners of both boxes unless i'm mistaken. If you decided to check the bottom left point of a smaller box, and the box was only half overlapping the bigger one from the left then it wouldn't get a hit.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  11. #11
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking hmm...

    Darkwraith:
    It is different because what i interpreted you as saying was that you only had to check the corners of you target box. I am saying you have to do this...AND...one of the other box.

    SLH: I see your point. Probably six checks is enough...4 of one box, then two opposite corners of another...interesting to think about.
    sql_lall

  12. #12
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Are you saying that it is possible for a box to overlap another box without at least one of its corners to be inside the other box? (I am assuming no rotation.)

    If so, I would like to see it.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  13. #13
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Originally posted by Darkwraith
    Are you saying that it is possible for a box to overlap another box without at least one of its corners to be inside the other box? (I am assuming no rotation.)

    If so, I would like to see it.
    Not sure if you're including a case like this but....
    A big box could completly cover a smaller one, and still have no points within the small (other) box.

    That's what sql_lall is saying, and why you have to check more than just 4 corners of one box.

    Thinking about it you should be able to check using only 4 corners, opposite ones for each box. i.e. Top-left/bottom right for one, and Top-Right/Bottom-Left for another. Think that would work.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  14. #14
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Ok, that would work.

    I don't know if you are assuming this or not, but you would not need to check the other box's points if (1) all the boxes are the same size or (2) there is no possible way at each frame for the change in distance = any box size.

    If this is false, then you have to use sql_lall's method for collision.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  15. #15
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    I don't think my method of checking 2 opposite corners of each box (which is different to sql_lall's) is based on either of those assumptions.

    If you could describe an example where my method fails i'd be greatfull.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  16. #16
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking hehe

    SLH - good point...i was thinking it would be 4...

    One way ur method fails (but any method would):
    Using floats, and the points are 'equal' yet the values representing them may not be...

    Other than that, the opposite corner thing is good
    sql_lall

  17. #17
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: hehe

    Originally posted by sql_lall
    SLH - good point...i was thinking it would be 4...

    One way ur method fails (but any method would):
    Using floats, and the points are 'equal' yet the values representing them may not be...

    Other than that, the opposite corner thing is good
    Yeah, i guess you could round the numbers before checking, but whether it'd be worth it remains to be seen....

    Still if the rectangles could be rotated then i think you'd have to check all corners.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  18. #18
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    The two assumptions in my previous post is if you were just to check 4 points of the one box ro detect a collision reliabily. It would be faster just to check 4 points than 5.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width