Background:
I have two 2d arrays. Each index within each 2d array represents a tile which is drawn on a square canvas suitable for 8 x 8 tiles.
The first 2d array represents the ground tiles and is looped and drawn on the canvas using the following code:
//Draw the map from the land 2d array
map = new Canvas(mainFrame, 20, 260, 281, 281);
for(int i=0; i < world.length; i++){
for(int j=0; j < world[i].length; j++){
for(int x=0; x < 280; x=x+35){
for(int y=0; y < 280; y=y+35){
Point p = new Point(x,y);
map.add(new RectangleObject(p,35,35,Colour.green));
}
}
}
}
This creates a grid of green tiles 8 x 8 across as intended.
The second 2d array represents the position on the ground. This 2d array has everyone of its indexes as null apart from one which is comprised of a Person class.
Problem
I am unsure of how I can draw the position on the grid. I was thinking of a similar loop, so it draws over the previous 2d array another set of 64 tiles. Only this time they are all transparent but the one tile which isn't null. In other words, the tile where Person is located.
I wanted to use a search throughout the loop using a comparative if statement along the lines of
if(!(world[] == null)){
map.add(new RectangleObject(p,35,35,Colour.red));}
However my knowledge is limited and I am confused on how to implement it.
Do not use a second Array at all. Simply create a RectangleObject named, for example, currentPosition or activeTile or personPosition.
You, however, do not really need to store the Point in the RectangleObject. Either use a 2D RectangleObject array for that (so that you can use the indexes to access them) and exclude the Point information in the RectangleObject,
or
create a List of RectangleObjects and add the Point information - but do not increment by 35, but rather by 1. When you're drawing the tiles, you can still (by knowing the index) figure out where to put the tile (e.g. indexX*tileWidth, indexY*tileHeight).
(It's not quite clear from what you've written what world and map are used for. Please explain so I can give a better answer)
Related
I have a GUI Maze game that I am creating and I am having trouble implementing walls that prevent the player from moving. The walls are rectangles but the player is a circle so I had to create a collision detection method between a rectangle and a circle. The problem I am having is that my collidesWith() method is in the player class and it takes a single wall as a parameter and returns a string that tells you which side of the wall is the player intersecting with. This means I can only check if the player is colliding with one wall at a time. Here is the code for the collidesWith() method. x and y are the x and y coordinates of the player since this method is in the player class:
I should mention that the code above doesn't exactly work how I wanted it to since it only works when the player is coming from the sides. If the player is coming from the top or bottom, the player just goes through the wall.
The reason I need this method to return a string to tell me where the player is coming from is so that I can explicitly restrict the movement of the player when the up, down, left, right keys are pressed. This is in another class where all the GUI components are. Here is the code for that. I have created a wall object on top so that it can be passed as a parameter here when checking for intersection:
In my view, it is better to avoid to iterate through the whole array of wallsArraylist. If I understood correctly, it is necessary to explore neighbour cells, if yes, then you can explore neighbours in a two-dimensional array using this approach:
(pseudo-code)
row_limit = count(array);
if(row_limit > 0){
column_limit = count(array[0]);
for(x = max(0, i-1); x <= min(i+1, row_limit); x++){
for(y = max(0, j-1); y <= min(j+1, column_limit); y++){
if(x != i || y != j){
print array[x][y];
}
}
}
}
In addition, try to avoid magic strings. Replace "comingFromRight" and etc to constant string:
public static final String COMING_FROM_RIGHT = "comingFromRight";
I have a TiledMap map with some object layers like "wall", "door", "spike"...
Each layer has only rectangles, at their specific positions.
What is the best way I could check if a specific cell of the tile layer contains an object from a specific object layer?
To do something like if Cell of the tile layer at (x,y) contains an object from a object layer "wall", say "can't move there".
I just started using libGDX and Tiles, and the current way of detecting something like this that I can think of would be to create all those rectangles and check if they overlap with the player rectangle each time a player moves to the next cell.
But that would be checking every single object, and I need to just check the one cell the player is currently in.
One way you could reduce computation (this only works if the walls/doors/etc. are all static and don't move or change shape) is by keeping a 2D array of booleans (basically your TiledMap) with true meaning that tile is blocked and vice versa.
In pseudocode:
// Initially all grid cells are un-blocked
boolean[][] gridBlockedByWall = new boolean[MAP_HEIGHT][MAP_WIDTH]; //Replace with your grid size
// Loop over each wall and set array (change depending on your implementation)
for (Rectangle wall: walls) {
// Here we run over every wall and column covered by this rectangle and set it as blocked
for (int row = wall.y; row < wall.y + wall.height; row++) {
for (int col = wall.x; col < wall.x + wall.width; col++) {
gridBlockedByWall[row][col] = true;
}
}
}
Now to test if position (x, y) is blocked by a wall simply do gridBlockedByWall[y][x] which will run in constant time.
I'm trying to implement Collision detection using Rectangles for my game but can't seem to get it to work.
Bullets are updated when pressing space every frame so they move across the screen. Apparently so I've been told on here, the collisionbox moves with it as well. I'm getting this by using sprite.getBoundingBox() which sets a datafield Rectangle to this value for each instance.
I have two Arraylists, one holding instances of Monsters and one holding instances of Bullets. They all have collisionRectangles associated with them.
I'm calling the checkCollisions() method from my main render loop every frame as long as there's something in the bullets or monsters arraylist.
/*this handles collisions between bullets and enemies*/
public static void checkCollisions(){
for (int i = 0; i < Main.currentBullets.size(); i++){
for (int k = 0; k < Main.currentMonsters.size(); k++){
if (Intersector.overlaps(Main.currentBullets.get(i).collisionBoxBullet,
Main.currentMonsters.get(k).collisionBoxMonster)){
System.out.println("Boom headshot");
}
}
}
Shouldn't this do Boom Headshot every time it overlaps? Instead it always seems to keep evaluating to true.
Why does it keep evaluating to true and writing boom headshot?
What I didn't do is set the position of the rectangle of the sprite which was at 0.0. I also then didn't move the collision box properly with collisionBox.setPosition().
It initially checking to see if sprite at 0/0 is colliding with collision box at 0.0 which is always true, hence always saying it's overlapping.
I have an ellipse:
Ellipse2D e2D = new Ellipse2D.Float(startPoint.x, startPoint.y, x - startPoint.x, y - startPoint.y);
And what I need is to get coordinates of all points that form circumference.
ArrayList<Point> oneDraw = new ArrayList<>();
for (int i = startX; i < borderX; i++)
for (int j = startY; j < borderY; j++)
if (e2D.contains(new Point(i, j)))
oneDraw.add(new Point(i, j));
By doing so, I put all coordinates that are inside my circle to the list, but I don't need this.
Thank you for the answer and spent time.
Create an ellipse that is a little larger than the target ellipse.
Create an ellipse that is a little smaller than the target ellipse.
Subtract the 2nd ellipse from the first. This will form an elliptical ring.
Do the current 'contains' code with the elliptical ring.
Your approach would add all pixels in the box which are inside the ellipse to the list, i.e. you'd get the area of the ellipse rather than its circumference. I'd say it would be better to look for the correct formula and solve it for discrete x/y pairs.
Or yet better apply one of the algorithms for drawing ellipses that can be found on the net, e.g. this one: http://www.mathopenref.com/coordcirclealgorithm.html
Then get the pixels that have been drawn (if still needed).
Edit: if you have a look at the source code of Ellipse2D you can either get an an idea of how to implement your own algorithm or you could just use getPathIterator() with a uniform transform and then "rasterize" the path elements into your list.
I currently have a program that draws overlapping rectangles and was wondering if there is an easy way to determine which shape is on the top (most visible). This has me stumped as there is no z axis to use like in when dealing in 3D.
I have tried looping through the rectangles and using the .contains method but it returns all rectangles under a specific point and not the highest level one.
I have also searched around but perhaps I'm using the wrong keywords?
Normally when people do painting code they do something like:
List rectangles = ....
for (int i = 0; i < rectangles.size(); i++)
//paint the rectangle
So if you want to search for a Point to determine what Rectanle it is in then you should use:
for (int i = rectangles.size() - 1; i >= 0; i--)
{
if (rectangles.get(i).contains(yourPoint))
{
// do something
break;
}
}
Starting from the end will give you the last rectangle painted which means it is on top of all aother rectangles.