Collision detection is kind of funny, the algorithm you use depends on the shape of what is colliding and how accurately you want to determine collision. Therefore, there is no overall "API" for it, not to mention, different applications require a different degree of accuracy.
Example algorithm for circles: Given circleA and circleB. [code]if the distance between circleA.midpoint and circleB.midpoint is <= the radius of either circleA or circleB then Collision[/code] As for accuracy... Physics simulations use a good deal of calculus to predict ahead of time when two objects (in the literal sense of the term) will hit each other. Typically math is done including object's acceleration in the calculations. On the other hand, many games do not use acceleration and only have velocity to worry about. In this case, just doing estimates to the first derivitive (velocity) will suffice. The simplest (and most inaccurate!) collision detection just checks periodically to see if two objects are about to or have just overlapped (for Calculus fans out there, this is just Y= with no velocity or acceleration components at all), and if so, does whatever needs to be done for a collision. Of course, if you don't check often enough for collisions, objects pass right through each other! oops! If you check too often, you spend all your time on collision detection (and you can spend ALL your time on collision detection, especially if you have, say, 10,000 objects on the screen at once. ) Some quick math showing how icky this gets: 5 objects you want to do collision detection with. A, B, C, D, and E. So you have to check: A <--> B A <--> C A <--> D A <--> E B <--> C B <--> D B <--> E C <--> D C <--> E D <--> E This should be a familiar pattern: A combination! Namely, N elements, taken 2 at a time. nCr. (If anyone ever told you not to pay attention in math class, now is the time to slap them!) You do not want to check all objects on the screen at the same time! A typically (and relatively straightforward) solution is to subdivide the screen into "regions" and only perform collision checking on objects close to each other (in the same region) Of course, if you only have a dozen or so objects on screen at once, just brute force it. :-D Oh, since all of the above is mostly useless, here is a link to a ready made collision algorithm that should do fine for 2D. :-D http://www.yov408.com/html/download.php?b=collision_detection&c=20 [Message sent by forum member 'devlinbentley' (devlinbentley)] http://forums.java.net/jive/thread.jspa?messageID=214334 =========================================================================== To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff JAVA2D-INTEREST". For general help, send email to [EMAIL PROTECTED] and include in the body of the message "help".
