PDA

View Full Version : Camera and Cube Overlapping

therealcain
12-04-2017, 06:01 AM
Hello, I'm trying to create a function that check if my camera is touching one of the cubes ( array ).
I thought about doing distance between MVP / View Matrix and Model and if it's equal to "0 + scale" / "0 - scale" it's overlapping.
but somehow it doesn't work...

Camera Update Source:

void Camera::Update(glm::vec3 const& Cube_scale,
glm::vec3 const& Cube_position)
{
Camera::computeMatricesFromInputs();

glm::mat4 Model = glm::mat4();
Model = glm::scale(Model, Cube_scale);
Model = glm::translate(Model, Cube_position);
glm::mat4 mvp = ProjectionMatrix * ViewMatrix * Model;

glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &mvp[0][0]);

// Overlap Detection - will moved to function
float dist = Distance3D(Model[3][0], mvp[3][0], Model[3][1], mvp[3][1], Model[3][2], mvp[3][2]);
std::cout << dist << std::endl;
}

but the values aren't correct.
by the way, I've seen something about "extracting" translation from matrices, so i guess i need to do something with that, but not sure how.

can anyone direct me to the correct path ? or give me an example ? ( it would be great ).

GClements
12-04-2017, 09:32 AM
The vector from the eye to the centre of the cube is (ViewMatrix * Model)[3], or ViewMatrix*Model*vec4(0,0,0,1). So you just need the length of that vector. E.g.

float distance = glm::length(glm::vec3((ViewMatrix * Model)[3]));

therealcain
12-04-2017, 09:54 AM
The vector from the eye to the centre of the cube is (ViewMatrix * Model)[3], or ViewMatrix*Model*vec4(0,0,0,1). So you just need the length of that vector. E.g.

float distance = glm::length(glm::vec3((ViewMatrix * Model)[3]));

i have tested what you said with my project and it FINALLY worked!
i didn't know about glm::length, i really should spend some time in GLM library xD