Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Camera and Cube Overlapping

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    5

    Camera and Cube Overlapping

    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:
    Code :
    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 ).
    Last edited by therealcain; 12-04-2017 at 07:42 AM.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,523
    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.
    Code :
    float distance = glm::length(glm::vec3((ViewMatrix * Model)[3]));

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    5
    Quote Originally Posted by GClements View Post
    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.
    Code :
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •