Hi, I'm trying to make a simple flight simulator, I've managed to make it move in the desired direction (according to rotation), but I'm having major issues with the tracking camera.
My first idea was to save a vector of previous ship position and place the camera there looking at the ship itself, but it work
When I started to experiment I've discovered that I can't actually make the camera look at the position I want at all. I present you the problematic code:
Controls:
w/s - forward/backward
up/down - pitch
left/right - roll/yaw (according to current pitch)
Quote://Main draw function
void draw()
{
lights();
background(0);
PVector sPos = p.getPos().get();
sPos.div(p.speed);
camera(-200, 200, 400, //
sPos.x, sPos.y, sPos.z, //
0.0, 1.0, 0.0); // upX, upY, upZ*/
p.getRot().z += rz*2;
p.getRot().x += rx*2;
drawModel(); // drawing space reference model
if(fwd != 0) {
p.move(fwd);
}
p.draw();
}
Quote://Ship draw and move functions
public void draw() {
pushMatrix();
resetMatrix(); //apparently OpenGL sets a default trans matrix which has to be reset everytime
translate(pos.x, pos.y, pos.z); //we move the ship to previously saved position
rotateZ(radians(rot.z)); //rotating it as needed
rotateX(radians(rot.x));
translate(0, 0, delta); //moving forward by delta (local Z-axis)
delta = 0; //reseting delta so that we don't move if there's no button push
prevpos.x = pos.x; prevpos.y = pos.y; prevpos.z = pos.z;
//the magic
PMatrix3D p = (PMatrix3D)getMatrix(); //apparently you can get the transform matrix
//functionality which is NOT documented in reference!
pos.x = p.m03; //getting the right row of the matrix (which is translation) and put it in pos
pos.y = p.m13;
pos.z = p.m23;
//end of magic
//this is the vector I want to use to position the camera behind the ship
PVector tBack = new PVector(prevpos.x - pos.x, prevpos.y -pos.y, prevpos.z - pos.z);
if(tBack.mag() >= 2.5) {
backVec = tBack;
}
fill(c); //building the ship model
box(70, 8, 20);
box(20, 10, 50);
translate(0, -10, 20);
box(10, 30, 10);
popMatrix();
}
//this function is called every time forward/backward button is pressed
public void move(int f) {
delta += f*speed;
}
you can find the sources
hereAnd thank you for your help