|
This thread is locked; no one can reply to it. |
1
2
|
Circular collision detection |
Bruce Perry
Member #270
April 2000
|
Quote: Floats, as far as IEEE-whatever non-two's-compliment, as PCs are, can also be abs'd by simply clearing the top bit (AFAIK). Apparently this isn't a good idea, because the processor's caching is separate for ints and floats, and whenever you reinterpret one, it has to wait ages flushing its cache. For floats, 'fabs' is probably implemented in the FPU and pretty fast. For ints, I tend to do this: //int y=abs(x); int y=(x^((signed int)x>>31))+((unsigned int)x>>31); In two's complement arithmetic, you negate a number by twiddling all the bits and then adding one. In the above code, the XOR will twiddle all the bits if the top bit is set, and then add one if the top bit is set. It does rely on a few implementation-specific aspects of C though. However, after all that, circle collision detection is probably going to be cheaper! -- |
Neil Black
Member #7,867
October 2006
|
Okay, I do know the Pythagoran theorem, but I don't know how to use it in programming, just the basic a^2 + b^2 = c^2. I'm actually very good at math, if bored by it, once I've had the chance to learn.
|
Tobias Dammers
Member #2,604
August 2002
|
Quote: Collision detection is the hardest thing I've done (using math) in programming to date. I don't know how to check collision with a circle, and all the code on this site is just a jumble of variables and numbers! When asked how to collide a circle with a rectangle the answer was to check collision with the closest part of the recatangle, but I don't even know how to find that! I'm fairly good at math, but I haven't had the opportunity to learn any of this stuff yet. I'M IGNORANT!!!!! Is there any website out there that goes over this kind of math in a simple, easy to understand way? Please cut down on the attitude a little. Just screaming that you're ignorant doesn't make it any likelier someone will really help you. I also recommend you read this. OK, so if you know the pythagorean theorem, that's a start. You can use it to calculate the shortest distance between two points when given their carthesian coordinates (a.k.a. "x" and "y"): just think of c as the distance, and a and b as the x and y coordinates respectively. Solving for radius, this gives you: sqr(x2-x1) + sqr(y2-y1) < r1 + r2
Only problem is that C doesn't define a sqr() function either, but you can easily do that yourself. I already gave you the code. --- |
Neil Black
Member #7,867
October 2006
|
Wow, you just made that almost perfectly clear to me. What little I didn't get I'll get when I actually try to use it and have to think it through properly. Thank you Tobias Dammers!
|
Indeterminatus
Member #737
November 2000
|
Quote: (x2-x1) ^ 2 + (y2-y1) ^ 2 < r1 + r2 Just a little correction (which was mentioned several times in this thread already, so it must've slipped Tobias this time): If you decide to compare the squares, you have to square the entire thing, which means also the right side of the inequation (x2-x1) ^ 2 + (y2-y1) ^ 2 < (r1 + r2) ^ 2 _______________________________ |
Neil Black
Member #7,867
October 2006
|
I never would have caught that, so thanks to you too!
|
|
1
2
|