LibGdx / Box2D - How To Group Tiles Into One Larger Polygon
Jan 19, 2015
I am making a tile based top-down 2D RPG and am using Box2D for the physics. Since my game is tile-based, there are many tiles on each map that cannot be moved through. This results in many small individual Box2D bodies. This is obviously very inefficient and makes the game lag. Therefore I figured that combining the individual tiles' bodies into larger complex groups would be better.
The way I thought of doing it is to, first, group together tiles into groups of tiles that all share 2 vertices with at least one other tile in the group. Then, for each group I do the following. First I get all of the uncommon vertices (these should be the ones on the outside of the polygon). Then I connect all of those vertices and then remove all of the overlapping lines. This should result in only lines on the outside of the polygon. Then I sort the lines so that the first line in the sorted array shares its end point with the second line's start point, etc. Then I remove the doubled vertices and using those remaining vertices (I called them the "true vertices") I create the polygon. I know that Box2D only supports convex polygons, but with Box2DSeparator I should be able to do this, but I first want this method to actually work.
/**
* Attempts to combine the given blocks into larger bodies to improve performance.
* @param bs The created wall blocks.
* @param w The world. Used for body creation.
*/
private void makeLargeBodies(Block[][] bs, World w) {
Array<Tile> tiles = new Array<Tile>();
for(int i = 0; i < bs.length; i++) {
[Code] .....
However, this is where the first problem arises. This method is extremely slow for large maps (like 200x200 tiles). I have worked at this for very long and right now my head can't figure out how to make the loop more efficient...
Now for the next part. After that I attempt to create the bodies for the TileGroups:
//create all the groups' bodies
size = groups.size;
System.out.println(size + " groups");
for(int i = 0; i < size; i++) {
TileGroup g = groups.get(i);
System.out.println("Starting group " + i + ".");
long timeCheck = TimeUtils.millis();
g.createBody(w);
System.out.println("Group 0 took " + (TimeUtils.millis() - timeCheck) + " millis");
}
*Note that the printing is for debugging purposes*...
This method is probably not a problem; it is what is inside createBody(World) that is the issue...
public void createBody(World w) {
Array<Vector2> allVertices = new Array<Vector2>();
Array<Vector2> uncommonVertices = new Array<Vector2>();
System.out.print("Starting search for uncommon vertices. ");
long timeCheck = TimeUtils.millis();
[Code] .....
Most of the code is self-explanatory (with comments). My current issue is where I connect the uncommon vertices (see the printed statements). This method does not actually finish (I have let it run for several minutes and it does not complete). This is likely due to a large number of vertices (often around 3000 in a 60x60 map), but I cannot figure out how to make the loop more efficient... Because of this early failure I don't know if the rest of the method works, both physically and in theory.
All relevant classes (Tile, TileGroup, Line) are below:
private class Tile {
private Vector2[] vertices;
private TileGroup group;
public Tile(int x, int y) {
vertices = new Vector2[4];
package TheWorldCup; import java.util.ArrayList; import java.util.*; public class WorldCup2014{ public static void main(String args[]){ ArrayList<String> WorldCupTeams = new ArrayList<>();
[Code] ....
What it is doing is printing the team name and all eight groups and showing the group is: team name under. I want it to print out the team name, put it into a group and print out the group. Once 4 teams in a group, close that group off, do this until all groups are filled. How can I do this?
Lets say the JDialog opens 5x5. The user is allowed to modify its size to be any size larger than 5x5 but is not allowed to make it smaller. Is there such a property or will this have to be regulated via code?
I have an encoder that will shift the character of a string X places. Im trying to account for the possibility of the shift being larger than alphabet array length. I've come up with the following, but I still get an ArrayIndexOutOfBounds Error and it never gets past the if statement.
I am learning about arrays in my class and my professor has a habit of throwing in code without explaining. We are doing a program called storing largest numbers where we read data from a file and place the larger of the two numbers in the corresponding position of a third array. They are in 4 by 4 format. Here is the ending code
import java.io.*; import java.util.*; public class prog464aStoringLargestNums { public static void main(String[] args)
I'm working on is to create a program to display an n-sided regular polygon and uses two buttons named +1 and -1 to increase or decrease the size of the polygon. Also enable the the user to increase or decrease the size by clicking the right or left mouse button and by pressing the UP and DOWN arrow keys. So, first off I'm just trying to figure out how to display an n-sided polygon. I have some of the other components started, but I'm just trying to focus on getting this to work.
I was following a tutorial on how to make a game when i suddenly got an error concerning moving tiles. i get this error when i tried to use a and d to move:
Exception in thread "Display" java.lang.ArrayIndexOutOfBoundsException: 48600 at com.cherno.graphics.Screen.render(Screen.java:44) at com.cherno.Game.render(Game.java:124) at com.cherno.Game.run(Game.java:87) at java.lang.Thread.run(Unknown Source)
I do not get the error while using w or s but the tiles are still not moving when pressing them.
So I'd like to make like a library of tiles, and it isn't too big, which I could just call using a method, and I would specificy what information I need to get, and it would return that information.
For example, a small section of the tiles library is grass_tile, I call this library and specify what I need, and in this case let's say I need the width, or can the character enter it from north? or can the character just stand on it? So it's like a library or array, with sections of it being named by tiles, and by mentioning these tiles I can information about them.
gTile("grass","walkon") will return true, first because I specifiy grass, and yes the character can walk on grass, so it returns a boolean value of true.
gTile("stone","width") will return 16, first because I specify stone, and the stone's png image size is 16.
What I'm asking is not for how to work out if the character can walk on the tile, or read the width of a tile, but can I create like a library from which I can obtain information by giving the name of the tile, and then telling it what info I need.
If up key pressed && gTile((yTILE + 1).name(),"walkon") == true then moveChar("up")
I have a java program which rotates a rectangular polygon. My program is having a problem. As soon as it starts rotating, it shrinks. The polygon is rotating perfectly though. I am trying but unable to figure out the cause.Main class which has an infinite loop (game loop) to rotate polygon continuously :-Java Code:
I have a shape, Polygon (javafx.scene.shape.Polygon) and want to test whether a point (javafx.geometry.point2D) is contained within the shape. (contains doesn't work)
I can change the polygon to any type, but I need to be able to create it using the 4 corners points and add it as a child to the Pane.
I have a method that draws a polygon on the screen:
public void poly(List<Point> points) { //code }
For the usage of the method, it makes no difference whether points is an array or a List. In fact, switching doesn't even change any of the code in the method since I use a for-each loop. So, my question is, is it a better practice to use a List as an argument, or an array when the method itself doesn't care about which to use?
My code is giving me an error at the main method and it says that modifier 'static' is not allowed in constant variable declarations. every program that i searched for had the same code line but none had the problem i do.
I have already tried every possible way for removing this bug but all failed. The problem is that sometimes my player passes through the map tiles, i mean, the algorithm is like -- if not colliding, move in required direction; else if colliding, move in opposite direction. But sometimes the second condition fails i dont understand why.
Alright, so I'm having problems with lines 11 - 55. I'm trying to calculate the possible tiles the clicked unit can be moved on and display it to the player.
The field is a 9 (columns) by 5 (rows) grid. A unit with can move tiles horizontally or vertically based on its movement.
Example: (unit = o; possible tiles = x) (assuming movement of 2).
I tried to calculate movement by finding out what column the unit is in, subtracting the column it wants to go to, multiplying the difference by *-1 if the difference is less than 0. Then, doing the same with rows and adding the difference of columns to the difference of rows and comparing it to the unit's movement. Unfortunately, with my code, it seems to move in any directions.
@Override public void mouseClicked(MouseEvent e) { if (Main.cards.size() > 0) { mainloop: for (int i = 0; i < Main.cards.size(); i++) { Card c = (Card) Main.cards.get(i); int startingColumn = 0;
Displaying the records in a table. I am looking for a group header to be placed above each printed table for its related category and subcategory, as well as, the no. of the records for each table to be shown at the top of the table.
The table contains columns for category, subcategory, name. ex:
Category Subcategory Name
CON Retail AAA
CON Wholesale BBB
SPEC Retail CCC
What I am looking for is the below layout:
Category/Subcategory (No. of records) –similar to a group header
Name – Country ..etc ----Table header
table records
Here is my code below:
<% //Retrieve the values from the DB while (rs.next()) { category_name1=rs.getString("category_name"); subcategory_name1=rs.getString("subcategory_name");
[code]....
The problem in the above code is that it is showing the group headers category & sub category multiple times and the count is incorrect.
import java.util.Scanner; public class AvgLrgSml{ public static void main(String[]args){ System.out.print("Hello there. Please enter any three numbers."); Scanner keyboard = new Scanner(System.in); double num1 = keyboard.nextDouble(); double num2 = keyboard.nextDouble();
How do you make it so that you can select 2 buttons in a radio button group? for example: If I have 7 radio buttons, and I want to be able to select 2 of the 7 instead of 1.
I am making an expert system using Jess about animals. I wanted to make an interface using Swing and so I did. I have a problem using group layouts. The application works fine but at the end a exceptions is thrown:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0, alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=, preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING, horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=, text=The animal is a cheetah.,verticalAlignment=CENTER, verticalTextPosition=CENTER] is not attached to a horizontal group
[Code] ....
The application ask the user some questions about the animal. When the expert system has enough information to know the animal it tells the name of the animal and shows a picture of it. The exception is thrown when the application has guessed the animal and shows the response.
I have to organize the rows from the lower to the higher number, the problem is, what I have only organizes the first 3 rows even If I insert 3,4,5...or 10 rows.
for (int i = arg[0].length - 1; i >= 0; i--) { for (int j = 0; j < i ; j++) { for (int k = 0; k < i ; k++) { if (arg[k][j] > arg[k][j + 1]) { int temp = arg[k][j];