Finding a fixpoint for the corner of a moveable camera in P3D Need urgent help

Hello everybody, currently I am working on a game in 3D where the camera moves when there’s deviation of mouseX from the middle of the screen. It’s turning around the character, and when c is pressed, it switches to ego-perspective. Now I want to create a health bar, which is always in the corner of the camerascreen, but everything I try won’t work. Please help me, I am at the border of desparation.
I won’t put all of the code in this, because the Character’s shape alone is a lot of data, and she also has attacks, it would just be too much, so I’ll show you how the camera moves and how the Character moves.

  void cam()
  {
    if(camey<5)
    {camdist=200;}
    xCam=(cos(mouseturn)*camdist)+xPos;
    if((mouseturn2<100&mouseturn>-100)&camey==0)
    {yCam=yPos+mouseturn2;}
    zCam=(sin(mouseturn)*camdist)+zPos;
    camera(xCam,yCam,zCam,xPos,yPos,zPos,0,1,0);
    if(camey>0)
    {
    camdist=10;
    yCam=yPos;
    }
  }
void moveAriel()
  {
    if(a1>0||a1<0)
    {
      xPos=(cos(mouseturn)*a1)+xPos;
      zPos=(sin(mouseturn)*a1)+zPos;
    }
    if(c1>0||c1<0)
    {
      xPos=(sin(mouseturn)*c1)+xPos;
      zPos=(sin(mouseturn)*c1)+zPos;
    }
    if(jump>0)
    {
      yPos=yPos+k;
      if(yPos>floor-160||yPos<floor-500)
      {k=-k;}
    }

If you wonder, the Character is an Ariel, an air spirit, at least that’s what I’ve called her.

Hi @SailorToei,

Would setMatrix or resetMatrix work? For example,

PMatrix3D gui = new PMatrix3D();

void settings() {
  size(512, 512, P3D);
}

void setup() {
  frameRate(60.0);
  gui.set(
    1.0, 0.0, 0.0, -width * 0.5,
    0.0, 1.0, 0.0, -height * 0.5,
    0.0, 0.0, 1.0, -height * 0.86602,
    0.0, 0.0, 0.0, 1.0);
}

void draw() {
  surface.setTitle(nfs(frameRate, 2, 1));
  drawWorld();
  drawGui();
}

void drawWorld() {
  float theta = frameCount * 0.01;

  background(#fff7d5);
  perspective();
  camera(
    height * cos(theta), 0.0, height * sin(theta),
    0.0, 0.0, 0.0,
    0.0, 1.0, 0.0);
  lights();

  noStroke();
  fill(#0000ff);
  box(50, 100, 25);
}

void drawGui() {
  noLights();
  ortho();
  setMatrix(gui);
  stroke(#7f7f7f);
  fill(#202020);
  rect(10, 10, 200, 20);

  noStroke();
  fill(#00ff00);
  rect(10, 10, 150, 20);
}

Best,
Jeremy

Thank you very much for your answer, I’m going to try it. Thank you very much!

1 Like

The Problem is, that I have Processing 3.5.4 and I can’t find a Function setMatrix or a class named PMatrix. But thank you very much though. Maybe I can use applyMatrix. Thank you for your advice :slightly_smiling_face:

Hi again,

No worries. Sorry I couldn’t be of more help. Does the example code I posted above run for you and do (a simplified version of) what you want? Are you talking about where to find them in the reference? You could try the Java Docs reference: PMatrix, PMatrix3D, setMatrix. That’s a good idea, setMatrix is the same as resetMatrix followed by applyMatrix.

Best,
Jeremy

I could not find it in the Reference, that’s why I thought it would not work. I’m going to try the code. Thank you very much. If this is my last message to you, I wish you a merry christmas. Thank you very much.

1 Like

I’m so sorry to bother you again, but I’m afraid I’m too stupid to understand all this matrix stuff. What exactly does the applyMatrix() function do? I am not the great vector user, so this topic is completely foreign to me. However, thank you for all of your work and advices.

Hi @SailorToei,

Don’t sweat it, it is complicated stuff, and I don’t know all there is to know on the subject either. You’ve probably seen 3D programs draw frames of reference with 3 lines representing right on the x axis in red, up on the y axis in green and forward on z axis in blue. Those three axes extend from an origin point, usually (0, 0, 0), but not necessarily. They look kinda like this

size(512, 512, P3D);
background(255.0, 255.0, 255.0);
camera(height, -height, height, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
strokeWeight(2.0);
stroke(255.0, 0.0, 0.0);
line(0.0, 0.0, 0.0, 100.0, 0.0, 0.0);
stroke(0.0, 255.0, 0.0);
line(0.0, 0.0, 0.0, 0.0, 100.0, 0.0);
stroke(0.0, 0.0, 255.0);
line(0.0, 0.0, 0.0, 0.0, 0.0, 100.0);

A matrix can store those three lines like this:

right x, up x, forward x, origin x,
right y, up y, forward y, origin y,
right z, up z, forward z, origin z,
0, 0, 0, 1

A vector is usually explained as a direction with a magnitude. However, a vector – along with a frame of reference stored in a matrix – can represent a point. You can multiply a matrix and a vector together, which I think the reference pages I linked earlier talk about.

A series of matrix-vector multiplications are needed to create the illusion of 3D space on your 2D computer screen. Processing manages most of the details for you behind the scenes.

  1. When you call ortho or perspective, Processing manages a projection matrix for you.
  2. When you call camera, Processing updates a camera matrix for you.
  3. When you call translate, rotate, scale, Processing updates a modelview matrix for you.

All resetMatrix does is reset a bunch of these matrices to the identity. But you also have to account for Processing’s convention of centering the camera at width / 2 and height / 2 at a suitable distance given your default field of view, height * sin(radians(60.0)). That’s why I set gui as I did above.

Best,
Jeremy

1 Like

see basically point (9) here

see https://amnonp5.wordpress.com/2012/01/28/25-life-saving-tips-for-processing/

when you repeat the camera() command at the end of draw(), you can draw the health bar afterwards in draw() (the 2D section of your code)

It’s called a HUD, see Wikipedia

Chrisir

2 Likes

@Chrisir,

Yeah, that’s much easier. Thanks! Also, the hint(DISABLE_DEPTH_TEST); is a good thing to remember to include.

1 Like

Thank you very much for the explaination, this is going to help me a lot for future times in processing and also in mathmatics. Thank you. Have a nice Christmas

Thank you very much for your answer. Have a nice Christmas

Thank you all very much for helping me. Thank you behreajj and Chrisir. If it was possible, I would mark both your comments as solutions. Thank you both very much. Here’s the final working code:

void healthbarA()
   {
     pushMatrix();
     hint(DISABLE_DEPTH_TEST);
     camera();
     noLights();
     rectMode(CORNER);
     fill(#FF0000);
     rect(650,375,healthbarA,10);
     hint(ENABLE_DEPTH_TEST);
     popMatrix();
   }

What about drawing the 2D first, before you manipulate the Matrix?