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.


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

Results 1 to 2 of 2

Thread: Search closest distance to an array of objects

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Thanked 0 Times in 0 Posts

    Default Search closest distance to an array of objects

    Hi, could somebody please explain to me how to search an array that will find the index that contains the closest object to another object, then move to it? I have an array with 8 gameObjects (circles with 50 radius) and a single 'playerObject' that must move over the top of the nearest circle in the array and then move over the top of the next nearest circle until all circles (gameObjects) have been 'eaten'. I would like to use the Euclidean distance. One more thing the positions of the gameObjects (Array) and the playerObject is randomized at each start of the program.

    So basically determine which is the closest barrier(gameObject) from the current position of the player object.

    All I have so far is a loop going through the array and some pseudo-code
    //newX = barriers[];
    //value to move to barrier?
    //Use Euclidean distance then update
    //increment value to move it
    //'gameObject' is the way you access the components
    //of the GameObject that this component belongs to
    //when a GameObjectComponent is added to a GameObject,
    //the function "AddComponent" automatically assigns
    //the GameObject itself to this 'gameObject' variable.
    //see 'GameObject.java' for more details.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: Search closest distance to an array of objects

    I would recommend you draw out some examples and think through how you'd figure it out in your head. Write out the process you go through for each step, and you'll have an algorithm that you can then start implementing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Search for the Array
    By Lurie1 in forum Collections and Generics
    Replies: 14
    Last Post: February 18th, 2012, 12:45 PM
  2. Array Search Method
    By Kyuubi426 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 08:31 PM
  3. [SOLVED] Help with array of Strings...Linear Search?
    By Stockholm Syndrome in forum Collections and Generics
    Replies: 4
    Last Post: March 22nd, 2011, 03:31 PM
  4. closest pair algorithm implementation
    By srose in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2011, 02:11 PM
  5. [SOLVED] Couldn't search for a string in an array.. Help please..
    By astrojunk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 10:47 PM