Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Circular collision detection

This thread is locked; no one can reply to it. rss feed Print
 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!

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Neil Black
Member #7,867
October 2006
avatar

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
avatar

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:
sqrt(x ^ 2 + y ^ 2) = r
...which is, BTW, an equation for a circle about the origin.
Now to use this for sphere <-> sphere collision detection, you'd simply check whether the sum of their radii is larger than the distance between their centers:
sqrt( (x2-x1) ^ 2 + (y2-y1) ^ 2 ) < (r1 + r2)
Since squaring is cheaper than square-rooting, it is a good idea to use take the square of both sides of the equation (which is OK since we're not interested in negative results):
(x2-x1) ^ 2 + (y2-y1) ^ 2 < r1 + r2
Since C doesn't come with a square operator, we need to use a function for that:

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.
The rectangle part is just about using bounding-box checks (basically, a series of 4 simple comparisons) BEFORE the above sphere test, to filter out the obvious non-collisions in a cheap way, avoiding most of the sphere tests (which involve expensive squaring).
In my example, I have used a C++ template for the sqr() function, so that it works with all data types that have a * operator.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Neil Black
Member #7,867
October 2006
avatar

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
avatar

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

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

Neil Black
Member #7,867
October 2006
avatar

I never would have caught that, so thanks to you too!

 1   2 


Go to: