Hot answers tagged grid
36
Octogons:
Hexagons:
The gaps in the octogons make for an unappealing game world.
Typically, if you wanted to allow for eight directions of movement, you would just use squares.
16
A hexagonal ring with the radius of N consists of 6 straight lines, each with length N - see my extremely crude example below :) For N=2:
The arrows cover 2 hexes each.
I assume you have some functions which give you the neighbouring tile in a specific direction, like north(), southeast() etc. So your algorithm, in pseudocode, should be something like ...
9
You can generate the optimal path using A*, then distort it with midpoint displacement.
This will ensure your endpoints are met and allow you to control the randomness to a great degree. For example, I would not randomize roads as much as rivers. Whatever intelligence is building roads typically attempts to be optimal about it.
Take care to ensure that ...
9
Fascinating question. I think one of the first issues you have to address is whether you want the patrolling behavior to be "optimum" patrolling or "lifelike" patrolling. I'm just making up these words, but what I mean is:
Optimum: The agents move about in a manner that perfectly distributes their coverage area for the system as a whole.
Lifelike: The ...
8
Sounds like you want A-star path-finding, which is pretty much the defacto for path-finding. I used this in my XNA game: http://www.csharpcity.com/reusable-code/a-path-finding-library/ (I repackaged the library into a slightly more usable stand-alone DLL).
7
There are many ways to go depending on exactly how you want it. Here is a rough outline of one way which I think will fit your description:
First generate the canals. Then start placing houses randomly, for each house you place, place the road in front of it and draw a road from that piece to the existing road network, if either is not possible or placing ...
7
Such a bijective mapping is much easier to express if your tile indices and rows/columns are zero-based.
col: 0 1 2 3
[ 0 1 2 3 // row 0
4 5 6 7 // row 1
8 9 10 11 // row 2
12 13 14 15 // row 3
...
If you look at the column numbers, as the column number increases by one, so does the tile index. More particularly, there's ...
7
Basically what you want is a monohedral tesselation (or tiling), that is a coverage of the entire plane (assuming 2d) with a single shape where the tiles do neither overlap nor leave gaps.
There are lots of shapes with which this can be done but when we introduce other constraints, usually orientation should stay the same or they should conform to a ...
6
This is a form of the Packing Problem.
Here are your options:
Brute force it as Gajet has mentioned. This can be aided by doing a pre-evaluation of existing space in your world grid, so as to find maximal axis-aligned bounding boxes. This article should give you some insight into how one developer applied solutions to the Packing Problem, in regards to ...
6
You allocate Grid like this: Grid = new std::vector<int>[gridx, gridy];
C++ is not C#, this does not quite what you expect.
gridx, gridy is evaluated (thanks to the comma operator) to just gridy, so you are allocating an array of gridy std::vector<int>s.
The same thing happens when you try to access Grid[x,y], you are actually accessing ...
6
Before I answer the question you already asked, some notes:
You can use A* with the original grid system you are using. The key things you need are neighbors and distance (for the heuristic). For neighbors with your grid system, you need to do something different for even and odd columns (as you mention); here's how:
neighbors = [
[ [+1, +1], [+1, ...
5
I think your drawing is a little misleading because you choose to draw strokes from the point on the circle tangent to your moving direction. I can see that the collisions to your grid edges happends when the TOP and LEFT points of your circle touch an edge.
Let C be your center and r the radius so P' = C + (r,0) and P" = C + (0,r).
If D is your direction ...
5
Judging from a quick look at the libgdx wiki's SpriteBatch entry, alpha blending is on by default.
Blending is enabled by default. This means that when a texture is drawn, translucent portions of the texture are merged with pixels already on the screen at that location.
This means that you can do what you said: open the Hero texture in Paint .NET and ...
5
The parallelogram coordinates you're using are easier to work with, but they do have the drawback of being weird for rectangular maps. One approach is to store it with the offset coordinates but actually use parallelogram coordinates in your game logic.
Observation: in each row of the map, the grid data is contiguous. All the wasted space is on the left ...
5
It won't look good to you unless you tessellate the mesh more finely (use more triangles, use a bigger grid, and have the elevation changes go much slower)
Your normals are off a bit. To find smoothed normals at a vertex, you have to take several cross products at each vertex, sum the normals you find, then normalize that normal.
For example,
Here you ...
4
At first I thought you wanted to switch to equilateral triangles like below
Then you can index into this grid in the exact same way as a rectangular grid.
The top left triangle will be (0,0), and the one to the right of it will be (1,0) then to the right of that will be (2,0), etc.
The row below is (1, j).
However, I see now that you question is ...
4
Personally, I would prefer simplicity over saving memory. Don't optimize until needed!
If you're still bent on saving a few bytes, here's how you can do it:
Slice the parallelogram in half to form two right triangles
Rearrange the two triangles to form a rectangle.
(Note I added the green buffer strip so the math works out nicely.)
Python code to map ...
4
pseudo-code:
ClearAllCells();
foreach(entity)
int minXCoord = floor(entity->GetPosition().x-entity->GetRadius()) / CollisionGrid.CELL_SIZE;
int minYCoord = floor(entity->GetPosition().y-entity->GetRadius()) / CollisionGrid.CELL_SIZE;
int maxXCoord = ceil(entity->GetPosition().x+entity->GetRadius()) / CollisionGrid.CELL_SIZE;
int ...
4
Look up the concept of tile maps. Essentially these are just 2 dimensional arrays of ID's that relate to tiles in a sheet. If a tile is 40 x 40 pixels you can draw the tiles out and get a 2d top down map. In addition you can then make maps with an editor. Good starting point is here:
http://en.wikipedia.org/wiki/Tile_engine
4
One of the popular ways to do this, as Doorknob says in their comment, is Bresenham's line algorithm. The example image of which looks nearly identical to you requirement example:
It's commonly used to draw lines on the computer screen deciding which pixels to use to represent that line. In your case, you'll use it to decide which grid spaces to check.
...
3
The A* algorithm will also allow you to assign values to tiles indicating their suitability. For instance, you can assign the lowest cost scores to low land for rivers, to flat land (but not swamp) for roads, and generate based on that. This doesn't give you the shortest route, but it does give you the most efficient route. Apply a little randomness to your ...
3
I also built a hexagonal terrain for my game. Unfortunately I didn't get much further than that. Here's what it looked like:
Pretty nice, if you don't mind me saying. ;)
First things first: how do you generate a hexagon? Well, a hexagon is a form of polygon. Therefor its edges lie on a unit circle. So we can use sine and cosine to generate six points:
...
3
You can do something like this
int gridCubeWidth = 16, gridCubeHeight = 16;
cube.Position.X = Math.Floor(cube.Position.X / gridCubeWidth) * gridCubeWidth;
cube.Position.Y = Math.Floor(cube.Position.Y / gridCubeHeight) * gridCubeHeight;
This basically rounds the X and Y positions to the nearest multiple of the cube dimensions. Then scales it by the cube ...
3
Without more information here's the solution I propose you :
Iterate through all the enemies in the map and calculate the distance between each enemy and the player. If the distance is equal to 1, they are within range 1, equal to 2, within range 2.
If you have too many units and it is slowing you down. You need to divide your map into sections. And ...
3
If you want to use a perspective matrix for rendering, but an orthographic projection for retrieving mouse input, why not set up both?
// this example uses the free and open source OpenGL Mathematics library
// you can get it here: http://glm.g-truc.net/
glm::mat4x4 perspective = glm::perspective(90.f, 640.f / 480.f, 0.1f, 1000.f);
...
3
The diamond shape here has 2 useful properties:
A diamond that has a width of x fits completely within the square of width x
A diamond like this (ie. a square rotated through 90 degrees) based on a grid pattern can be defined by the Manhattan distance from the centre of the diamond.
So, an efficient check to see if a value is within this diamond would be ...
3
In general you'll be adapting existing pathfinding algorithms to be width-sensitive. Primarily that means adapting A*, though since your game is grid-based you may find the algorithm for solving fat mazes helpful.
For A*, the most basic solution is to simply add an assertion for width to your calculations, according to your movement rules. However, this ...
3
Sounds more like a you'd want a breadth first search, instead of a depth first like A*, a common one is Dijkstra's algorithm. It is typically used for path finding, but you don't need to have a goal in mind. You can almost use it as is, except you want to limit the depth to the number of moves the player has.
A gif from the wikipedia page shows how this ...
3
Just execute A* each and every step.
If this is too slow, think about optimising it. Then there are several approaches, for example jump-point-search makes the A* itself faster. You can also try and work out the circumstances when you need to recompute the path, and only doing so then.
3
Let's first define a new number. No worries, it's an easy one.
f: f × f = -3
Or, to put it simply: f = √3 × i, with i being the imaginary unit. With this, a rotation by 60 degrees clockwise is the same as multiplication by 1/2 × (1 - f), and rotation by 60 degrees counter-clockwise the same as multiplication by 1/2 × (1 + f). If this sounds strange, ...
Only top voted, non community-wiki answers of a minimum length are eligible