We're making a hexagon-shaped pipe-connecting puzzle game. Our code is based on Objects, and we are making the puzzle piece components by object functions we made. We don't have any physics yet either. Each pipe is able to move and rotate.
How can we write a function to check whether two or more pipes are connected?
What game engine do you use? I'd suggest keeping track of the pipe hexagons objects in a grid (a 2D array). Just imagine that every second row of the grid is shifted in by half the size of a hexagon). Then you can simply access all neighbors through that grid.
Eventually you need a function within the pipe tiles object like
bool canFlowTo(pipe_tile & other)
where you implement the right answer for each possible constellation of the pipe tile itself and the "other" tile.
To check where a pipe flows to, just call this function with all its neighbors in the grid.
Of course you can also approach this with a physics engine and not check correctness of the maze with code. For that just have colliders for each tile that have the right shape. For liquid physics you should definitely use some existing engine.
Oh and by the way, game questions that are not really related to a general C++ algorithm would probably fit better here: https://gamedev.stackexchange.com/
Related
I'm programming my first game and I have one last problem to solve. I need an algorithm to check if I can move a chosen ball to a chosen place.
Look at this picture:
The rule is, if I picked up the blue ball on the white background (in the very middle) I can move it to all the green spaces and I can't move it to the purple ones, cause they are sort of fenced by other balls. I naturally can't move it to the places taken by other balls. The ball can only move up, down, left and right.
Now I am aware that there is two already existing algorithms: A* and Dijkstra's algorithm that might be helpful, but they seem too complex for what I need (both using vectors or stuff that I weren't taught yet, I'm quite new to programming and this is my semester project). I don't need to find the shortest way, I just need to know whether the chosen destination place is fenced by other balls or not.
My board in the game is 9x9 array simply filled with '/' if it's an empty place or one of the 7 letters if it's taken.
Is there a way I can code the algorithm in a simple way?
[I went for the flood fill and it works just fine, thank you for all your help and if someone has a similar problem - I recommend using flood fill, it's really simple and quick]
I suggest using Flood fill algorithm:
Flood fill, also called seed fill, is an algorithm that determines the
area connected to a given node in a multi-dimensional array. It is
used in the "bucket" fill tool of paint programs to fill connected,
similarly-colored areas with a different color, and in games such as
Go and Minesweeper for determining which pieces are cleared. When
applied on an image to fill a particular bounded area with color, it
is also known as boundary fill.
In terms of complexity time, this algorithm will be equals the recursive one: O(N×M), where N and M are the dimensions of the input matrix. The key idea is that in both algorithms each node is processed at most once.
In this link you can find a guide to the implementation of the algorithm.
More specifically, as Martin Bonner suggested, there are some key concepts for the implementation:
Mark all empty cells as unknown (all full cells are unreachable)
Add the source cell to a set of routable cells
While the set is not empty:
pop an element from the set;
mark all adjacent unknown cells as "reachable" and add them to the set
All remaining unknown cells are unreachable.
PS: You may want to read Flood fill vs DFS.
You can do this very simply using the BFS(Breadth First Search) algorithm.
For this you need to study the Graph data structure. Its pretty simple to implement, once you understand it.
Key Idea
Your cells will act as vertices, whereas the edges will tell whether or not you will be able to move from one cell to another.
Once you have implemented you graph using an adjacency list or adjacency matrix representation, you are good to go and use the BFS algorithm to do what you are trying to do.
The title is a little vague, but I'll explain better here.
The Setup
I'm trying to program a little water level simulator, much like the water levels in the game, LIMBO. When an opening is made, allowing water to flow between two bodies of water, the levels equalize. The setup I have now is two containers, with blue blocks inside representing water levels. My mouse removes chunks of terrain away, and so, when an opening is made between the bodies, they should adjust and their Y values should move to match.
Image examples:
Semi-filled tanks:
Equalized tanks:
Now, I know some maths could be done to figure out how much to adjust the levels and the ratios between different sized tanks. That part I think is pretty straight forward. But I can't figure out a good method of determining if and when the two bodies of water are connected.
Any algorithm, pseudo-code, or references would be much appreciated!
If you need more clarification, please, don't hesitate to ask. I look forward to all feedback and will edit my post for specific clarification.
Thanks!
~ natebot13
BTW: I'm using C++, SFML, and Box2D (Box2D is for some other physics related things I need, not necessarily needed for this example).
You need to check whether the edge of the container1 is connected to container2 at any point of time if so then adjust the water level. I guess you are working on a image so you can use the connected components algorithm to check if any of the edge pixels of container1 is connected to any of edge pixels of container2 and also get their positions.
Algorithm :-
puts edges of container1 in one set which is connected to a dummy parent1.
puts edges of container2 in another set which is connected to another dummy parent2.
say after every one second add the new added pixels to sets using connected components
check at end of every union whether dummy parent1 and parent2 are connected.
You can use DFS to check the exacts points of connection by starting from one edge set1 and reaching the other. The last pixel and
the previous first pixel in edge set1 are connection end points.
Note:-
There is a implementation of disjoint set in c++ boost lib which might be useful in implementation of connected components.
I think you can start from left to right for example:
the right side of the left tank position is known.
search the column to its right for a blue square.
once found, search its surrounding squares - up, down and to the right.
keep searching until you get to the known left side of the right tank.
I work on a project which demands to use A* algorithm. In this project you select your player with the left click and you guide him in the map with the right click, like the gameplay of thousand strategy game. The graphics are 2D, a little like the game Don't Starve and the game is developped with SFML / C++.
I need to use A* for the deplacement of the player, indeed if an obstacle appears on his road he has to avoid it. But for the moment, i don't know how to apply a grid to the map, i want to place any tree / rocks and other stuff anywhere in order not to see the grid cells. For now the open list is only composed of pixels, which is not the good solution I think ^^, the algorithm is pretty slow. If you have any solution for a realistic rendering while keeping a fast algorithm I'd be happy to hear it. :)
Thank you in advance,
Do you have a screenshot?
The pathfinding grid, and rendering grid can be different. Zelda used different sized tiles for movement and rendering.
navigation mesh
This may be overkill for your map structure, but you may use a navigation mesh.
,
edit: If you haven't read it, Amit has a great resource: http://theory.stanford.edu/~amitp/GameProgramming/
What you're looking for is discretization. Behind this obscene name stands a simple principle : you can't deal with an infinite amount of data.
You need then to perform a transformation on your world : instead of allowing your character/unit to go at any location (x and y being real numbers), you can divide your world into some sort of a grid (this is what the navigation mesh and waypoints are doing), and only allow your char to go on these cells (or points, you can see it as you want). This is discretising : you are going from continuous values (real coordinates) to discrete values (integer coordinates / point). The more precise you go, the nicer it'll look.
After doing this, assigning moving costs between cells/points is rather simple, and performing A* on it as well.
I'm currently making a game in the DirectX engine in c++. I'm using path-finding to guide an army of soldiers to a specific location. the problem is that I use raycasts to see if there is nothing in the way of my path, and this slows down the speed of the game. Is there a better way to do pathfinding?
I also have a problem with the moving of my army. Right now i'm using the average of soldiers' positions as the start point, which means all the soldiers need to go there first before moving to the end point. Is there a way to make them go to the end point without going to the startpoint?
Thanks for the help.
Have you tried something like A-Star? to navigate via nodes, or some sort of 2d-array representation of your map? written good it could possible be faster aswell as easier to do with jobs ( multithreaded ).
if you have a solider, who is at postion A, and needs to get to B.
just calulate the path from C(the avrage position what ever) to B. get the direction from a to b and do some sort of interpolation. ( havent done this, or tried it, but it could probablt work out pretty well!)
Are you hit-testing every object when you are raycasting?
That can be very expensive when you have many objects and soldiers.
A common solution is to divide your world into square grid cells, and put each object in a list of objects for that grid.
Then you draw an imaginary line from the soldier to the destination and check each cell what objects you need to hit test against. This way you will evaluate only objects close to the straight path and ignore all others.
I have a 2d randomly genrated map for a platformer made of block(squares 40 by 40) stored in an array of 30, i have a push function to move the blocks around push changes the xpos aswell as the position on the array i'm only ever drawing the 5-25 position on the array.
When i move i'll only be moving the character within the first half of the screen. so there is collision between the middle part and the 0xpos of the screen now the problem i'm having is moving the blocks .
I cant think of a way to move them so it looks natural. Any ideas on how to do it? so far i have it so that every time the character collides with one side of the screen equivilant to 40 pixels worth of velocity it pushes a block and randomly genorates another.
Instead of trying to move all the blocks through an array it may be easier to use a standard queue.
http://www.cplusplus.com/reference/stl/queue/
With this method you would just have to deal with the movement of the blocks on the screen, and could remove the blocks from the front of the queue when they are no longer needed, adding another to the end.
This kind of effects are better achieved by using some physics engine. Look for example this one. Such engines really simplify the live in game developement, and the results always worth the effort of learning how to use them.