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

**URL:** <https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482>\
**Category:** Coding Questions\
**Created:** [December 23, 2020, 6:59pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482 "2020-12-23T18:59:34Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)\
**Post date:** [December 23, 2020, 6:59pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/1 "2020-12-23T18:59:34Z")

</div>

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.

```auto
  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;
    }
  }

```

```auto
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.

---

<div class="post-metadata">

**Author:** ![behreajj](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@behreajj](https://discourse.processing.org/u/behreajj)\
**Post date:** [December 23, 2020, 7:20pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/2 "2020-12-23T19:20:48Z")

</div>

Hi @SailorToei,

Would `setMatrix` or [resetMatrix](https://processing.org/reference/resetMatrix_.html) work? For example,

```auto
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

---

<div class="post-metadata">

**Author:** ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)\
**Post date:** [December 23, 2020, 7:43pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/3 "2020-12-23T19:43:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)\
**Post date:** [December 23, 2020, 7:52pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/4 "2020-12-23T19:52:07Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![behreajj](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@behreajj](https://discourse.processing.org/u/behreajj)\
**Post date:** [December 23, 2020, 7:59pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/5 "2020-12-23T19:59:50Z")

</div>

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](http://processing.github.io/processing-javadocs/core/processing/core/PMatrix.html), [PMatrix3D](http://processing.github.io/processing-javadocs/core/processing/core/PMatrix3D.html), [setMatrix](http://processing.github.io/processing-javadocs/core/processing/opengl/PGraphicsOpenGL.html#setMatrix-processing.core.PMatrix3D-). That’s a good idea, `setMatrix` is the same as `resetMatrix` followed by `applyMatrix`.

Best,  
Jeremy

---

<div class="post-metadata">

**Author:** ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)\
**Post date:** [December 23, 2020, 8:02pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/6 "2020-12-23T20:02:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)\
**Post date:** [December 23, 2020, 9:20pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/7 "2020-12-23T21:20:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![behreajj](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@behreajj](https://discourse.processing.org/u/behreajj)\
**Post date:** [December 23, 2020, 9:43pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/8 "2020-12-23T21:43:10Z")

</div>

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

```auto
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:

```auto
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](https://github.com/processing/processing/blob/94144be998354ecf7a06d5301b7b4fab72df724c/core/src/processing/opengl/PGraphicsOpenGL.java#L3957) 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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 23, 2020, 9:43pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/9 "2020-12-23T21:43:41Z")

</div>

see basically point (9) here

see [https://amnonp5.wordpress.com/2012/01/28/25-life-saving-tips-for-processing/](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

---

<div class="post-metadata">

**Author:** ![behreajj](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@behreajj](https://discourse.processing.org/u/behreajj)\
**Post date:** [December 23, 2020, 9:45pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/10 "2020-12-23T21:45:45Z")

</div>

@Chrisir,

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

---

<div class="post-metadata">

**Author:** ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)\
**Post date:** [December 24, 2020, 1:24pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/11 "2020-12-24T13:24:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)\
**Post date:** [December 24, 2020, 1:27pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/12 "2020-12-24T13:27:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)\
**Post date:** [December 24, 2020, 2:03pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/13 "2020-12-24T14:03:46Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![Quertz](https://avatars.discourse-cdn.com/v4/letter/q/cc9497/32.png) [@Quertz](https://discourse.processing.org/u/Quertz)\
**Post date:** [December 27, 2020, 2:20pm UTC](https://discourse.processing.org/t/finding-a-fixpoint-for-the-corner-of-a-moveable-camera-in-p3d-need-urgent-help/26482/14 "2020-12-27T14:20:34Z")

</div>

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