Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [LIBGDX] Starting out on AI :)  (Read 1249 times)
0 Members and 1 Guest are viewing this topic.
Offline Delta517

Senior Devvie


Medals: 2
Projects: 1



« Posted 2013-09-19 23:30:54 »

Hi, guys! Smiley

So right now Im starting to really add a real AI for the enemies in my game. Lucky for me, all the enemy is going to do is chase the player. I have used a simple "if(player.getX() > enemy.getX()) speedX = 3;)", but that easily gets the enemy stuck of course. I was wondering if any of you know any examples for AI in Libgdx Smiley I have looked into the A* pathfinding and are considering using that one since it seems like the best choice out there. Does anyone happend to know how I can combine that with the use of Tiled Map Editor too? Smiley Its a simple 2D overview game by the way Wink

Thanks for reading Smiley
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #1 - Posted 2013-09-20 01:04:07 »

If its a non random world, you can make waypoints that the entity will walk to. Then you can make predefined paths that lead you to these waypoints. I'd suggest viewing this article on A* though, as you really should include pathfinding instead of baked in paths.
http://www.policyalmanac.org/games/aStarTutorial.htm

Offline Delta517

Senior Devvie


Medals: 2
Projects: 1



« Reply #2 - Posted 2013-09-20 15:18:58 »

So I have read through the site you sent me and Im trying to add it into my game which is using Libgdx and Tiled. I was wondering if you guys could take a look at what Ive got so far and say what you think? Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
private Array<Cell> openList = new Array<Cell>();
   private Array<Cell> closedList = new Array<Cell>();
   private Vector2 startingPoint, endingPoint, currentPoint;
   
   private int g_movementCost;
   private int h_heuristic;
   private int f;
   
   private float tileWidth = 32;
   private float tileHeight = 32;
   
   private TiledMapTileLayer layer;
   
   private Cell currentCell;
   private Cell tempCell;
   
   public void createPath(float startX, float startY, float endX, float endY){
      startingPoint.set(startX / tileWidth, startY / tileHeight);
      endingPoint.set(endX / tileWidth, endY / tileHeight);
     
      currentCell = layer.getCell((int) startingPoint.x, (int) startingPoint.y);
      currentPoint.set(startingPoint.x, startingPoint.y);
      openList.add(currentCell);
     
      //Top-Left
      if((tempCell = layer.getCell((int) currentPoint.x - 1, (int) currentPoint.y + 1)) != null){
         if(!isCellSolid(tempCell)){
            openList.add(tempCell);
         }
      }
      //Top
      if((tempCell = layer.getCell((int) currentPoint.x, (int) currentPoint.y + 1)) != null){
         if(!isCellSolid(tempCell)){
            openList.add(tempCell);
         }
      }
     
      //Top-Right
      if((tempCell = layer.getCell((int) currentPoint.x + 1, (int) currentPoint.y + 1)) != null){
         if(!isCellSolid(tempCell)){
            openList.add(tempCell);
         }
      }
      //Left
      if((tempCell = layer.getCell((int) currentPoint.x - 1, (int) currentPoint.y)) != null){
         if(!isCellSolid(tempCell)){
            openList.add(tempCell);
         }
      }
      //Right
      if((tempCell = layer.getCell((int) currentPoint.x + 1, (int) currentPoint.y)) != null){
         if(!isCellSolid(tempCell)){
            openList.add(tempCell);
         }
      }
      //Bottom-Left
      if((tempCell = layer.getCell((int) currentPoint.x - 1, (int) currentPoint.y - 1)) != null){
         if(!isCellSolid(tempCell)){
            openList.add(tempCell);
         }
      }
     
      //Bottom
      if((tempCell = layer.getCell((int) currentPoint.x, (int) currentPoint.y - 1)) != null){
         if(!isCellSolid(tempCell)){
            openList.add(tempCell);
         }
      }
     
      //Bottom-Right
      if((tempCell = layer.getCell((int) currentPoint.x + 1, (int) currentPoint.y - 1)) != null){
         if(!isCellSolid(tempCell)){
            openList.add(tempCell);
         }
      }
     
   }


Im also wondering if the next thing I should do now is to make it not add the same cell again in the openList by checking if its in the closedList, but Im not quite sure on how to do this Sad Do I need to loop through the closedList everytime I try to add something in the openList? And also, theres no "tempCell.getX or .getY()" which can give me the coordinate, but I guess it could be possible to make another Vector2 Array that could hold that infomation? Tongue
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (9 views)
2014-12-28 10:39:27

CopyableCougar4 (16 views)
2014-12-28 02:10:29

BurntPizza (20 views)
2014-12-27 22:38:51

Mr.CodeIt (13 views)
2014-12-27 04:03:04

TheDudeFromCI (18 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (93 views)
2014-12-09 22:41:13

BurntPizza (114 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!