Problem rotating camera using quaternions

Discussion in 'Programming' started by angelo234, Feb 2, 2021.

  1. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    I'm trying to make a pseudo backup camera (basically placing freeroam camera behind the vehicle and pointing downwards). It sort of works but the camera seems to rotate really weirdly and I'm not sure what I'm doing wrong. When I draw the vector out before converting it back to a quaternion, it seems to be pointing exactly where I want it no matter the orientation of the car. But after conversion, it doesn't work really well, especially when the roll and pitch values aren't zero.

    Here's a video of the problem and thanks in advance for the help! :)



    Code:
    local veh = be:getPlayerVehicle(0)
    if veh == nil then return end
    
    local veh_vel = vec3(veh.obj:getVelocity())
      
    local carPos = vec3(veh:getSpawnWorldOOBBRearPoint())
    local carDir = vec3(veh.obj:getDirectionVector()):normalized()
    local carDirUp = vec3(veh.obj:getDirectionVectorUp()):normalized()
    local carDirRight = carDir:cross(carDirUp):normalized()
      
    --Fixes lag of carPos
    carPos = carPos + veh_vel * dt
      
    --Set reverse cam little bit higher
    local camPos = carPos + vec3(0,0,0.25)
    
    --Rotate camera around car's right direction vector (cross product between fwd dir and up dir) 45 degrees downwards
    local quatRot = quatFromAxisAngle(carDirRight, -reverse_cam_downwards_angle * math.pi / 180.0)
    
    local camRot = quatRot * -carDir
      
    --Draws direction vector of where cam faces
    debugDrawer:drawLine((camPos):toPoint3F(), (camPos + camRot):toPoint3F(), ColorF(0,1,0,1)) 
    debugDrawer:setLastZTest(false)
      
    camRot = quatFromDir(camRot, carDirUp)
      
    --Set camera to rear of car
    setCameraPosRot(camPos.x,camPos.y,camPos.z,camRot.x,camRot.y,camRot.z,camRot.w)
     
    • Like Like x 1
  2. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    If no one knows why, I think it's because it doesn't allow you to apply a roll on the camera.
     
  3. atv_123

    atv_123
    Expand Collapse

    Joined:
    Aug 5, 2012
    Messages:
    1,710
    I feel like this has to be possible though... Like... take whatever cam position the 4 key is. That particular camera always stays locked to the orientation of the vehicle no matter what. So I feel like the same should be doable for this. Is it possible you could just change your script to switch camera types first to that camera type, then reposition it? You already seem to have the repositioning bit nailed down. Then when you pop out of reverse, you can just swap it back (hold the previous camera type in a variable perhaps to remember what it was)

    Caution.... I am not a programmer (in LUA anyways) at all... so I don't actually know if this is possible.
     
  4. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Yeah I tried using the relative camera mode but I can't seem to position the camera and rotate it using commands. But I think I could just make a custom camera mode using some of the relative camera code to make it work.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice