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?
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.
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.
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.
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.
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.
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.
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
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.
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.
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
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.
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
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
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.