This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Collision Detection" Watch "Collision Detection" New topic
Author

Collision Detection

Veronica Love
Greenhorn

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.

Thanks.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
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
Greenhorn

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).

Thankyou.


 
jQuery in Action, 2nd edition
 
subject: Collision Detection
 
Similar Threads
Calculating the distance between two points problem
equality doubt
Use of the "This" keyword, and "Point" objects.
New College Project
java problem