File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Collision Detection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Collision Detection" Watch "Collision Detection" New topic

Collision Detection

Veronica Love

Joined: Mar 12, 2013
Posts: 8
Hi guys. I have recently began to learn Java at university and the below code is a snippet from a weekly task that is soon due. Just looking for some help towards acquiring an answer (no spoon feeding). Basically we need to write two methods. One to find the distance between the objects in the game (circles) which I have already done correctly I believe. However I'm not sure how to implement the 'collision' method. The 'GameObject' is a separate class from the class below. Basically, the game has a playerObject (circle) that moves around with the keys and if it touches any circle in the GameObject[] barriers the method returns false (or true?). Please any help would really be appreciated.


Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
Welcome to the Ranch

I shall move this thread to the GUIs forum.
Use the Math.hypot method for that calculation. One way you can do it is to see whether the separation is less than the radius of the objects, but that may only work for circles. I think many classes in the 2D package have methods for whether they intersect.
Veronica Love

Joined: Mar 12, 2013
Posts: 8
Hi thanks for the reply.
Could some please check if i have written the two below methods correctly? The playerObject is at least detecting the 'barrier' object now. However, sometimes the two don't quite touch each-other. The jUnit tests are also failing with the expected result being 6.844 but I'm getting a result of 9.844.
I can't seem to find the error. (Note that the 'playerObject' and the array of objects are all circles).


I agree. Here's the link:
subject: Collision Detection
It's not a secret anymore!