# Collision Detection

Veronica Love

Greenhorn

Posts: 8

posted 3 years ago

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.

Thanks.

Campbell Ritchie

Sheriff

Posts: 48652

56

posted 3 years ago

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.

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

posted 3 years ago

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.

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.