# PlayerObject to Collide with an Array of Objects

• March 13th, 2013, 09:25 PM
floorplay
PlayerObject to Collide with an Array of Objects
Hi, two empty methods have been supplied and its my job to implement both of them. Please tell me if the first method is correct? Also the program itself consists of a 'playerObject' (a circle) which can be moved around with the cursor keys. Another 4 random sized stationary circles and their positions are also displayed which are held in the GameObject barriers[]. What I'm having trouble with is the 'collision' method. I would like to implement the radius of the 'object' and compare that with the radius of the 4 other circles in the 'barriers[]'. So if the separation between them is less than the radius of all the objects a collision is detected. I have no idea how to do this. I can get the Width of the 'GameObject object' but what about the 'GameObject barriers[]' ??? Can i do this without using any standard libraries? Thanks and sorry if I asked too many questions.

Code :

``` public static double distance(GameObject object1, GameObject object2) // TODO stub { float x1 = object1.getX(); float y1 = object1.getY(); float x2 = object2.getX(); float y2 = object2.getY(); System.out.println(x1 + " " + x2 + " " + y1 + " " + y2); return Math.sqrt(Math.pow((x1 - y1), 2) + Math.pow((x2 - y2), 2)); }   /** * Determine if the object collides with a barrier * @param object the player object * @param barriers array of barriers * @return true if the player collides with a barrier */ public static boolean collision(GameObject object, GameObject[] barriers) // TODO stub { float radius = object.getWidth()/2;   return false; }```
• March 13th, 2013, 09:41 PM
floorplay
PlayerObject to Collide with an Array of Objects
Hi I have been given two empty methods which need to be implemented. I have done the first one, I'm pretty sure its correct but the second method 'collision' has confused me. The program itself displays a circle (a playerobject) which moves around with the cursor keys. 4 other circle are also displayed at random positions and sizes and are held in the barriers[] array. I would like to implement the method by calculating to see if the seperation between the playerobject and the other 4 circles is less that the radius of the objects. But how do i do this? That array is confusing me! I can get the radius of the 'GameObject object' but what do I do with the 'GameObject[] barriers'? Can this all be done without using any standard library methods apart from the one used in the first method?
Some help would really be appreciated!

Code :

``` public static double distance(GameObject object1, GameObject object2) // TODO stub { float x1 = object1.getX(); float y1 = object1.getY(); float x2 = object2.getX(); float y2 = object2.getY(); System.out.println(x1 + " " + x2 + " " + y1 + " " + y2); return Math.sqrt(Math.pow((x1 - y1), 2) + Math.pow((x2 - y2), 2)); }   /** * Determine if the object collides with a barrier * @param object the player object * @param barriers array of barriers * @return true if the player collides with a barrier */ public static boolean collision(GameObject object, GameObject[] barriers) // TODO stub { float radius = object.getWidth()/2;   return false; }```
• March 14th, 2013, 06:27 AM
Norm
Re: PlayerObject to Collide with an Array of Objects
Quote:

So if the separation between them is less than the radius of all the objects a collision is detected. I have no idea how to do this
Compute the distance between the centers of two circles and compare that to the sum of their radius.
Use some geometry to compute the distance using the x and y values.