# Rotating 3D-objects in PGraphics for hires output

**URL:** <https://discourse.processing.org/t/rotating-3d-objects-in-pgraphics-for-hires-output/31155>\
**Category:** Coding Questions\
**Created:** [July 10, 2021, 2:54pm UTC](https://discourse.processing.org/t/rotating-3d-objects-in-pgraphics-for-hires-output/31155 "2021-07-10T14:54:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArjanBytez](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/arjanbytez/32/13832_2.png) [@ArjanBytez](https://discourse.processing.org/u/ArjanBytez)\
**Post date:** [July 10, 2021, 2:54pm UTC](https://discourse.processing.org/t/rotating-3d-objects-in-pgraphics-for-hires-output/31155/1 "2021-07-10T14:54:00Z")

</div>

I’m trying to achieve something which should be fairly easy, but after checking the forums I still can’t get it to work: making 3D-objects to rotate in hires imagery in Processing (v3 in this case, v4 doesn’t seem to work) by using a PGraphics object. Everything works, except the rotating part, hence the obvious.  
I tried all kinds of variations with begin/endDraw(), rotating the screen instead g, wondering why I have to declare g anyway since it’s a global variable anyway (in JAVA2D mode at least), using another PGraphics object instead of global g, using push/pop etc. I get the hires image as expected, except for the boxes which are fixed in position, not rotating at all. What am I doing wrong?

```auto
int w=3000, h=3000;
PGraphics g;

void setup() {
  g = createGraphics(w, h, P3D);
  size(1000, 1000, P3D);
  background(#ffffff);
}

void draw() {
  g.beginDraw();
  g.rotate(.01*frameCount); // this should slowly rotate the boxes, but it doesn't!
  g.fill(#aa33ff+frameCount);
  g.stroke(#ffffff);
  g.strokeWeight(5);
  g.endDraw(); // I only get hires output with end/beginDraw before drawing boxes
  g.beginDraw();
  g.translate(int(random(w)), int(random(h)));
  g.box(random(100));
  g.endDraw();
  image(g, 0, 0, width, height);
}

void keyPressed() {
  if (key==' ') {
    g.save("hires.png");
  }
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 10, 2021, 3:05pm UTC](https://discourse.processing.org/t/rotating-3d-objects-in-pgraphics-for-hires-output/31155/2 "2021-07-10T15:05:27Z")

</div>

Hello,

Try this:

```auto
  g.beginDraw();
  //g.rotate(.01*frameCount); // this should slowly rotate the boxes, but it doesn't!
  g.fill(#aa33ff+frameCount);
  g.stroke(#ffffff);
  g.strokeWeight(5);
  g.endDraw(); // I only get hires output with end/beginDraw before drawing boxes
  
  g.beginDraw();
  //g.rotate(.01*frameCount);
  g.translate(int(random(w)), int(random(h)));
  g.rotate(.01*frameCount);
  g.box(random(100));
  g.endDraw();

  image(g, 0, 0, width, height);

```

Try rotate before or after translate to see what happens.

Also try above removing the endDraw() / beginDraw() in the middle.

Read these to understand why:

- [PGraphics / Reference / Processing.org](https://processing.org/reference/PGraphics.html)
- [2D Transformations / Processing.org](https://processing.org/tutorials/transform2d/) states:

> Note: in Processing, the coordinate system is restored to its original state (origin at the upper left of the window, no rotation, and no scaling) every time that the `draw()` function is executed.

`:)`

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/109325d7623a826d71a8c26cbc340a64a098d345.png)

---

<div class="post-metadata">

**Author:** ![ArjanBytez](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/arjanbytez/32/13832_2.png) [@ArjanBytez](https://discourse.processing.org/u/ArjanBytez)\
**Post date:** [July 10, 2021, 7:02pm UTC](https://discourse.processing.org/t/rotating-3d-objects-in-pgraphics-for-hires-output/31155/3 "2021-07-10T19:02:37Z")

</div>

I somehow completely failed to see this - in hindsight - easy solution, but I didn’t. Thanks, this obviously does exactly what is needed!
