Skip to main content
27 votes

Ways to draw tracks on 4-connected grid

This is Matt, the creator of Trainyard. @DMGregory summoned me here! :) It sounds like you're working through the same issues I had to deal with. It's a tricky problem where there isn't necessarily a ...
Matt Rix's user avatar
  • 371
9 votes
Accepted

Casting Civilization V based Hex Grid on Unity Terrain and Select Certain Areas of Grid

There's a useful isomorphism we can exploit between hex grids and square grids: with a simple shear transformation, we can transform one into the other. We'll use this in two ways: We'll use the ...
DMGregory's user avatar
  • 134k
8 votes

More efficient way to implement Line of sight on a 2d grid with ray casting?

I have implemented the algorithm suggested by Jimmy. Video of the code in action here: https://youtu.be/lIlPfwlcbHo ...
Stoiko's user avatar
  • 99
8 votes

Calculate the distance/number of moves one square is from another

If you don't need to navigate around obstacles along the way, you can do this with a simple formula. Just like in continuous space we can use the Euclidean distance metric: $$d_{\text{Euclidean}} = \...
DMGregory's user avatar
  • 134k
8 votes
Accepted

How can I procedurally find a wall that separates two or more points on a grid-based map?

I will present a general concept and three solutions using that concept. Concept is an Influence map: For each location in the map, you are going to store a number that represent the distance to each ...
Theraot's user avatar
  • 26.7k
7 votes

Elegantly transition from 4-grid to 6-grid and back

This is most intuitive way that I can think of.
Shadows In Rain's user avatar
7 votes

How does HPA*(Hierarchical Pathfinding A*) really work?

One of the creators of HAA* (a generalization of HPA*) wrote a very accessible article explaining how it works on aigamedev.com. Unfortunately that site seems to be dead, but fortunately archive.org ...
BlueRaja - Danny Pflughoeft's user avatar
6 votes
Accepted

Finding all hexagonal grid coordinates inside cover arc

I prefer to do hex-cell coordinate math in what Amit calls cubical coordinates on your linked page. For the rest of this answer, (X, Y, Z) will refer to cubical coordinates. Basically, for any of ...
Jimmy's user avatar
  • 9,019
6 votes
Accepted

What is the "Unity" way to approach tech design for a 2D game like this?

How do you approach Canvases in Unity? It's possible to use more than one screen-space canvas in Unity, but it's uncommon. There is usually no reason to do that, unless you want different UI elements ...
Philipp's user avatar
  • 119k
6 votes

Ways to draw tracks on 4-connected grid

I think you can solve the two pain points you indicated with a few heuristics. One is a little hysteresis: once the player is drawing in a particular row/column, keep their drawing cursor locked in ...
DMGregory's user avatar
  • 134k
6 votes
Accepted

Find a encircliement in Hexagonal grid (optimization issue)

1. Optimize Needlessly Slow Operations Your basic approach should work well on small to medium maps, it's just that the way you've chosen to implement it is unnecessarily slow: You search the same ...
DMGregory's user avatar
  • 134k
5 votes
Accepted

Representation of trains on a simplified rail grid

On tracking Your current simulation behaves pretty well, I would say. Because a rail wagon that is halfway a sharp bend, will indeed exhibit the behaviour that the wagon's center is no longer above ...
Bram's user avatar
  • 3,729
4 votes

place sprites in grid pattern within an area

first time here, have some time to kill so this is what I did. I'm assuming this is in a gui/2d game. This is what i did to get it to work. create a canvas and add 4 UI images. These act as the 4 ...
Carlos's user avatar
  • 41
4 votes
Accepted

Finding a path with minimal number of turns on a grid?

First fix your A* implementation so that it will find the shortest path. Adjust the cost of the turn instead of the heuristic. This means that when you generate the neighbours the one going straight ...
ratchet freak's user avatar
4 votes
Accepted

Find a path centered on the edge between traversable tiles

Your problem lies in generating the graph, before any actual pathfinding takes place. It looks like you want the intersections of tiles to be considered the nodes of your graph, and only want ...
BlueRaja - Danny Pflughoeft's user avatar
4 votes

What is the "Unity" way to approach tech design for a 2D game like this?

You would usually use the canvas for UI elements. You can anchor your elements relative to positions and it should take care of your scaling for different dimensions if done correctly. A gameobject ...
Zibelas's user avatar
  • 4,291
4 votes

How to handle two player-programmed robots attempting to enter the same discrete grid cell in one game tick

Some ways I've seen this solved: Treat a collision as a failure state Zachtronics games like SpaceChem and Opus Magnum commonly go this route. The player is challenged to create a program that does ...
DMGregory's user avatar
  • 134k
3 votes

How do I move a unit one grid-square-width per button press in Unity3D?

I think the best way to approach this would be to create a grid system where each square is 64x64 pixels, and then have your button press increment the square number. So the frog technically moves 64 ...
DisturbedNeo's user avatar
3 votes
Accepted

Calculate position on Isometric grid

Looks like you want: ...
DMGregory's user avatar
  • 134k
3 votes
Accepted

Diagonal symmetry across a filtered array?

You are writing to the same array you are reading from - that's what causes diagonal symmetry. Problem is you can't sample correctly if you have already begun to change the array you are sampling from!...
Engineer's user avatar
  • 29.5k
3 votes
Accepted

Messy results from simple cellular automata algorithm?

You require more effective smoothing methods. Google "cave generation" and you will find many. Also if you haven't already begun, some research on cellular automata will be beneficial. This issue ...
Engineer's user avatar
  • 29.5k
3 votes
Accepted

Creating random path in grid

Edit: I just noticed you don't have a fixed endpoint to steer towards. In this case, we can solve this with recursive backtracking: ...
DMGregory's user avatar
  • 134k
3 votes
Accepted

How should I store world data in a 2D grid sandbox game?

But here is the problem, since not every tile's size is 1x1, some may take 2x3 block size. and if i just put in Array this object's pointer, there is 6 same pointer will store in single array. Don't ...
Philipp's user avatar
  • 119k
3 votes
Accepted

How to decorate the floor with a grid?

One way to do a square pattern in glsl is with mod: ...
Theraot's user avatar
  • 26.7k
3 votes

How to handle grid snapping when the object is greater than the grid cell width and height?

The problem is, that you seem to take the center of the shape as pivot. What you want is to select a pivot, that places the shape inside the grid, so that it overlaps as few cells as possible. How to ...
Chillersanim's user avatar
3 votes
Accepted

Draw animating grid with terrain deformation and variable colors

When this question was just about drawing a plain grid of lines, I said... I'd be tempted to draw this with a shader on a single quad, using the world coordinates of the vertices to snap the lines to ...
DMGregory's user avatar
  • 134k
3 votes

Find a path centered on the edge between traversable tiles

If I understand correctly, you want the algorithm to return a path that touches the center of each grid (the red dots, in your last image) rather than one that goes along the grid lines (the blue line,...
Agent Tatsu's user avatar
3 votes
Accepted

Supercover line algorithm from corners instead of the square's center?

The solution was to use "A Fast Voxel Traversal Algorithm for Ray Tracing", as suggested by DMGregor, to trace a ray from/to "world" position instead of a grid cell's coordinate. I ...
Ainsley Harriott's user avatar
3 votes
Accepted

Holding "Shift" to lock mouse movements to a straight line?

Cache relevant x or y value after shift key is detected PseudoCode: ...
Acme Nerd Games's user avatar
3 votes
Accepted

How do I place objects so they snap to my TileMap in Godot?

Godot snaps to the grid guidelines and not the cells between them. So you won't snap to the middle of the square that you're dragging to. That's because you're snapping the position of the object (...
idbrii's user avatar
  • 798

Only top scored, non community-wiki answers of a minimum length are eligible