Making a 3D table

i only mentioned the maze because it was in the example. I’m building my own first person shooter cam atm. Chrisir b4 you said using camera to do first person shooter wouldn’t work.

1 Like

It can be done I guess

How would I make the camera rotate? ATM I got the camera showing a first person shooter view but only looking frontwards. I need it to be able to turn left or right. I got my new idea for first person from your table idea.

sketch


ArrayList<Objects> objects = new ArrayList<Objects>();
ArrayList<Objects> floor = new ArrayList<Objects>();
ArrayList<Objects> wall = new ArrayList<Objects>();
Player player;


final color RED=color(255, 0, 0);
final color GREEN=color(0, 255, 0);
final color BLUE=color(0, 0, 255);
final color WHITE=color(255);

void setup() {
  size(600, 600, P3D);
  player = new Player();

  //  the table itself 
  objects.add(new Objects(0,-100,0,240,10,140, color(170)));

  // 4 legs
  objects.add(new Objects(100, -50, 50, 10, 100, 10, RED));
  objects.add(new Objects(-100, -50, 50, 10, 100, 10, BLUE));
  objects.add(new Objects(100, -50, -50, 10, 100, 10, GREEN));
  objects.add(new Objects(-100, -50, -50, 10, 100, 10, WHITE));

  //floor
  floor.add(new Objects(0, 0, 0, 1000, 3, 1000, color(200)));
  // wall
  objects.add(new Objects(-500,-50,0,10,100,1000, GREEN));
  objects.add(new Objects(500,-50,0,10,100,1000, GREEN));
}

void draw() {
  lights();
  background(55);
  camera(player.pos.x,player.pos.y,player.pos.z,player.pos.x+100, player.pos.y,player.pos.z,0,1,0);
  noStroke();
  player.show();
  player.move();
  textSize(20);
  for (Objects o : objects){
    o.show();
  }
  for (Objects o : floor){
    o.show();
  }
  text("L: " + player.left,player.pos.x,-10,player.pos.z+100);
  text("F: " + player.front,player.pos.x,-30,player.pos.z+100);
  text("B: " + player.back,player.pos.x,-50,player.pos.z+100);
  text("R: " + player.right,player.pos.x,-70,player.pos.z+100);
}

void keyPressed(){
  if (key == 'w'){player.xspd = 1;}
  if (key == 's'){player.xspd = -1;}
  if (key == 'a'){player.zspd = -1;}
  if (key == 'd'){player.zspd = 1;}
}
void keyReleased(){
  player.xspd = 0;
  player.zspd = 0;
}

objects

class Objects {

  PVector pos, bsize;
  color col;

  Objects(int x, int y, int z, 
    int b, int h, int t, 
    color col_) {
    pos = new PVector(x, y, z);
    bsize = new PVector(b, h, t);
    col=col_;
  }

  void show() {
    //3D
    pushMatrix();
    fill(col); 
    stroke(0); 
    translate(pos.x, pos.y, pos.z);
    box(bsize.x, bsize.y, bsize.z);
    popMatrix();
  }
}//class

Player

class Player{
  PVector pos;
  int xspd, zspd;
  boolean front,back,left,right;
  int r = 26;
  Player(){
    pos = new PVector(0,-100,-160);
  }
  
  void show(){
    pushMatrix();
    fill(255,0,0);
    translate(pos.x,pos.y,pos.z);
    sphere(r);
    popMatrix();
  }
  
  void move(){
    front = false;
    back = false;
    left = false;
    right = false;
    for (int i = 0; i < objects.size(); i++){
      Objects o = objects.get(i);
      if (pos.x + (r+5) <= o.pos.x + o.bsize.x/2 && pos.x + (r+5) >= o.pos.x - o.bsize.x/2 && 
          pos.z - r < o.pos.z + ((o.bsize.z/2)+5) && pos.z + r > o.pos.z - ((o.bsize.z/2)+5)){
        front = true;
      }
      if (pos.x - (r+5) >= o.pos.x - o.bsize.x/2 && pos.x - (r+5) <= o.pos.x + o.bsize.x/2 && 
          pos.z - r < o.pos.z + ((o.bsize.z/2)+5) && pos.z + r > o.pos.z - ((o.bsize.z/2)+5)){
        back = true;
      }
      if (pos.z - (r+5) >= o.pos.z - o.bsize.z/2 && pos.z - (r+5) <= o.pos.z + o.bsize.z/2 && 
          pos.x - r < o.pos.x + ((o.bsize.x/2)+5) && pos.x + r > o.pos.x - ((o.bsize.x/2)+5)){
        left = true;
      }
      if (pos.z + (r+5) <= o.pos.z + o.bsize.z/2 && pos.z + (r+5) >= o.pos.z - o.bsize.z/2 && 
          pos.x - r < o.pos.x + ((o.bsize.x/2)+5) && pos.x + r > o.pos.x - ((o.bsize.x/2)+5)){
        right = true;
      }
    }
    if (front == true && xspd == 1 || back == true && xspd == -1){xspd = 0;}
    if (left == true && zspd == -1 || right == true && zspd == 1){zspd = 0;}
    pos.x += xspd;
    pos.z += zspd;
  }
}

here is a function that yopu can call at the beginning of draw()

xpos, ypos, zpos is your

player.pos.x,player.pos.y,player.pos.z

Here is the function:

void setCameraAccordingToMouse () {
  // Mouse  
  float radius = 450.0;  // radius of circle 

  // command map: See Help. 
  angle = map(mouseX, width, 0, 0, 359); // left right 

  // look at 
  xlookat = radius*sin(radians(angle)) + xpos;            // look left / right, x look at
  ylookat = map(mouseY, 0, height, -1470, height); // look up / down 
  zlookat = radius*cos(radians(angle)) + zpos;            // look left / right, z look at

  camera (xpos, ypos, zpos, 
    xlookat, ylookat, zlookat, 
    0.0, 1.0, 0.0
    );
}

I was thinking only move to left or right when within 50 pixel of side. I saw in tutorial that coding train used heading, could I place in a heading then change the heading left or right and change the cam?

1 Like

Why not? Just try it

I am but i don’t know how to adjust the cam with the heading. What if I um… redid the camera view whenever I changed the heading like if heading = 45 camera view is something. Do you think that will work?

Sure. Just try it

Chrisir

I can show you my idea when you post your CURRENT entire code with the 50 pixel on the sides

I haven’t added the 50 pixel side yet the entire code to what i’m trying is up above. I’m trying new things then adding it if works.

1 Like

ok, then just try it

OK, according to my line that I drew to see what angel I need the angels are right, but when I try the cameras. they show the first and third one as right & the second and third as wrong.
sketch

import peasy.*;

Player player = new Player();
PeasyCam cam;
void setup(){
  size(600,600,P3D);
  cam = new PeasyCam(this,2000);
}

void draw(){
  background(0);
  noStroke();
  player.show();
  player.view();
  noStroke();
  pushMatrix();
  translate(100,300,0);
  fill(255,0,0);
  box(10,10,100);
  popMatrix();
  pushMatrix();
  fill(0,255,0);
  translate(500,300,0);
  box(10,10,100);
  popMatrix();
  pushMatrix();
  fill(0,0,255);
  translate(300,100,0);
  box(10,10,100);
  popMatrix();
  pushMatrix();
  fill(255,0,255);
  translate(300,500,0);
  box(10,10,100);
  popMatrix();
}

void keyPressed(){
  if (key == 'e'){player.heading += 1;}
  if (key == 'q'){player.heading -= 1;}
}

Player

class Player{
  PVector pos;
  int heading;
  int r = 24;
  Player(){
    pos = new PVector(300,300,0);
  }
  
  void show(){
    pushMatrix();
    fill(255);
    translate(pos.x,pos.y,pos.z);
    sphere(r);
    popMatrix();
  }
  
  void view(){
    stroke(0,255,255);
    if (heading == -1){heading = 3;}
    if (heading == 4){heading = 0;}
    if (heading == 0){
      //camera(player.pos.x,player.pos.y,player.pos.z,player.pos.x+100, player.pos.y,player.pos.z,0,1,0);
      line(player.pos.x,player.pos.y,player.pos.z,player.pos.x+100, player.pos.y,player.pos.z);
    }
    if (heading == 1){
      //camera(player.pos.x,player.pos.y,player.pos.z,player.pos.x, player.pos.y+100,player.pos.z,0,1,0);
      line(player.pos.x,player.pos.y,player.pos.z,player.pos.x, player.pos.y+100,player.pos.z);
    }
    if (heading == 2){
      //camera(player.pos.x,player.pos.y,player.pos.z,player.pos.x-100, player.pos.y,player.pos.z,0,1,0);
      line(player.pos.x,player.pos.y,player.pos.z,player.pos.x-100, player.pos.y,player.pos.z);
    }
    if (heading == 3){
      //camera(player.pos.x,player.pos.y,player.pos.z,player.pos.x, player.pos.y-100,player.pos.z,0,1,0);
      line(player.pos.x,player.pos.y,player.pos.z,player.pos.x, player.pos.y-100,player.pos.z);
    }
  }
}

the cameras are comment out so I can check if the angels are right. I can’t see the line for some reason with them on.

with this new version there is a field of 50 pixels left and right at the screen border that changes camera view when you move the mouse into it.

use wasd and mouse

(no libraries used)

// USING mouseXSpecial instead of Mouse


Player player = new Player();
ArrayList<Objects> objects = new ArrayList<Objects>();

final color RED=color(255, 0, 0);
final color GREEN=color(0, 255, 0);
final color BLUE=color(0, 0, 255);
final color WHITE=color(255);

// camera / where you are / where you look at
float xpos=60, ypos, zpos=300, 
  xlookat, ylookat=40, zlookat; 
float angle=0.0; // (angle left / right; 0..359 degree)

// player is crouching yes / no 
boolean isCrouching = false; 

// Floor / plane has y-value 
final float floorLevel = 500.0; 

float mouseXSpecial=-180; 
float mouseXSpecialSpeed=2; 

// ------------------------------

void setup() {
  size(600, 600, P3D);

  //  the table itself 
  objects.add(new Objects(200, 50, -200+50, 
    240, 10, 140, color(170)));

  // 4 legs
  objects.add(new Objects(200-100, 102, -200, 10, 100, 10, RED));
  objects.add(new Objects(200+100, 102, -200, 10, 100, 10, BLUE));
  objects.add(new Objects(200-100, 102, -200+100, 10, 100, 10, GREEN));
  objects.add(new Objects(200+100, 102, -200+100, 10, 100, 10, WHITE));

  //floor
  objects.add(new Objects(-1000, 102+50+2, -1000, 
    4000, 3, 4000, 
    color(200)));
}

void draw() {
  background(0);

  if (mouseY>0) { 
    setCameraAccordingToMouse();
  }
  camera (xpos, ypos, zpos, 
    xlookat, ylookat, zlookat, 
    0.0, 1.0, 0.0 );

  noStroke();
  player.show();
  player.view();
  noStroke();
  pushMatrix();
  translate(100, 300, 0);
  fill(255, 0, 0);
  box(10, 10, 100);
  popMatrix();
  pushMatrix();
  fill(0, 255, 0);
  translate(500, 300, 0);
  box(10, 10, 100);
  popMatrix();
  pushMatrix();
  fill(0, 0, 255);
  translate(300, 100, 0);
  box(10, 10, 100);
  popMatrix();
  pushMatrix();
  fill(255, 0, 255);
  translate(300, 500, 0);
  box(10, 10, 100);
  popMatrix();

  // ------------------------------

  if (keyPressed) 
    keyPressedForStateNormal(); 

  // ------------------------------

  for (Objects o : objects) 
    o.show();

  // ------------------------------

  if (mouseX<50) 
    mouseXSpecial-=mouseXSpecialSpeed;

  if (mouseX>width-50) 
    mouseXSpecial+=mouseXSpecialSpeed;

  // ------------------------------
}

//------------------------------------------------------

void keyPressedForStateNormal() {

  float Radius = 13; 

  if (!(key==CODED)) { 

    //
    // ----------------------------    
    // forward & backward
    if (key == 'w' || key == 'W') {
      // forward : should be running towards lookat 
      xpos =   Radius*sin(radians(angle)) + xpos;
      zpos =   Radius*cos(radians(angle)) + zpos;
    } else if (key == 's' || key == 'S') {
      // backward
      xpos =  xpos- (Radius*sin(radians(angle))) ;
      zpos =  zpos- (Radius*cos(radians(angle))) ;
    }
    // ----------------------------    
    // left & right 
    else if (key == 'a' || key == 'A') {
      // left
      xpos =   xpos- Radius*sin(radians(angle-90)) ;
      zpos =   zpos- Radius*cos(radians(angle-90)) ;
    } else if (key == 'D' || key == 'd') {
      // right 
      xpos =   Radius*sin(radians(angle-90)) + xpos;
      zpos =   Radius*cos(radians(angle-90)) + zpos;
    } 
    // ----------------------------    
    // fly up & down 
    else if (key == 'r' || key == 'R') {
      ypos-=4;  // fly up
    } else if (key == 'f' || key == 'F') {
      ypos+=4;  // down 
      if (ypos > floorLevel-120) {  // check Floor
        ypos = floorLevel-120;
      }
    }     
    // ----------------------------    
    // fly up & down FAST 
    else if (key == 'b' || key == 'B') {
      // Bird
      ypos-=400;
    } else if (key == 'n' || key == 'N') {
      // un-Bird: go deeper 
      ypos+=400; 
      if (ypos > floorLevel-120) { // check Floor
        ypos = floorLevel-120;
      }
    }        
    // ----------------------------        
    // crouch 
    else if (key == 'c' || key == 'C') {
      isCrouching=!isCrouching;
      if ( isCrouching ) {
        // crouch
        ypos = floorLevel - 40; //  height/2.0;
      } else 
      {
        // stand 
        ypos = floorLevel - 120; //  height/2.0;
      }
    } // 'c'
    else if (key == 'i' || key == 'I') {
      // Saves a TIFF file named "diagonal.tif"
      save("Runner1.tif");
    } else if (key == 'y' || key == 'Y') {
      // go center
      xpos = 0; 
      zpos = 0;
    } else {
    }
    //
    //checkBoundaries ();
  } else {

    // key is coded
    // F1
    if (keyCode == LEFT) {
      // robot.mouseMove(1, mouseY);
      //angle--;
      //rmx-=3;
    } else if (keyCode == RIGHT) {
      // robot.mouseMove(1, mouseY);
      //angle--;
      //rmx+=3;
    } else if (keyCode == UP) {
      // robot.mouseMove(1, mouseY);
      //angle--;
      // rmy-=3;
    } else if (keyCode == DOWN) {
      // robot.mouseMove(1, mouseY);
      //angle--;
      // rmy+=3;
    } else {
    }
    //
  } // else
} // func
//

// -------------------------------------------------------------

void setCameraAccordingToMouse () {

  // USING mouseXSpecial instead of Mouse

  float radius = 450.0;  // radius of circle 

  // command map: See Help. 
  angle = map(mouseXSpecial, width, 0, 0, 359); // left right 

  // look at 
  xlookat = radius*sin(radians(angle)) + xpos;            // look left / right, x look at
  ylookat = map(mouseY, 0, height, -1470, height); // look up / down 
  zlookat = radius*cos(radians(angle)) + zpos;            // look left / right, z look at
}

void keyPressed() {
  if (key == 'e') {
    player.heading += 1;
  }
  if (key == 'q') {
    player.heading -= 1;
  }
}

class Player {
  PVector pos;
  int heading;
  int r = 24;
  Player() {
    pos = new PVector(300, 300, 0);
  }

  void show() {
    pushMatrix();
    fill(255);
    translate(pos.x, pos.y, pos.z);
    sphere(r);
    popMatrix();
  }

  void view() {
    stroke(0, 255, 255);
    if (heading == -1) {
      heading = 3;
    }
    if (heading == 4) {
      heading = 0;
    }
    if (heading == 0) {
      //camera(player.pos.x,player.pos.y,player.pos.z,player.pos.x+100, player.pos.y,player.pos.z,0,1,0);
      line(player.pos.x, player.pos.y, player.pos.z, player.pos.x+100, player.pos.y, player.pos.z);
    }
    if (heading == 1) {
      //camera(player.pos.x,player.pos.y,player.pos.z,player.pos.x, player.pos.y+100,player.pos.z,0,1,0);
      line(player.pos.x, player.pos.y, player.pos.z, player.pos.x, player.pos.y+100, player.pos.z);
    }
    if (heading == 2) {
      //camera(player.pos.x,player.pos.y,player.pos.z,player.pos.x-100, player.pos.y,player.pos.z,0,1,0);
      line(player.pos.x, player.pos.y, player.pos.z, player.pos.x-100, player.pos.y, player.pos.z);
    }
    if (heading == 3) {
      //camera(player.pos.x,player.pos.y,player.pos.z,player.pos.x, player.pos.y-100,player.pos.z,0,1,0);
      line(player.pos.x, player.pos.y, player.pos.z, player.pos.x, player.pos.y-100, player.pos.z);
    }
  }
}

// ===============================================================
//class

class Objects {

  PVector pos, bsize;
  color col;

  Objects(int x, int y, int z, 
    int b, int h, int t, 
    color col_) {
    pos = new PVector(x, y, z);
    bsize = new PVector(b, h, t);
    col=col_;
  }

  void show() {
    //3D
    pushMatrix();
    fill(col); 
    stroke(0); 
    translate(pos.x, pos.y, pos.z);
    box(bsize.x, bsize.y, bsize.z);
    popMatrix();
  }
}//class
//

I don’t like how the mouse control all cam movement. you get movement that I don’t like. But I think I can modify this to please my needs. TY.

1 Like

?? - that’s how first person shooter work.

Remark

the mouse controls the cam movement

  • for x: when you are in the left or right 50 pixel zone
  • for y: when mouseY>0

you can make two 50 pixel zones for y too. Then the mouse doesn’t have so much influence.

So far I figured out how to make the wasd control the cam and then i’ll have the mouse be within 50 of side to turn then I still can keep it function to attack also. The program you just sent the cam zoomed upwards without any reason. It went from showing table to the sky. After I figure out how to add the 50 to left and right and also fix the angel then I’ll either use a 50 to look up and down or a key maybe both. At least that the plan, so the mouse shouldn’t go as crazy & show the sky when wanna see the table.

1 Like

I already did that for left and right side

Remark

Actually, there is a robot class tool that can put the mouse to certain spots. Very useful. I couldn’t make it work.

But did you notice how in queasycam the mouse can go all the way left and re-appear on right side? That’s robot class I guess.

I saw that now I need to do it for up and down. Why did you use mouse instead of player?

Read the reference on camera

The first 3 parameters are player position, then follow 3 parameters with look At position, these are derived from mouse

OK, I’m close to making the code work for me. at the keyPress why do you got Radius? shouldn’t the eye of the camera be at xpos, which for me is player.x.

int Radius = 13
if (key == 'w' || key == 'W') {
      // forward : should be running towards lookat 
      xpos =   Radius*sin(radians(angle)) + xpos;
      zpos =   Radius*cos(radians(angle)) + zpos;
    } else if (key == 's' || key == 'S') {
      // backward
      xpos =  xpos- (Radius*sin(radians(angle))) ;
      zpos =  zpos- (Radius*cos(radians(angle))) ;
    }