Loading...
Logo
Processing Forum

1st person perspective

in Programming Questions  •  2 years ago  
I've been trying to implement a first person perspective using the mouse to rotate the camera and WASD to move around using the pushMatrix function but I'm stuck at this point. It's probably poorly programmed but I'm waiting for a positive result before optimisation..

Copy code
  1. void setup() {
  2.   size(640, 360, P3D); 
  3.   background(0);
  4.  }
  5. int space=70;
  6. int hormove=0;
  7. float fev=0;
  8. float xpos=-500;
  9. float zpos=0;
  10. void draw() {
  11.   lights();
  12.   background(0);
  13.   pushMatrix();
  14.   if(keyPressed) {
  15.     if (key == 's' || key == 'S') {
  16.       xpos-=4;
  17.     }
  18.     if (key == 'a' || key == 'A') {
  19.       zpos+=4;
  20.     }
  21.     if (key == 'w' || key == 'W') {
  22.       xpos+=4;
  23.     }
  24.     if (key == 'D' || key == 'd') {
  25.       zpos-=4;
  26.     } }
  27.   if(mouseX<(width/2)-200) {
  28.     hormove-=1;
  29.   }
  30.   if(mouseX>(width/2)+200) {
  31.     hormove+=1;
  32.   }
  33.   translate(zpos,0,xpos);
  34.   rotateY(radians(hormove));
  35.   for(float x=0;x<width;x+=space) {
  36.     for(float z=0;z<(200);z+=space) {
  37.       for(float y=0;y<height+1;y+=height) {
  38.         pushMatrix();
  39.         translate(x,y,z);
  40.         noStroke();
  41.         fill(255);
  42.         box(space);
  43.         popMatrix();
  44.       }}}
  45.   popMatrix();
  46.   println(xpos);
  47. }
Any info on how to proceed would be greatly appreciated.

Replies(10)

What you are doing here is moving the world around a static camera. The better way is to move the camera then draw your objects.

The trick here is to use the camera() function at the beginning of your draw method. This method has 9 parameters which might seem a bit daunting but are easy to understand.

camera( eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ)

eyeX, eyeY, eyeZ represents the position of the camera and
centerX, centerY, centerZ represents the location the camera is looking at and
upX, upY, upZ are the camera's up direction and are normally 0,1,0

To rotate the camera you calculate new values for centerX, centerY, centerZ
To move the camera you calculate new values for eyeX, eyeY, eyeZ

To move the camera without introducing unwanted rotations due to the movement then changes in the position of the camera should also be applied to eye and center X/Y/Z values. If you only change eye then movement will introduce some unwanted rotation.

In draw()
  1. calculate the new values for the eye and center values based on key presses and mouse position
  2. use the camera() method to position the camera
  3. pushMatrix
  4. then translate to the box position say bx, by, bz
  5. use fill stroke etc to colour the box
  6. call box(size_of_box)
  7. popMatrix
  8. repeat setps 3-7 for each box you want
It may take a while to get this right but the effort is well worth it if you want to program 3D

This sure takes a while..
Just to make sure what i think is right,

In first person, the camera is not on the position of the player but behind right?
Like below, green is the player (should be wider maybe). The black thick line is the width of the computer screen, and the other thing is the camera.


So if you rotate 90 degrees, the camera not only rotate 90 degrees but moves to a new position based on the radius and the angle that is rotated right?



Plz let me know if i'm correct with this.
Correct and going back to the command

camera( eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ)

then centerXYZ is the point of rotation (near position of the player) and eyeXYZ is the camera position.

Any possibility of getting some example code of this? I'm having difficulty implementing a first person perspective using camera() for WASD movement and using the mouse to look. 
I dropped, i got a gimbal lock problem (do a search on this forum and you find more about it).
It's ten times harder then expected, and if you ever succeed then it would be nice if your the one that drops the example :)

Maybe you can find something here:

JavaGaming.org


Thank you both for your nice comments!

Chris


Hello,

I think 1st Person game means, the camera IS the player and you can't see the player / avatar (Doom / Quake...). 3rd Person Game means you see the avatar (Lara Croft in Tob Raider).

I now rewritten both programs 1st Person Game and 3rd Person Game I've posted here before and posted them both on openprocessing, see below.

They don’t work in the browser because they use Robot (to keep the mouse centered). So please download and use offline. Change the size-command then.

The use of Robot makes it possibly to endlessly look around. Thanks to rbauer. 

I made a small level as a test field.

Person runs around in the level.
Runs best with OPENGL - P3D has to many issues. 
Use mouse to look around.  
Use WASD to move. You run where you look at.
Use r/f for your height (or b/n for FAST height change).
Use c for crouch on/off.

1st Person (avatar can not be seen): http://openprocessing.org/visuals/?visualID=25255

3rd Person (avatar can be seen): http://openprocessing.org/visuals/?visualID=25245

Greetings!

Chris


I don't have time to look at it now but i really liked where you working on.
The movement was only really slow, look at this:

try 'w' and try 'e' , with 'e' you move much smoother and faster cause it's handled in draw.

Copy code
  1. int y1 = 500;
  2. int y2 = 500;

  3. void setup() {
  4.   size(600, 600);
  5. }

  6. void draw() {
  7.   background(255);
  8.   
  9.   if(keyPressed){
  10.     if(key == 'e'){
  11.       y2--;
  12.     }  
  13.   }
  14.   
  15.   rect(10, y1, 5, 5);
  16.   rect(50, y2, 5, 5);
  17.   
  18. }

  19. void keyPressed() {
  20.   if(key == 'w') {
  21.     y1--;
  22.   }
  23. }