Hello I need a lot of help, I have been stuck trying to figure out a way to generate a 2d maze for quite some time, i opted to using griffpatch’s scratch video as a guide but honestly I don’t know how to apply that to monogame. I have done the basic udemy course and understand basics but I am struggling so much, especially with wall collisions, with my player getting stuck, the walls not generating and not knowing how to go about this at all. This is required work for my exam, not doing it would cost me serious grades please help, any videos or just a guide would be extremely useful as I have been stuck on this problem for months
Most 2D games use a tile system. Each tile has its own local and world coordinates. For example, the local position of the tile is X and Y, and the world coordinates are the local coordinates multiplied by the tile size. Knowing the World coordinates, you can generate a rectangle for each tile. And that’s where the magic begins, since the rectangle from the Xna namespace has enough methods and functions through which you can calculate:
- whether the character crosses the wall
- how deep the character has penetrated into the wall and how much you need to “push” the character.
Thanks to these functions, you can easily implement the primitive physics of the game.
Is this Ai? If so, why? What’s the point… like it takes 2 seconds to instantly see that “unique number” won’t be unique, will it?
Why would collision in uniform grid have to iterate through all tiles? Position in uniform grid is always O(1) test.
Can we have some rules against this? At best this will just confuse users. How is this code even relevant to original question?
Anyway to OP: for simple collision system in maze you can do following: before you apply movement in desired direction, check if tile in that direction is collidable, if so, simply don’t apply movement, hence you wont get to state where you would be stuck in wall at all. Is your movement smooth or just tile by tile?
This poor guy said he has been stuck on this for months.
Make a toy project guy a simple 5 x 5 array
1,1,1,0,1
1,0,0,0,1
1,0,1,1,1
1,0,0,0,1
1,1,1,0,1
This is your maze outline right now considered or imagine the following.
Each block has a width of 10 that you will later draw onto your screen.
a 1 in your array is a block a zero is empty space.
examine row 3 for example and consider the formula
int X_Array_Index = (int)(Player.Pos.X / 10f);
lets say the player x position is 10.5f then we get 10.5 /10 = 1
this is the index in the array.
now
lets say the player is moving left and up by -.707 -707 a distance of 1 and we will practically say this is the frame pixel speed.
In our update you push up left and this is the resulting velocity it will be added to the position.
now collision in this example is a check BEFORE you apply the motion which IS the change of position to see if the move is legal to say it is illegal for you to place the player in a block.
we have the index we know how to calculate were the player will be if he moves in relation to the block array and of course you know how to check if there is a 1 in a array at a index.
lets check to see if a move is legal in the X direction 10.5f + -.707f = is about 9.79
9.79 / 10 then changed to a integer will be 0 what is at index [3][0] a 1 were you have a block. This move is illegal
At this point you decide how to handle collisions because you know if you make this move your character would enter the wall the simplest way is to simply change the motion in the X direction to zero and apply the X motion and repeat this process for Y
In this way your map has a width of 10 the empty spaces have a width of 10 and this is all achieved by dividing simply by 10
When you draw your map each index starting position X or Y is multiplyed by … yep 10 each blocks width and height is 10.
you can draw your map from the array were each index draws a block sized 10 like so
for(y =0; y < mapHeight ; y ++){ // 5
for(x =0; x < mapWidth ; x ++){ // 5
spritebatch draw (… new Rectangle(x * blockWidth, y *blockHeight, blockWidth,blockHeight) …)
}
}
you can draw your player were he is as a vector position.
you might say ya but my player is like 5 in width and height so he can partially go into walls for that just change your calculation to subtract a extra 5 from your players position when moving left or add it when moving right basically you want a method to do this with temporary variables that copy your positions and velocitys add or subtract them as needed and ultimately just returns a true or false out of all of it. Which represents is moving legal or will it cause a collision.
This is the simplest explination of the concept i can think of hope it helps.
movement is smooth
When updating position, check whether resulting tile position allows free movement, if so, then actually update position, otherwise don’t.
Vector2 newPosition = position + velocity * elapsed;
if(IsTileWalkable(newPosition))
{
position = newPosition;
}