Make a camera who follow a player

Hi everyone, i want to make a camera who follow my player ( i allready make a topic about it but now it’s a bit different)

so, i need a camera who follow the player but not linked to the screen so if i change the size of my screen it does not cause issue.

here’s a basic example:

Player player;
LimitOfTheWorld limitoftheworld;

int worldSize = 500;

void setup(){
  frame.setResizable(true);
  size(250,250);
  PVector pos = new PVector(250,250);
  PVector pos2 = new PVector(10,10);
  player = new Player(pos);
  limitoftheworld = new LimitOfTheWorld(pos2);
}

void draw(){
  background(255);
  fill(0,0,0);
  rect(worldSize,worldSize,0,0);
  player.display();
  player.playerMvt();
  limitoftheworld.display();
}
class LimitOfTheWorld{
  PVector position;
  float r;

  LimitOfTheWorld(PVector pos2){
    position = pos2.get();
    r = 450;
  }
  
  void display(){
    noFill();
    rect(position.x,position.y,r,r);
    stroke(5);
  }
}
class Player{
  
  PVector position;
  float r;
  
  
  Player(PVector pos){
    position = pos.get();
    r = 10;
  }
  
  void display(){
    fill(255,0,0);
    ellipse(position.x,position.y,r,r);
  }
  
  void playerMvt(){
    if(keyPressed){
      if(key == CODED){
        if(keyCode == UP){
          position.y -= 10;
        }
        if(keyCode == DOWN){
          position.y += 10;
        }
        if(keyCode == LEFT){
          position.x -= 10;
        }
        if(keyCode == RIGHT){
          position.x += 10;
        }
      }
    }
  }
}

So how can i make a “camera” who follow my player ?

Thanks for your help and your time.

1 Like

Chrisir

2 Likes

thanks i will read that and come back later for questions :slight_smile:

1 Like

Thanks guys, as always it’s work perfectly.

I just modify it a little bit so its work in my code, here is what i make.

Player player;
LimitOfTheWorld limitoftheworld;
PVector view;
int mode;
int worldSize = 500;

void setup(){
  surface.setResizable(true);
  size(250,250);
  PVector pos = new PVector(250,250);
  PVector pos2 = new PVector(10,10);
  player = new Player(pos);
  limitoftheworld = new LimitOfTheWorld(pos2);
  rect(worldSize,worldSize,0,0);
}

void draw(){
  background(255);
  
  switch(mode){
    case 0: // FLAT
    break;
    case 1: // TRACK
    translate(width/2, height/2);  // center screen on 0,0
    translate(-view.x, -view.y);   // recenter screen on view
    break;
  }

  
  pushMatrix();
  noFill();
  player.playerMvt();
  view = PVector.lerp(player.position,player.position,0.0);
 
  limitoftheworld.display();
  player.display();
  //ellipse(view.x,view.y,3,3);
  popMatrix();
}

void keyReleased() {
  if (key == CODED) {
    if (keyCode == CONTROL) {
      mode = (mode+1)%2;
    }
  }
}

i didn’t change Player and LimitOfTheWorld

2 Likes