Detecting stuff in 3d is not as easy as in 2d. I have managed to detect the collision but I am stuck trying to get the correct collision response. It seems that there is no good information online, so I might aswell ask it here. Note im trying to check collision against a voxel.
Here is my collision function which 100% works
bool collision(glm::vec3 block_position)
{
int x = block_position.x;
int y = block_position.y;
int z = block_position.z;
auto x1 = camera.position.x > x - 0.5f && camera.position.x < x + 0.5f; // 0.5 half block size
auto y1 = camera.position.y > y - 0.5f && camera.position.y < y + 0.5f;
auto z1 = camera.position.z > z - 0.5f && camera.position.z < z + 0.5f;
return x1 && y1 && z1; // this function works
}
But my collision response function I have not really managed to put together. Can someone explain.
void collision_response(glm::vec3 block_position)
{
int x = block_position.x;
int y = block_position.y;
int z = block_position.z;
auto dx = camera.position.x - x;
auto dy = camera.position.y - y;
if(!dx && !dy)
{
camera.position.x = prevPos.x;
camera.position.y = prevPos.y;
return;
}
auto angle = abs(atan2(dy, dx) + PI / 4);
if (angle >= -1*(PI) / 2 && angle <= 0) camera.position.x = prevPos.x;
if (angle >= 0 && angle <= PI / 2) camera.position.y = prevPos.y;
}