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 3 of 3

Thread: PlayerObject to Collide with an Array of Objects

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

    Default 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
    	{						
    		float radius = object.getWidth()/2;		 				
     
    		return false;
    	}


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

    Default 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
    	{						
    		float radius = object.getWidth()/2;		 				
     
    		return false;
    	}

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

Similar Threads

  1. array of drawn objects
    By l.oomen in forum Loops & Control Statements
    Replies: 2
    Last Post: October 14th, 2012, 01:56 PM
  2. Array of objects
    By spark in forum Object Oriented Programming
    Replies: 2
    Last Post: August 1st, 2012, 03:06 PM
  3. two animated sprites collide
    By risen375 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 22nd, 2011, 08:23 AM
  4. all objects in array the same?
    By abrohm in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 17th, 2011, 11:21 AM
  5. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM