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: Distance not being calculated properly

1. ## Distance not being calculated properly

The first method below is suppose to calculate the distance between two objects. The second method is suppose to return true if the 'playerObject' (a single circle) and the 'barriers' (an array of circles) object collide. I have tried to sum the radius of the objects and compared that to being less than the distance being calculated in the first method. At this time the collision is being detected but roughly 50% of the time the playerObject doesn't quite touch a barrier, stopping roughly 5 pixels from the edge of a barrier circle at times. When I ran a Junit test it failed. The result was suppose to be 6.884 but I was getting 9.884.

```public class Submission {
/**
* Compute the distance between two objects. The distance may be negative if
* the two objects overlap.
* @param object1 first object
* @param object2 second object
* @return the distance between the two objects
*/
public static double distance(GameObject object1, GameObject object2)
{

float x1 = object1.getX();
float y1 = object1.getY();
float x2 = object2.getX();
float y2 = object2.getY();

return Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 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)
{

for (int i = 0; i < barriers.length; i++)
{
return true;
}
return false;
}```

2. ## Re: Distance not being calculated properly

How can the code be compiled and executed for testing? I don't see a main() method.
Are all the used class definitions available?

3. ## Re: Distance not being calculated properly

There are 6 classes including the Test and Main class. Yes I have all of them.
I was just hoping for some feedback on the code I had written thus far for any obvious mistakes that could be fix. At the moment the program isn't stable...don't know why. Still have another 2 methods to finish as well! What should i do?

4. ## Re: Distance not being calculated properly

No way to compile and test the code without all the needed parts.