How to make a permanent foreground object

If I am doing something which has an object moving, and therefore the background being redrawn each frame, is there any way to keep another object on the screen? I cannot figure out how to do it without redrawing that object every single frame, which is difficult when the origin is moving due to translate().

Answers

  • Answer ✓

    You will have to redraw the background every time but you can isolate translations (also rotation and scale transformations) with pushMatrix(0 and popMatrix() like this

    PVector obj0, obj1;
    
    void setup() {
      size(800, 300, P2D);
      obj0 = new PVector(100, 100);
      obj1 = new  PVector(100, 200);
    }
    
    void draw() {
      background(0);
      pushMatrix();
      translate(obj0.x, obj0.y);
      fill(255, 255, 0);
      ellipse(0, 0, 20, 20);
      popMatrix();
      pushMatrix();
      translate(obj1.x, obj1.y);
      fill(255, 0, 255);
      ellipse(0, 0, 20, 20);
      popMatrix();
      obj0.x = (obj0.x + 2.5) % width;
      obj1.x = (obj1.x + 1.9) % width;
    }
    
  • in 3D you can also use HUD - see Wikipedia

    ask if you need one of those please

  • You don't have to use translate() to move one object? You could just change it's coordinates.

Sign In or Register to comment.