Like if I have to rectangles on the screen and I want to know when they touch each other, how would I do that?
Like if I have to rectangles on the screen and I want to know when they touch each other, how would I do that?
Why drink and drive when you can smoke and fly?
check to see if any of one rectangles points are within the bounds of another rectangles points.
hehe, yeah well lets say I have two cartesian points, each of those points is the top left corner of a square, one is 5x5 and the other 10x10, how would I check if one is within the bounds of the other?
Why drink and drive when you can smoke and fly?
check to see if any of one rectangles points are within the bounds of another rectangles points.
Alright... here's an easy way to do that - you know the x-min, x-max, y-min, y-max, etc... Just check if there's anything suspicious like the y-max of one being greater than the y-min of the other (depending on how you're looking at this, but hopefully you get the idea). If that returns true, check out the same way if based on the x-min and x-max values, the intersection of the y values occurs within the bounds of the x-coordinates. Takes a lot of checking but a couple of if statements and some not-too-complex arrays ought to do the trick quite well.
ok thanks Ill try go do that
Why drink and drive when you can smoke and fly?
This should cover all possible hits.Code:if (object_x>=check_x && object_x<=object_x2) { if (object_y>=check_y && object_y<=check_y2) { //hit } } if (object_x2>=check_x && object_x2<=check_x2) { if (object_y>=check_y && object_y<=check_y2) { //hit } } if (object_x>=check_x && object_x<=check_x2) { if (object_y2>=check_y && object_y2<=check_y2) { //hit } } if (object_x2>=check_x && object_x2<=check_x2) { if (object_y2>=check_y && object_y2<=check_y2) { //hit } }
There are better ways to do this.
One method I can think of off the top of my head involves using a black and white image for each collision spot.
Basically you draw in white the area where collision should occur and then write your code to detect a collision between a white pixel of one image and another white pixel of another image colliding with each other.
Not sure as to the speed though as I have never actually implemented it.
Last edited by Frobozz; 03-09-2004 at 01:34 AM.
a duck hunt reference! that's how they did shot detection w/ the lightwave (is that what they were called?) guns for the regular nintendo. if you've ever played, you'll notice when you fire the screen flashes. this is because everything is turned white except the ducks (or maybe it's reversed, doesn't really matter tho) and the gun detects dark for a hit or white for a miss. it's an ingenious system really.
"uh uh uh, you didn't say the magic word"
-Jurassic Park
You cannot test pixel per pixel like you are saying w/o some other type of testing. If you add enough acceleration to an object it will in time never contact the other object but it will skip over it. This is because the velocity between frame 1 and frame 2 is such that it never fails the hit test because it never lies within the boundaries of the object being tested against.
Solution is to integrate between point 1 and point 2 over smaller time increments. This can be done using linear interpolation and there are other methods as well.
Do not do the precise hit testing until the imprecise test fails. If the imprecise test fails then there is a possible hit. The imprecise test must be based on total distance and not bounding boxes. There is a Taylor power series that you can do to estimate total distance between two objects w/o using sqrt().
This would be your trivial rejection or imprecise hit testing.
I prefer using the exclusion method, where you remove the cases where they don't overlap. If all those cases are removed, the only possible outcome is that they do overlap. It also gives faster code, since you don't have to do all calculations to get an outcome (the worst case still requires it though). Here's an example:
Code:bool Overlap(OBJECT& o1, OBJECT& o2) { if(o1.x >= (o2.x + o2.width)) return false; if(o2.x >= (o1.x + o1.width)) return false; if(o1.y >= (o2.y + o2.height)) return false; if(o2.y >= (o1.y + o1.height)) return false; return true; }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Very good. However you have incurred 4 adds per detection - simply store x and x2 , y and y2 in the object structure.
Well, that depends on how you're using it. If you store both corners separately you have to update both when the rect moves (if it does), while if you're using one global corner and the other relative to the first (width, height) you only have to update 1.Originally posted by Bubba
Very good. However you have incurred 4 adds per detection - simply store x and x2 , y and y2 in the object structure.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.