Storing Node Coorinates for AI Paths - java

I am currently working on a game and have come into a bit of a problem.
I am working on an algorithm that will create a path for the AI characters to follow in the game. It is a top down adventure game and the AI will choose a random location on a 50 by 50 map then take the shortest path, taking obstructions into account.
I originally had it so that the AI will use 0-3 to determine how to move. If it is 0, they move up, 1, right, etc. Now I am trying to use the A* algorithm to create a list of moves. Once they arrive, they will choose a new destination and the process will repeat.
The problem I am having is storing the squares. From what I understand, you need a closed list and an open list. I was planning on using linked lists for these, then ultimately using a third linked list that stores the path to be followed.
The issue is that I need to store both an x and a y coordinate. I thought I could just use two lists for each, but that seems inefficient.
By the way, I am using Java to program it.

Instead of having lists for each coordinate just wrap your x and y into a class. You can use the Point class or make your own storing the x and y and implementing a comparison to help with your A* Search. You can also have a look at Implementation of A Star (A*) Algorithm in Java

Related

libGDX: Implementing AI on a sidescroller

I'm working on a 2D sidescroller game.
Description:
Im using libGDX and the AI extension. The game will be released on android (the AI should not be heavy resurce consuming). My terrain is not grid based, it's a procedural generated polygonal height map (without caves).
There are 3 types of enemies (NPC) - near-, distance- (bullets) and combined-combat.
Entities have 3 ways to move - left, right and jump. Allso some can hover over the ground like on the picture. I tougth to make nodes with an y offset from the terrain should work but if a player jumps, a near-combat enemy couldn't have a path (I didn't test it but i assume it).
I seen many examples on grid based games but non for my scenario.
Excuse my less knowlege, I just jumed in AI devolepment a few days ago.
Questions:
Is the AI (from libGDX) compatible with an (almost) infinite world?
How should I setup nodes?
Can the AI be used to calculate bullet directions to hit a player?
First, I do not know libGDX but if you want to have an infinite world you have to program it. That means you generate the terrain randomly and destroy it in the memory if it is not needed any more. If you want infinitely go to the left and the right you have to store efficiently your world on your hard drive if the memory is full. For your polygons, you just have to store the nodes and some numbers as references to surrounding elements if you want to go back with your avatar.
Second, because your game is two-dimensional the possible paths are that simple that you need no path finding algorithms. So you do not need nodes or things like that. What you need is something like hit-boxes so that your enemies know when they hit each other. The AI needs only to know if your avatar is left or right and if it is reachable directly. That are simple geometry calculations. If it is possible to jump over other enemies it is as if they can go through them for the path.
Finally, the bullet direction is calculated simply with linear algebra to get the direct line from your avatar to your enemy. You need only to calculate if it intersects with other enemies or the terrain to know if your avatar can be hit.
The only AI aspect here is to determine the behaviour of the enemies. That can be done with a state machine, where the enemies have states like aiming, waiting, following or shooting. Depending on how far away your avatar is or how reachable it is the states change.

Finding the distance between two objects in a 2D array, not going Diagonal

Not sure if this was posted before, but this is a relatively short question
I am currently working on a maze game being chased by something.
Currently, the player starts at (0,0), and the monster starts at (9,9). If moving is incrementing/decrementing one (not both), what is the algorithm/code to find the amount of moves it'd take for the monster to reach the main character?
From the comments I realized I should have clarified a few thing.
If the room type is 1, then it's a wall, else is open. But the main thing is that the walls do NOT impact the monster. Perhaps the better way to ask is how many moves would it take if all of the arrays were open.
You could take a look at the A* search algorithm which is widely used in games since it uses a heuristic to make performance improvements.
In your case, the heuristic could be the Manhattan Distance metric to calculate the distance between your grid elements. This distance metric takes into consideration the total of the differences in the X and Y coordinates (which unlike euclidean distance, does not allow for diagonal traversal).
To use this algorithm however, you would need to make a graph representation of your map, something like below:

Random actions in Java

I'm writing my first own chess game in Java.
My code already works for two players, manually playing from the keyboard, and I would like to expand it to a player against the computer game.
My problem is that I've never dealt with random actions.
The method which is moving a piece on the board, needs to receive 4 inputs:
two integers for the piece location, and two integers for the destination on the board.
Choosing the wrong inputs is OK since the movePiece method already checks if the integers are not out of the board's bounds, that they really reach a piece of the current player's color, that there's a piece in the coordination and its not empty, etc.
But how do I get these 4 integerss randomly? And how do I make them as closest as possible to "real" inputs (so I don't spend a lot of time disqualifying bad inputs)?
Java provides at least two options to generate random values:
http://docs.oracle.com/javase/6/docs/api/java/util/Random.html
http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#random()
E.g. to get random int values bounded by 4:
int x = new Random().nextInt(4);
int y = (int) (Math.random() * 4);
System.out.println(x + " " + y);
>> 2 3
If you really would like to create a computer player who just moves about randomly, I think it would be better to do it it like this:
Randomly select one of the existing chess pieces on the board
Compute all legal destinations to which that chess piece could move
If there are no legal destinations, choose a new piece amongst the previously not selected pieces
Randomly select one of the computed legal destinations
However, letting a computer player move about randomly is rather silly. A computer player would need to have some form of logic to compare which out of two moves is better. Implementing such logic is probably rather complex, and you're probably better off using a chess library as someone suggested in the comments.
You don't need 4 ints. You only need 1.
A way of doing this:
Iterate the pieces on the board.
For each piece, determine its available moves and stick all the available moves in a list.
Pick one move from the list at random (or at non-random, if you feel so-inclined).

Java More Resourceful Collision Detection

I am making a game in java which involves characters moving around a map and having some solid collision objects (i.e. buildings) placed around the map by reading certain data from a text file. There will be multiple maps where these objects' locations will change. My question is would painting a rectangle in a certain color that indicates collision behind such structures or would reading mouse coordinates and searching an array of these structures to see if that point lies on a building, thus denying the move or altering, be more resourceful and/or quicker. If painting a rectangle is the best, would leaving it behind the structure or deleting it after detecting for collision be better. Thanks for your time!
In my junior year in college I worked on a Collision detection system algorithm for the windows phone. It is hardly perfect but it was EXTREMELY efficient and can be adapted to a majority of games.
The way that it worked was pretty simple. There were two types of objects; Collidable objects (such as enemies or buildings) and Objects that you wish to check for collisions with these collidable objects.
I had this idea when I was going through a data structures class and we spoke about Linked Lists. I thought what if each link was a collidable object that you could stick your game objects that were already created in it. Then as the game objects moved around you would have a lightweight way of checking their locations for collisions. Thus my system was born.
All it really is, is a class that fires off either every game cycle or when ever you choose to check for collisions. You give it your players location, or bullet location or what ever object you want to see if it is colliding with something and it searches all of the collidable object locations and conducts test to see if they are overlapping.
The real efficiency of it comes into play when you add in a second element (Locations AND quadrant)
For Example if I break the phone screen up into for parts and I know which quadrant my player or bullet is in I can choose to only scan a list of collidable objects that are within that quadrant. Thus cutting your search algorithm to a fourth of its origonal size.
There are many different ways of detecting collisions. This was a simple example I used in my class to show how you could detect two circles colliding that were actually squares. As you can see simply by taking the center point coords of the circles and the radius's you can calculate the hypotenuse and determine where or if they are touching.
Good luck! if you have any questions feel free to ask!
The last reply in this posting may help you out. It is a simple maze. The structure of the maze is controlled by a data file which simply contains 0, 1 to indicate a path or a wall. You navigate through the maze using the arrow keys. When an arrow key is pressed the code checks to make sure the next square is not a wall.

Java Collision Detection Not Working

I have made threads in the past about similar questions but because of my lack of detail the answers have not really been related to what I needed so I am going to try explain my question in as much detail as I can and hopefully it will be easier for you to understand what I require.
I watched Bucky's slick game tutorials on youtube and made a 2D Java game, the game is basically a 2D player viewed from above (birds eye view) can move around a 2D map with user key input (up, down, left, right). The map the player moves around is very small so that meant boundaries had to be set so that the player could not walk off of the map, to give you a better idea of how this was done, here is the tutorial for setting up the voundries:
http://www.youtube.com/watch?v=FgGRHId8Fn8
The video will also show you exactly what the game is like.
The problem is, these boundaries only require one axis meaning that if the player is walking down you say something like "if player gets to the coordinate (number) on the X axis change the player movement to the opposite direction so that he can not go any further."
Now this creates a problem for me because this only requires one axis so it easy to set up and understand but if you look on the video, on the map there is a house and I want my player not to be able to walk over that also but this deals with 2 dimensions, I have looked at things like rectangle collisions and have seen things relating to them in the other posts but I get confused because I am new to Java and havent really done much with it at the moment apart from watching Bucky's tutorials.
My code at the moment for my game class has got the following methods: init, render and update. So to sum it up I really just want to set up a way of not letting my player walk through the house, I will mention also (I should have mentioned it in my other threads) as I am very new to Java, could you please take a step by step method of showing me how to set up the collisions, I mean even the basics of things like making the rectangle if required.
If my code is required please tell me and I will post it as soon as possible.
Thank you in advance.
You can set up the board as a 2x2 grid of a class that has has property such as 'isBlocked'. By default the edges of the board would have this property set to true to prevent the character from walking off the edge. When you add other obstacles such as a house or a wall the grid position(s) the object occupies would also have the property set to true. Then when moving a character you just check if the grid position the character moves to has the property set to false to see if it's an allowable move. This also makes it quite trivial to save the level data so you can just load them from disk later on.
Two possible options:
Extend Shape or Rectangle or the relevant Slick objects (they should exist IMO) and just check for intersect()
Look for (x1,y1) and (x2,y2) values such that it starts outside and ends up inside.
Assuming you have intersect() methods:
//Grab the previous position
prevPosX = movingobject.X;
prevPosY = movingobject.Y;
//Update the position
movingobject.update();
//Test for collision
if(movingobject.intersects(targetobj)) {
//If it collided, move it back
movingobject.X = prevPosX;
movingobject.Y = prevPosY;
//And reverse the direction
//(might want to do other stuff - e.g. just stop the object)
movingobject.speed *= -1; //Reverse the speed
}
in this case your update class should also add one more condition to look for the house. let say the cooridnates of house(assuming rectanglular house here for other shape just change x and y values) are (x1,y1)(x1,y2)(x2,y2)(x3,y1) you have to add a condition to make sure
x value is not between x1 and x2 and at the same time y value cannot between y1 and y2.

Categories