# PShape.resetMatrix() not resetting rotation in P2D

**URL:** <https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508>\
**Category:** Coding Questions\
**Created:** [February 18, 2019, 1:46pm UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508 "2019-02-18T13:46:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![wqt](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wqt/32/3546_2.png) [@wqt](https://discourse.processing.org/u/wqt)\
**Post date:** [February 18, 2019, 1:46pm UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508/1 "2019-02-18T13:46:57Z")

</div>

Hello everyone!  
I have a quite weird `resetMatrix` bug on `PShape`. It works fine in the default renderer, but in `P2D` it seems to reset only the translation and not the rotation. So what I am seeing is a spinning quad, instead of a fixed one. If you are testing the code below, are you seeing the same difference?  
Processing reference mentions that `resetMatrix()` is similar to OpenGL’s `glLoadIdentity()`, so is there an equivalent way to do this?

```auto
PShape s;
void setup(){
  size(300, 300, P2D); // works fine with default renderer
  s = createShape(QUAD, -75, -75, 75, -75, 75, 75, -75, 75);
}
void draw(){
  background(0);
  s.resetMatrix();
  s.rotate(0.01); // rotate is not reset, so it keeps adding up
  s.translate(30,10); // translate is reset properly
  shape(s, 150, 150);
}

```

🙂

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [February 18, 2019, 3:40pm UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508/2 "2019-02-18T15:40:04Z")

</div>

Why are you calling `rotate` and `translate` on the shape and not directly? Changing the matrix in the `PShape` can be quite inefficient.

---

<div class="post-metadata">

**Author:** ![wqt](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wqt/32/3546_2.png) [@wqt](https://discourse.processing.org/u/wqt)\
**Post date:** [February 18, 2019, 4:00pm UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508/3 "2019-02-18T16:00:22Z")

</div>

> [@neilcsmith](#):
>
> Why are you calling `rotate` and `translate` on the shape and not directly?

I am using a shape group, I did not include it here to simplify the problem. Each shape in the group needs to rotate and translate individually.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [February 18, 2019, 4:15pm UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508/4 "2019-02-18T16:15:09Z")

</div>

OK. Out of interest does this work with Processing 3.4? I know there was some changes to try and make matrix operations on shapes more efficient. What you’re doing actually performs worse than just drawing the shapes directly unfortunately.

---

<div class="post-metadata">

**Author:** ![wqt](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wqt/32/3546_2.png) [@wqt](https://discourse.processing.org/u/wqt)\
**Post date:** [February 18, 2019, 4:33pm UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508/5 "2019-02-18T16:33:41Z")

</div>

> [@neilcsmith](#):
>
> What you’re doing actually performs worse than just drawing the shapes directly unfortunately

Actually I tried making about 10000 of those quad PShapes without the shape group and I got worse performances, frameRate dropped by half.

> [@neilcsmith](#):
>
> OK. Out of interest does this work with Processing 3.4?

I haven’t tried reverting to 3.4, I am using 3.5.2. I would try it but my code needs to run on machines that do not belong to me but to a museum, the guy that runs the IT department barely lets me install external libraries 😒

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [February 18, 2019, 4:35pm UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508/6 "2019-02-18T16:35:51Z")

</div>

> [@wqt](#):
>
> Actually I tried making about 10000 of those quad PShapes without the shape group and I got worse performances, frameRate dropped by half.

I meant vs using `quad(...)` directly without PShape.

> [@wqt](#):
>
> I haven’t tried reverting to 3.4, I am using 3.5.2. I would try it but my code needs to run on machines that do not belong to me but to a museum

Purely an idea to see if this is a regression caused by recent change in behaviour.

---

<div class="post-metadata">

**Author:** ![wqt](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wqt/32/3546_2.png) [@wqt](https://discourse.processing.org/u/wqt)\
**Post date:** [February 18, 2019, 4:51pm UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508/7 "2019-02-18T16:51:36Z")

</div>

> [@neilcsmith](#):
>
> I meant vs using `quad(...)` directly without PShape.

Oh ok, sorry I misunderstood! 🙂 I need an image texture on those quads, hence the shape. If I got it correctly `texture()` only works with shapes.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [February 18, 2019, 5:02pm UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508/8 "2019-02-18T17:02:30Z")

</div>

Well, you could use `beginShape(QUADS)` and calls to `vertex(..)`.

The problem is that resetting matrices on a `PShape` causes performance issues which negates the benefit of the retained shape. This issue, and some changes that I wonder might be causing a difference between 3.4 and 3.5, are covered at [https://github.com/processing/processing/issues/5685](https://github.com/processing/processing/issues/5685) and have been discussed in a few threads here.

Also see [https://processing.org/examples/texturequad.html](https://processing.org/examples/texturequad.html)

---

<div class="post-metadata">

**Author:** ![wqt](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wqt/32/3546_2.png) [@wqt](https://discourse.processing.org/u/wqt)\
**Post date:** [February 19, 2019, 12:10am UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508/9 "2019-02-19T00:10:02Z")

</div>

> [@neilcsmith](#):
>
> Well, you could use `beginShape(QUADS)` and calls to `vertex(..)` .

So I just tried that with 10000 quads and it is even slower. Thank you for suggesting it though.

I think I am going to stick with the original group shape and compute a difference angle for the rotation.

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [February 19, 2019, 2:20am UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508/10 "2019-02-19T02:20:06Z")

</div>

a really dirty workaround (that should never be used)

```auto
PShape s;
PMatrix2D matrix;

void setup(){
  size(300, 300, P2D); // works fine with default renderer
  s = createShape(QUAD, -75, -75, 75, -75, 75, 75, -75, 75);
  matrix = new PMatrix2D();
}
void draw(){
  background(0);
  
  matrix.reset();
  matrix.translate(30, 10);
  matrix.rotate(45);
  s.resetMatrix();
  s.applyMatrix(matrix);
  
  shape(s, 150, 150);
}

```

but don’t ask me why 😛

---

<div class="post-metadata">

**Author:** ![wqt](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wqt/32/3546_2.png) [@wqt](https://discourse.processing.org/u/wqt)\
**Post date:** [February 19, 2019, 9:13am UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508/11 "2019-02-19T09:13:17Z")

</div>

@hotfooted Great! this solved the rotation issue without slowing down anything 🤩

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [February 19, 2019, 9:18am UTC](https://discourse.processing.org/t/pshape-resetmatrix-not-resetting-rotation-in-p2d/8508/12 "2019-02-19T09:18:57Z")

</div>

> [@wqt](#):
>
> So I just tried that with 10000 quads and it is even slower. Thank you for suggesting it though.

Interesting, and unexpected! So I was going to suggest you test the Demos/Performance/DynamicParticles(Immediate|Retained) examples. On 3.4 the performance of the retained example is dramatically worse. But I just noticed that on 3.5 it isn’t.

So, then I tested your original code. On 3.4 it’s correct, on 3.5 it continually rotates. So it’s definitely a regression caused by the performance improvements in PShape matrix handling.
