Skip to main content

collision detection in Java 2D???

4 replies [Last post]
mgv
Offline
Joined: 2007-04-25

Hello,

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.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
orelero
Offline
Joined: 2007-08-23

consider this library Kollider2D (also available on google code).
New update very soon (today or tomorrow).

devlinbentley
Offline
Joined: 2007-04-06

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

rivasdiaz
Offline
Joined: 2003-06-11

Check package java.awt.geom.
The classes PathIterator and Path2D are enough to implement collision detection in Java2D. I don't know OdeJava though...

pepe
Offline
Joined: 2003-06-10

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:
http://frederic.barachant.com/prog.html
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 !