How to implement collision detection in Java 2D?
Is there any dynamic engine API for Java 2D as OdeJava API for Java 3D?
Thanks for your help.
consider this library Kollider2D (also available on google code).
New update very soon (today or tomorrow).
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
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
Check package java.awt.geom.
The classes PathIterator and Path2D are enough to implement collision detection in Java2D. I don't know OdeJava though...
Some time ago, (was it a bit after 1.4 release?...) i did an experiment based on what was told not to be good to create games. Just to be plain nasty, i did ALL that was said to create problems and slow down everything. I created MASSIVE garbage, zoomed and rotated alpha bitmaps everywhere, did not care about object reuse and... handled collisions through java.awt.geom.Area/box intersection. Even at that time, none of the things i did changes my game into a slow unplayable thing.
It all depends on your domain of use, my experience might be of no interest to you, but if you are planning things like in GoSub, just check that. It is fast to implement and might be good enough for you too.
You can test it here:
Check the webstart link on top of the page. (but do not start fullscreen. There is a known bug with resolution reset that forces you to quit the game to retrieve the original settings.)
If you want to know more on the project, check the pdf on that same page or ask here.
Good luck !
Your use of this web site or any of its content or software indicates your agreement to be bound by these Terms of Participation.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.