PDA

View Full Version : cockpit camera in flight simulator



davbwild
05-31-2011, 11:40 PM
Hi ,
could someone please give some step-by-step details to handle a cockpit camera in a flight simulator ??
I can't manage to get the right moves along yaw,roll,pitch
My parameter target in gluLookat function is wrong ...
Thank you very much for you help

BionicBytes
06-01-2011, 02:49 AM
Some one else is going to ask..so it may as well be me.
What is wrong with your code..can you describe the problem.
Post some code which handles the camera - use the [code ] [/code ] tags to make the code readable (don't include the extra space in the brackets though).

RefleX
06-01-2011, 04:11 AM
Post some code and brush up on your matrix maths while you wait for a reply.

Rotation in a flight game works different since you want the relative rotation from your current rotation.
NewRotationMat = CurrentRotationMat*RelativeRotationMat;
Hope that helps somewhat.

davbwild
06-01-2011, 04:49 AM
Ok thank you for your answer. I realized after posting that without code (I didn't mention that I work in Java (JOGL), it wouldn't mean a lot to the most helpful viewer
So here we are :
the main algorithm is quite basic :
GL_Renderer calls an Object Scene which draws the elements (the terrain and the plane)
here is the code of the call-back display() of GlRenderer:


public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
GLU glu = GLU.createGLU(gl);

// Clear the drawing area
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
// Reset the current matrix to the "identity"
gl.glLoadIdentity();

// on dessine la scene
myScene.draw(gl, glu);


Then the scene draw method sets the cockpit camera with the right eye and right target (depending on the plane position and attitude)


public void draw(GL2 gl, GLU glu) {

// TEST CAMERA TOUR
// test avion
myObj.setPosAtt(myMNT, myMNT.get_longUL()+myMNT.get_cellsize()*(1.1+step) , myMNT.get_latUL()+myMNT.get_cellsize()*(1.+step), 820.f, 30.f, 0.f, 0.f);


currentCamera.look(glu, gl, this.getAvion());


Then I draw the terrain (an height grid starting from (0,0,0) in the MODEL frame)
Then I draw the plane applying the moves according to:


public void display(GL2 gl, GLU glu) {

myVBO.bind(gl);

myIdx.bind(gl);

myTexture.bind(gl);

// gl.glLoadIdentity();

gl.glTranslated(posAvion.x, posAvion.y, posAvion.z );


gl.glRotated(getRoll(), 0.0, 0.0, 1.0);

gl.glRotated(getPitch(), 1.0, 0.0, 0.0);


gl.glRotated(getYaw(), 0.0, 1.0, 0.0);

avion.draw(gl);

// gl.glDrawElements(GL2.GL_TRIANGLES, myIdx.getCount(), GL2.GL_UNSIGNED_INT, 0);
}


Then I define how to set camera eye and target by :
eye= plane position
up is y axis with the roll applied
target direction is plane position vector plus the direction of yaw and pitch rotations applied the Z axis (my plane is along Z axis)



public void searchTarget(Obj plane, GL2 gl) {
gl.glPushMatrix();

gl.glRotated(plane.getRoll(), 0.0, 0.0, 1.0);
System.out.println("ok");
m1 = new float[16];
gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX, m1, 0);

//vecteur Up
vert = Vec4.multMatVec(m1, new Vec4(0.f, 1.f, 0.f, 1.f));
super.setUp(new Vec3(vert.x, vert.y, vert.z));




gl.glRotated(plane.getPitch(), 1.0, 0.0, 0.0);


gl.glRotated(plane.getYaw(), 0.0, 1.0, 0.0);
m2 = new float[16];
gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX, m2, 0);
// vecteur direction regard
dirEye = Vec4.multMatVec(m2, new Vec4(0.f, 0.f, 1.f, 1.f));
sum=Vec3.vec1Plusvec2(plane.getPosAvion(), new Vec3(dirEye.x, dirEye.y, dirEye.z));
// super.setTarget(Vec3.vec1Plusvec2(plane.getPosAvio n(), new Vec3(dirEye.x,dirEye.y,dirEye.z)));
super.setEye(new Vec3(radius*sum.x,radius*sum.y,radius*sum.z-6));

gl.glPopMatrix();

}


I hope it is clear enough
Thanks

RefleX
06-01-2011, 06:51 AM
Deprecated OpenGL :eek:

The language is not such a problem :) you just need to do the translation.


public void display(GL2 gl, GLU glu)
{
myVBO.bind(gl);
myIdx.bind(gl);
myTexture.bind(gl);

// gl.glLoadIdentity();

gl.glTranslated(posAvion.x, posAvion.y, posAvion.z );

// Here is the piece you need to change
gl.glPushMatrix();

// get(Roll/Pitch/Yaw)() should reflect how much you want to rotate the ship in a single frame not it's actual rotation (think of it as velocity/torque)
gl.glLoadMatrixf(avion.rotMat); // rotMat should be initialized as an identity matrix and is of type float[16]
gl.glRotated(getRoll(), 0.0, 0.0, 1.0);
gl.glRotated(getPitch(), 1.0, 0.0, 0.0);
gl.glRotated(getYaw(), 0.0, 1.0, 0.0);
gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX, avion.rotMat, 0);

gl.glPopMatrix();

gl.glMultMatrixf(avion.rotMat);
//

avion.draw(gl);

// gl.glDrawElements(GL2.GL_TRIANGLES, myIdx.getCount(), GL2.GL_UNSIGNED_INT, 0);
}

The code I replaced should work, you also need to change the following code...



public void searchTarget(Obj plane, GL2 gl) {
gl.glPushMatrix();

gl.glRotated(plane.getRoll(), 0.0, 0.0, 1.0);
System.out.println("ok");
m1 = new float[16];
gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX, m1, 0);

//vecteur Up
vert = Vec4.multMatVec(m1, new Vec4(0.f, 1.f, 0.f, 1.f));
super.setUp(new Vec3(vert.x, vert.y, vert.z));




gl.glRotated(plane.getPitch(), 1.0, 0.0, 0.0);


gl.glRotated(plane.getYaw(), 0.0, 1.0, 0.0);
m2 = new float[16];
gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX, m2, 0);
// vecteur direction regard
dirEye = Vec4.multMatVec(m2, new Vec4(0.f, 0.f, 1.f, 1.f));
sum=Vec3.vec1Plusvec2(plane.getPosAvion(), new Vec3(dirEye.x, dirEye.y, dirEye.z));
// super.setTarget(Vec3.vec1Plusvec2(plane.getPosAvio n(), new Vec3(dirEye.x,dirEye.y,dirEye.z)));
super.setEye(new Vec3(radius*sum.x,radius*sum.y,radius*sum.z-6));

gl.glPopMatrix();

}

This piece of code is troublesome and I can only help you out with the code I use in C++, it will be up to you to convert it to Java.


// mat.rot() - extract the rotation part of the matrix, returns a matrix
cVec3 eye = ship->mat*cVec3(0, -ship->model->mesh->radius*3.0f, ship->model->mesh->radius*1.0f);
cVec3 centre = ship->mat*cVec3(0, 1000000.0f, 0);
cVec3 up = ship->mat.rot()*cVec3(0, 1, 0);

cMat4 view = cMat4::LookAt(eye, centre, up);

As I said, up to you to translate.

davbwild
06-01-2011, 11:48 AM
Thank you very much for your help
I will try to change my code and let you know