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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Distance not being calculated properly

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    Please help me to improve my code.

    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)
    	{				
    		float palyerRadius = object.getWidth()/2;
    		float barrierRadius = 0;		
     
    		for (int i = 0; i < barriers.length; i++)	
    		{	
    			barrierRadius = barriers[i].getWidth()/2;
    			float sumOfRadius = barrierRadius + palyerRadius;
    			if (Submission.distance(object, barriers[i]) < sumOfRadius)			
    			return true;
    		}		
    		return false;
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Distance not being calculated properly

    No way to compile and test the code without all the needed parts.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Distance function help
    By abf617 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 2nd, 2012, 06:32 PM
  2. Getting Distance between two addresses
    By Shockwave786 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 23rd, 2012, 10:19 AM
  3. Applet: Calculated variable can't be displayed
    By iTTerror in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 12th, 2012, 01:23 AM
  4. Distance between 2 points
    By captain in forum Java Theory & Questions
    Replies: 3
    Last Post: February 22nd, 2012, 12:53 AM
  5. Won't display calculated fahrenheit and celsius when running.
    By smithmar in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 17th, 2012, 09:00 PM