Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collision Detection

 
Veronica Love
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic