Vehicle demo attaching camera problem.

Post Reply
rebirth
Posts: 24
Joined: Thu Jul 24, 2014 2:48 am

Vehicle demo attaching camera problem.

Post by rebirth »

Hi, I have the vehicle in Y-up coordinates, but when I get the quaternion from the rigidbody it's upside down and facing backwards. I can of course multiply with another quat to correct this, but I'm curious why this is happening and if there is anything else I can attach to for an FPS inside car cam. I notice the camera also bounces incorrectly when the car is rolling or crashing, but I guess this is just how the vehicle's transform works? I've tried getWorldTransform, getCenterOfMassTransform and the raycastvehicle's getChassisWorldTransform. All appear to return the same value.

I'm using OpenGL for rendering.

P.S. I'm not using the camera with the demo, because I'm using the gl core profile (3.3).

My camera's default up vector is (0, 1, 0) and the forward vector is (0, 0, -1). I set the lookat vector by creating a new forward vector (default forward * quaternion) and add that unit vector to world position. At the moment both the quaternion and the world position is coming from the rigidbody's transform.
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Vehicle demo attaching camera problem.

Post by Flix »

I think that's just a matter of conventions.

I've read on some other forum some time ago that the "correct" way to map all the bodies in OpenGL should be with Y up and -Z forward, since this convention can be used for both the camera and the other objects.

I don't remember what convention Bullet uses for its camera and bodies in the demos (if that does mean something...). Anyway I've always used Y up and +Z forward: I think this is a much more intuitive convention, even if this requires some tuning for the camera (you can't just grab the matrix of a body and apply it to the camera to get a first person camera view).

I think this is what you're experimenting: it's not a big deal, and you've already solved the problem... so there's not much to worry about :) .

[Edit :idea: ] I've found the following excerpt in (a version of) a Bullet demo I posted long time ago in the demo section (hope it can be useful :D).
I used Bullet btTransforms for everything (camera included) with the "normal" right-hand convention: X = left, Y = up, Z = forward.
To make this work I applied the following tweak to the camera matrix.

Code: Select all

btTransform m_cameraT;	//it's the Bullet transform used for the camera position and orientation with the "normal" right-hand convention: X = left, Y = up, Z = forward.
void GlutDemoApplicationEx::calculateAndSetModelviewMatrixFromCameraT()	{
        {
        // The "system" modelview matrix is the inverse of m_cameraT, BUT we must be coherent with the axis conventions...
        // If we choose +Z to be the "forward" axis, and +X the left axis (like we do), we must "spoil" the camera from its own convention,
        // because in openGL, if we assign an identity matrix to the camera, it looks in the -Z direction, and its +X direction
        // is "right", instead of left.
        // Many people force this convention (-Z = forward and +X = right) for all the objects, so they don't need to do this hack when
        // dealing with the camera, but I strongly advice you not to do so, as most models are modelled with the +Z axis as their
        // forward axis, and you'll end up rotating all the objects in the world, when you can just do some "dirty work" on the camera...
        //
        // P.S. For the ones that are still confused: don't worry, m_cameraT.getBasis().getColumn(2) is still the camera forward axis and that's
        // the important thing to remember.
        static btTransform m_cameraArrangedT;
        m_cameraArrangedT = m_cameraT;
        btMatrix3x3& m_cameraArrangedBasis = m_cameraArrangedT.getBasis();
        //invert the sign of m_cameraArrangedT.getBasis().getColumn(2);	// Z axis
        m_cameraArrangedBasis[0].setZ(- m_cameraArrangedBasis[0][2]);
        m_cameraArrangedBasis[1].setZ(- m_cameraArrangedBasis[1][2]);
        m_cameraArrangedBasis[2].setZ(- m_cameraArrangedBasis[2][2]);
        //invert the sign of m_cameraArrangedT.getBasis().getColumn(0);  // X axis
       	m_cameraArrangedBasis[0].setX( - m_cameraArrangedBasis[0][0]);
        m_cameraArrangedBasis[1].setX( - m_cameraArrangedBasis[1][0]);
        m_cameraArrangedBasis[2].setX( - m_cameraArrangedBasis[2][0]);
        // Here m_cameraArrangedT has been cleaned from the "camera convention"
        m_cameraArrangedT.inverse().getOpenGLMatrix(m_modelviewMatrix);	// very fast (see the implementation of btTransform::inverse())
        }

    glMatrixMode(GL_MODELVIEW);
	#ifdef BT_USE_DOUBLE_PRECISION
    glLoadMatrixd(m_modelviewMatrix);
    #else //BT_USE_DOUBLE_PRECISION
    glLoadMatrixf(m_modelviewMatrix);
	#endif //BT_USE_DOUBLE_PRECISION
}
rebirth
Posts: 24
Joined: Thu Jul 24, 2014 2:48 am

Re: Vehicle demo attaching camera problem.

Post by rebirth »

Thanks for the tips, but with what I already have making everything using Bullet's transforms and using that coordinate system would set me back weeks or even longer. I will certainly keep it in mind, though.

For now, I'm just using the quick fix and I fixed the bouncing wrongly problem I mentioned (it was an error on my part). The camera also has its own local coordinate system so it can be off-setted (it's how I'm fixing the rotation).

The only problem I'm having now is that the faster I go, the more further back my camera goes. It's a cool effect but not what I'm after. I haven't actually built the vehicle demo, but I think (just glancing at it) it uses a third person camera.
Post Reply