I am currently working on a Minecraft type game with OpenGL and C ++. I ended up abstracting a Camera object and configuring motion and even defining basic gravity as simulations, but I'm stuck on collision detection and resolution.

The movement is defined by a function which adds a speed "x" and "z" depending on the key pressed. I finally have a speed vector that can look like `(-2.0, 0.0, 2.5)`

. The speed vector is then added to the position vector of the camera in a Move function. Then I look for collisions and try to resolve them (currently only with the y-axis but even that doesn't work). The Move function code looks like:

```
void Camera::Move(const std::vector& cubePositions)
{
glm::vec3 nPos = glm::vec3((int)(round(Position.x)), (int)(round(Position.y)), (int)(round(Position.z)));
Position += Velocity;
// Collision resolution
for (float i = -2.0f; i <= 2.0f; i++)
{
for (float j = -2.0f; j <= 2.0f; j++)
{
for (float k = -2.0f; k <= 2.0f; k++)
{
if (std::find(cubePositions.begin(), cubePositions.end(), glm::vec3(nPos.x - i, nPos.y - j, nPos.z - k)) == cubePositions.end())
continue;
glm::vec3 box = glm::vec3(nPos.x - i, nPos.y - j, nPos.z - k);
if
(((Position.x - 0.5f <= box.x + 0.5f) || (Position.x + 0.5f >= box.x - 0.5f)) &&
((Position.y - 2.0f <= box.y + 0.5f) || (Position.y + 0.5f >= box.y - 0.5f)) &&
((Position.z - 0.5f <= box.z + 0.5f) || (Position.z + 0.5f >= box.z - 0.5f)))
{
LOG("Collision check with box " << box.x << " " << box.y << " " << box.z);
Position.y -= Velocity.y;
LOG("Position.y: " << Position.y);
}
}
}
}
LOG("Position: " << Position.x << " " << Position.y << " " << Position.z);
Velocity = glm::vec3(0.0f);
}
```

(LOG is just a macro that prints on the screen). At the moment, when a collision with a block is detected, it causes this type of nervous movement. Here is the output:

```
...
(LOG) Position: 2.10997 -12.1245 3.19591
(LOG) Position: 2.10997 -12.1899 3.19591
(LOG) Position: 2.10997 -12.2546 3.19591
(LOG) Position: 2.10997 -12.3261 3.19591
(LOG) Position: 2.10997 -12.3952 3.19591
(LOG) Position: 2.10997 -12.4618 3.19591
(LOG) Position: 2.10997 -12.5288 3.19591
(LOG) Collision check with box 4 -15 5
(LOG) Position.y: -12.5288
(LOG) Collision check with box 4 -15 4
(LOG) Position.y: -12.4595
(LOG) Collision check with box 4 -15 3
(LOG) Position.y: -12.3903
(LOG) Collision check with box 4 -15 2
(LOG) Position.y: -12.321
(LOG) Collision check with box 4 -15 1
(LOG) Position.y: -12.2518
(LOG) Collision check with box 3 -15 5
(LOG) Position.y: -12.1825
(LOG) Collision check with box 3 -15 4
(LOG) Position.y: -12.1133
(LOG) Collision check with box 3 -15 3
(LOG) Position.y: -12.044
(LOG) Collision check with box 3 -15 2
(LOG) Position.y: -11.9748
(LOG) Collision check with box 3 -15 1
(LOG) Position.y: -11.9055
(LOG) Collision check with box 2 -15 5
(LOG) Position.y: -11.8363
(LOG) Collision check with box 2 -15 4
(LOG) Position.y: -11.767
(LOG) Collision check with box 2 -15 3
(LOG) Position.y: -11.6978
(LOG) Collision check with box 2 -15 2
(LOG) Position.y: -11.6285
(LOG) Collision check with box 2 -15 1
(LOG) Position.y: -11.5593
(LOG) Collision check with box 1 -15 5
(LOG) Position.y: -11.49
(LOG) Collision check with box 1 -15 4
(LOG) Position.y: -11.4208
(LOG) Collision check with box 1 -15 3
(LOG) Position.y: -11.3515
(LOG) Collision check with box 1 -15 2
(LOG) Position.y: -11.2823
(LOG) Collision check with box 1 -15 1
(LOG) Position.y: -11.213
(LOG) Collision check with box 0 -15 5
(LOG) Position.y: -11.1437
(LOG) Collision check with box 0 -15 4
(LOG) Position.y: -11.0745
(LOG) Collision check with box 0 -15 3
(LOG) Position.y: -11.0052
(LOG) Collision check with box 0 -15 2
(LOG) Position.y: -10.936
(LOG) Collision check with box 0 -15 1
(LOG) Position.y: -10.8667
(LOG) Position: 2.10997 -10.8667 3.19591
(LOG) Position: 2.10997 -10.9176 3.19591
...
```

(here, when I say with the box x y z, x y z refers to the coordinates of the box, which are only transformations applied to a defined set of vertices when each box is drawn)

I've been working on it for a while and I've been looking for a solution for a while, but I haven't found one. I appreciate any help 🙂

Edit: I changed the if condition since I discovered it, *false*. But the rebound movement still exists, but on a smaller scale.

The correct if condition:

```
if
(((Position.x - 0.5f <= box.x + 0.5f) && (Position.x + 0.5f >= box.x - 0.5f)) &&
((Position.y - 2.0f <= box.y + 0.5f) && (Position.y + 0.5f >= box.y - 0.5f)) &&
((Position.z - 0.5f <= box.z + 0.5f) && (Position.z + 0.5f >= box.z - 0.5f)))
```

The result:

```
...
(LOG) Position: 5.43324 -12.5657 6.24592
(LOG) Collision check with box 6 -15 7
(LOG) Position.y: -12.5657
(LOG) Collision check with box 6 -15 6
(LOG) Position.y: -12.5014
(LOG) Collision check with box 5 -15 7
(LOG) Position.y: -12.4371
(LOG) Position: 5.43324 -12.4371 6.24592
(LOG) Position: 5.43324 -12.5011 6.24592
(LOG) Collision check with box 6 -15 7
(LOG) Position.y: -12.5011
(LOG) Collision check with box 6 -15 6
(LOG) Position.y: -12.4362
(LOG) Position: 5.43324 -12.4362 6.24592
(LOG) Position: 5.43324 -12.5246 6.24592
(LOG) Collision check with box 6 -15 7
(LOG) Position.y: -12.5246
(LOG) Collision check with box 6 -15 6
(LOG) Position.y: -12.4312
(LOG) Position: 5.43324 -12.4312 6.24592
...
```