We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › 3rd person view camera implementation problem
Page Index Toggle Pages: 1
3rd person view camera implementation problem (Read 2045 times)
3rd person view camera implementation problem
Oct 14th, 2009, 7:55am
 
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 Sad 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 here

And thank you for your help Cheesy
Re: 3rd person view camera implementation problem
Reply #1 - Oct 14th, 2009, 8:42am
 
Do you want the ship to stay in one place, as in the demo you linked?  If so, there's no need for fancy camera tracking...you have two things happening: your ship-model rotates as you turn, and your landscape shifts past accordingly.  You can get away with performing all the transformations on the model rather than the camera...
Re: 3rd person view camera implementation problem
Reply #2 - Oct 14th, 2009, 6:16pm
 
Do you mean I should move everything around except for the ship? That'd really complicate things for me. If you meant something else, please elaborate.
Re: 3rd person view camera implementation problem
Reply #3 - Oct 14th, 2009, 6:43pm
 
I prefer to shift the model rather than the camera, yes.  I'm not sure what you want to see when you move your ship...the demo you linked would most likely be viewed as a rotating model staying fixed in a shifting landscape.  The camera position in that case would just determine how closely one "followed" the ship...(how big it appeared, really)

But if you don't want to see the ship turn, you could just project a vector backwards from its current xy- and zy- angles and call that the camera position, right?

By the way, one problem that I would see coming up in that approach is that the camera would sometimes be put inside a wall / cliff...
Re: 3rd person view camera implementation problem
Reply #4 - Oct 14th, 2009, 11:11pm
 
maybe this is something for you http://users.design.ucla.edu/~ptierney/kaleidoscope/


kaleidoscope
a 3D First Person Shooter/Maya style camera for Processing
       

Re: 3rd person view camera implementation problem
Reply #5 - Oct 15th, 2009, 2:58am
 
Thanx for the replies and link.
The most urgent problem that I'm having at the moment is the fact the camera won't actually follow the ship, it sort of follows to a point and then the ship just runs off the screen. Any suggestions on that part?
Re: 3rd person view camera implementation problem
Reply #6 - Oct 15th, 2009, 4:14am
 
yes, that happens, the space is not endless. BenHem was right, it is much more usual to keep the ship where it is, and just rotate it, and move the space, land, street, whatever arround it.
that how this is done, even and especially in old scrolling games. You just have to draw what is onescreen, and dont have to take care of the rest.
Re: 3rd person view camera implementation problem
Reply #7 - Oct 15th, 2009, 8:34am
 
I meant that the ship runs off screen when you take hard right/left or up/down, not because it's far away but because the camera doesn't really follow it...
Re: 3rd person view camera implementation problem
Reply #8 - Oct 15th, 2009, 1:37pm
 
I am not sure if i understand. Are you making a 3d game, 2d game where you look at it from above ? but what you describes should be possible to handle somehow depending on what programm you got.
Maybe you can post an example.
Page Index Toggle Pages: 1