OpenGL : Camera collision - problem to stop camera

Viewed 166

I'm learning camera collision detection in OpenGl. I already managed to do a collision detection for AABB vs. AABB and for the camera with point vs. AABB. This is the detection for the camera:

   fun checkCameraQuadAABB(quad : Renderable?) : Boolean {

    val collisionX = camera.getPosition().x <= quad?.getPosition()?.x?.plus(maxX)!! && camera.getPosition().x >= quad?.getPosition()?.x?.plus(minX)
    val collisionY = camera.getPosition().y <= quad?.getPosition()?.y?.plus(maxY) && camera.getPosition().y >= quad?.getPosition()?.y?.plus(minY)
    val collisionZ = camera.getPosition().z <= quad?.getPosition()?.z?.plus(maxZ) && camera.getPosition().z >= quad?.getPosition()?.z?.plus(minZ)

    return collisionX && collisionY && collisionZ  }

This works o.k.

These are my camera movements for example:

fun moveCamera (dt: Float, t: Float) {

    this.dt = t - lastFrame
    lastFrame = t.toFloat()
    val s = 2.0f
    val cameraSpeed = dt * s

    camera.parent = null


    if (window.getKeyState(GLFW.GLFW_KEY_J) == true) {
            camera.translateLocal(Vector3f(-cameraSpeed.toFloat(), 0.0f, 0.0f)) }

    if (window.getKeyState(GLFW.GLFW_KEY_K) == true) {
            camera.translateLocal(Vector3f(0.0f, 0.0f, -cameraSpeed.toFloat())) }

    if (window.getKeyState(GLFW.GLFW_KEY_L) == true) {
           camera.translateLocal(Vector3f(cameraSpeed.toFloat(), 0.0f, 0.0f))  } }

I want to stop movement of the camera, when collision is detected, but I am not able to find a way. I tried to stop movement like this:

  if (window.getKeyState(GLFW.GLFW_KEY_K) == true) {
         if (collisionDetectionEarth(earth, wallModule1x1Renderable) == true) {
            camera.translateLocal(Vector3f(0.0f, 0.0f, 0.0f))
        } else {
            camera.translateLocal(Vector3f(0.0f, 0.0f, -cameraSpeed.toFloat()))
        }
    }

But this is obviously not working, because the camera stops for all directions. I need a solution with a method like this:

    fun doCollisionCamera() {
    //check single renderable oder list of renderables
    
    if(checkCameraQuadAABB (wallModule1x1Renderable) == true) {
        //TODO set velocity 0 in the current direction, but allow other directions
               }
    else {
        //TODO move on
    }
}

But I can't figure out how to solve this. How can I check the direction and set velocity(cameraspeed) only in this direction to 0?

0 Answers
Related