I am working on a 3D maze game where the labyrinth itself is generated automatically and randomly each time I run the program. So basically the walls are always somewhere else. This is where I need help to figure out how to detect collision thus not able the player to press the buttons to move since there's a wall. (3D actually doesn't really matter since it's seen from above and it works like a 2D maze now)
This is where I am so far:
My character is a simple cube:
struct Cube {
glm::vec3 position;
glm::vec3 size;
};
Below you can find how I make my character moveable. I could check whether there's the edge of the maze so you can't move thru those walls but inside the labyrinth, you still can. The walls are always 0.25 wide.
I'd like to implement a function like IsCollide but I don't really know how. When I'd call the function, the std::vector<Cube> maze
will contain the datas I'll show on the picture.
static void MoveCharacter(Cube &character, Keyboard keyboard, std::vector<Cube> maze)
{
if (keyboard.getKey(GLFW_KEY_UP))
{
if (character.position.x >= MazeHeight - 1.0f) || IsCollide(character, maze))
{
character.position += glm::vec3(0,0,0);
} else character.position += glm::vec3(0.05, 0, 0);
}
if (keyboard.getKey(GLFW_KEY_DOWN))
{
if (character.position.x <= 0.0f)
{
character.position += glm::vec3(0, 0, 0);
} else character.position -= glm::vec3(0.05, 0, 0);
}
if (keyboard.getKey(GLFW_KEY_RIGHT))
{
if (character.position.z >= MazeWidth - 1.0f)
{
character.position += glm::vec3(0, 0, 0);
} else character.position += glm::vec3(0, 0, 0.05);
}
//...
}
I tried to write my function like this somehow but I am pretty much lost in this and not sure how to go on...
static bool IsCollide(Cube character, std::vector<Cube> maze)
{
for (auto i : maze)
if ( (character.position.x < (i.position.x + i.size.x)) && (character.position.x + character.size.x > i.position.x)) // && (//...) && (//...)
// to implement!
{
return true;
} return false;
}
I'd be VERY happy for every answer and help!
Thank you so much in advance!
Keyboard
if possible). – Acorn