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: A* Search for pathfinding in a game

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A* Search for pathfinding in a game

    I have implemented the A* search algorithm in a game for pathfinding AI. For some reason when I am in the positive X, Y range in relation to the entity tracking me, the Entity jitters back and forth. I printed the size of the node path returned and it would seem that it is generating two paths and getting stuck between them on each pass. This happens on a flat surface with no obstacles and within the level I built for testing with walls as well. Does anyone know any common things to check for in my algorithm? or if it may be outside the actual search method?
    public List<Node> findPath(Vector2i start, Vector2i goal, boolean boatOrFloat) {
    		List<Node> openList = new ArrayList<>();
    		List<Node> closedList = new ArrayList<>();
    		Node current = new Node(start, null, 0d, start.getDistance(goal));
    		openList.add(current);
    		while(!openList.isEmpty()) {
    			Collections.sort(openList, nodeSorter);
    			current = openList.get(0);
    			if (current.tile.equals(goal)) {
    				List<Node> path = new ArrayList<>();
    				while (current.parent != null) {
    					path.add(current);
    					current = current.parent;
    				}
    				openList.clear();
    				closedList.clear();
    				System.out.println("" + path.size());
    				return path;
    			}
    			openList.remove(current);
    			closedList.add(current);
    			for(int i = 0; i < 9; i++) {
    				if (i == 4) continue;
    				int x = current.tile.getX();
    				int y = current.tile.getY();
    				int xi = (i % 3) - 1;
    				int yi = (i / 3) - 1;
    				Tile at = getTile(new Coord(x + xi, y + yi));
    				if (at == null) continue;
    				if (at.solid(boatOrFloat)) continue;
    				Vector2i a = new Vector2i(x + xi, y + yi);
    				double gCost = current.gCost + current.tile.getDistance(a) == 1 ? 1 : 0.95;
    				double hCost = a.getDistance(goal);
    				Node node = new Node(a, current, gCost, hCost);
    				if (vecInList(closedList, a) && gCost >= node.gCost) continue;
    				if(!vecInList(openList, a) || gCost < node.gCost) openList.add(node);
    			}
    		}
    		closedList.clear();
    		return null;
    	}

  2. #2
    Junior Member
    Join Date
    Sep 2021
    Location
    united states
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A* Search for pathfinding in a game

    While I do not have a lot of knowledge about java programming, I am learning it. I i was wondering if we could make a Java game?

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Location
    united states
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A* Search for pathfinding in a game

    Quote Originally Posted by ian daniel View Post
    While I do not have a lot of knowledge about java programming, I am learning it. I i was wondering if we could make a Java game?
    i just want to add on can we make modded games with java programming? like this site has developed a moded game https://kickthebuddyapk.com

Similar Threads

  1. Replies: 1
    Last Post: August 20th, 2014, 02:10 AM
  2. A Pathfinding Story
    By fatchance30 in forum Java Theory & Questions
    Replies: 7
    Last Post: April 27th, 2014, 11:53 PM
  3. Graph Search Theory with extend of BFS, UFS and A* Search in grid
    By keat84 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 7th, 2012, 09:46 AM
  4. A Star Pathfinding algorithm optimization
    By randomcracker in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 18th, 2011, 11:11 PM
  5. Search Word puzzle game
    By lew1s in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 9th, 2011, 04:23 AM