Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: PlayerObject to Collide with an Array of Objects

1. ## 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.

```	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
{

return false;
}```

2. ## 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!

```	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
{

return false;
}```

3. ## Re: PlayerObject to Collide with an Array of Objects

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.