# Transformations issues

**URL:** <https://discourse.processing.org/t/transformations-issues/24079>\
**Category:** Coding Questions\
**Created:** [September 23, 2020, 12:00pm UTC](https://discourse.processing.org/t/transformations-issues/24079 "2020-09-23T12:00:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bernat\_ratpenat](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bernat\_ratpenat](https://discourse.processing.org/u/bernat_ratpenat)\
**Post date:** [September 23, 2020, 12:00pm UTC](https://discourse.processing.org/t/transformations-issues/24079/1 "2020-09-23T12:00:56Z")

</div>

Hi there! I’ve been trying to code a torus and, while I have found valid examples, I still don’t understand why my code does not work. I’m sure it’s related to transformations and must’ve misunderstood something about them but after reading the reference and seeking up examples I’ve made no progress 😕  
I tried to position the vertices through transformations in a function but it appears as if they were not read and all the vertices ended up one on top of another.

Any help or suggestions are welcome!  
Thanks!!

```auto
import peasy.*;

float r1, r2;
PeasyCam cam;

void setup() {
  size(900, 900, P3D);
  cam = new PeasyCam(this, 500);
}

void draw() {
  background(57);
  r1 = 200;
  r2 = 100;
  rotateY(frameCount*PI/370);
  rosquilla(r1, r2);
   
}

void rosquilla(float r1, float r2) {
  float sides = 20;
  float inc1 = TWO_PI/sides;
  float sides2 = 8;
  float inc2 = TWO_PI/sides2;
  
  beginShape(QUAD_STRIP);
  //noFill();
  stroke(255, 175, 100);
  pushMatrix();
  
  for (int i = 0; i < sides; i++) { // ihave some sort of problem with transformations
    pushMatrix();
    rotateY(inc1 * i);
    translate(r1, 0, 0);
    
    for (int j = 0; j < sides2; j++) {
      vertex(r2 * cos(inc2 * j), r2 *sin(inc2 * j), 0 );
      pushMatrix();
      rotateY(inc1 * (i + 1));
      vertex(r2 * cos(inc2 * j), r2 *sin(inc2 * j), 0);
      popMatrix();
      
    }
    popMatrix();
  }
  
  popMatrix();
  endShape();
  
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 24, 2020, 2:50am UTC](https://discourse.processing.org/t/transformations-issues/24079/2 "2020-09-24T02:50:17Z")

</div>

> [@bernat\_ratpenat](#):
>
> I tried to position the vertices through transformations in a function but it appears as if they were not read and all the vertices ended up one on top of another.

Try just moving your “beginShape” and “endShape” lines to around your inner loop, then run your sketch again.

```auto
void rosquilla(float r1, float r2) {
  float sides = 20;
  float inc1 = TWO_PI/sides;
  float sides2 = 8;
  float inc2 = TWO_PI/sides2;

  noFill();
  stroke(255, 175, 100);
  pushMatrix();

  for (int i = 0; i < sides; i++) { // ihave some sort of problem with transformations
    pushMatrix();
    rotateY(inc1 * i);
    translate(r1, 0, 0);

    beginShape(QUAD_STRIP);
    for (int j = 0; j <= sides2; j++) {
      vertex(r2 * cos(inc2 * j), r2 *sin(inc2 * j), 0 );
      pushMatrix();
      rotateY(inc1 * (i + 1));
      vertex(r2 * cos(inc2 * j), r2 *sin(inc2 * j), 0);
      popMatrix();
    }
    endShape(CLOSE);
    popMatrix();
  }

  popMatrix();
}

```

 ![Screen Shot 2020-09-23 at 7.48.38 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/479657bd0b8a881067b60818eb765b9aa091fcba.png)

You may also want one extra vertex on each ring, and to close those shapes.

---

<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:** [September 24, 2020, 7:51am UTC](https://discourse.processing.org/t/transformations-issues/24079/3 "2020-09-24T07:51:09Z")

</div>

[[Sorry, I wrote my answer before I saw Jeremy’s reply (I had difficulties to post my answer)]]

several issues

foremost, shape / vertex ignores all rotate and translate commands.

so yours won’t work

**Remark 1**

Even this Sketch won’t take s.translate(r2,0,0) (it would take it, but not translate the vertex but the entire shape imho);

```auto
PShape s; // The PShape object

void setup() {
  size(100, 100);
  // Creating a custom PShape as a square, by
  // specifying a series of vertices.
  s = createShape();
  s.beginShape();
  s.fill(0, 0, 255);
  s.noStroke();
  s.vertex(0, 0);
  s.vertex(0, 50);
  s.vertex(50, 50);
  s.vertex(50, 0);
  s.endShape(CLOSE);
}

void draw() {
  shape(s, 25, 25);
}

```

**Remark 2**

Here comes my approach using modelX etc.  
but it’s far from perfect (I never understood `QUAD_STRIP`)

Chrisir

```auto

//https://discourse.processing.org/t/transformations-issues/24079

import peasy.*;

float r1, r2;
PeasyCam cam;

void setup() {
  size(900, 900, P3D);
  cam = new PeasyCam(this, 500);
}

void draw() {
  background(57);
  r1 = 200;
  r2 = 100;
  // rotateY(frameCount*PI/370);
  noFill();
  rosquilla(r1, r2);
}

void rosquilla(float r1, float r2) {
  float sides = 20;
  float inc1 = TWO_PI/sides;
  float sides2 = 8;
  float inc2 = TWO_PI/sides2;

  noFill();
  //fill(255, 0, 0); 
  stroke(255, 175, 100);
  // pushMatrix();

  for (int i = 0; i < sides; i++) { // ihave some sort of problem with transformations

    beginShape();

    pushMatrix();
    rotateY(inc1 * i);
    translate(r1, 0, 0);

    for (int j = 0; j < sides2; j++) {
      // vertex(r2 * cos(inc2 * j), r2 *sin(inc2 * j), 0 );
      pushMatrix();
      rotateZ(inc2 * (j));
      translate(r2, 0, 0);
      float x1= modelX(0, 0, 0);
      float y1= modelY(0, 0, 0);
      float z1= modelZ(0, 0, 0);
      vertex(x1, y1, z1); 
      popMatrix();
    }//for I
    popMatrix();
    endShape(CLOSE);
  }//for II

  // popMatrix();
}

```

---

<div class="post-metadata">

**Author:** ![bernat\_ratpenat](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bernat\_ratpenat](https://discourse.processing.org/u/bernat_ratpenat)\
**Post date:** [September 24, 2020, 8:49am UTC](https://discourse.processing.org/t/transformations-issues/24079/4 "2020-09-24T08:49:31Z")

</div>

> [@jeremydouglass](#):
>
> You may also want one extra vertex on each ring, and to close those shapes.

Yeah I tried doing that through the rotation inside the nested loop, but, as @Chrisir said, shape/vertex ignore transformation commands ☹ Thanks for your answer!

---

<div class="post-metadata">

**Author:** ![bernat\_ratpenat](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bernat\_ratpenat](https://discourse.processing.org/u/bernat_ratpenat)\
**Post date:** [September 24, 2020, 8:56am UTC](https://discourse.processing.org/t/transformations-issues/24079/5 "2020-09-24T08:56:19Z")

</div>

Thanks, I did not know that vertices ignored transformations. Guess I’ll try another algorithm to set the points, maybe filling an array of PVectors or so.  
Thanks for mentioning modelX functions as well!! I’ll have to study harder hehe

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 24, 2020, 5:18pm UTC](https://discourse.processing.org/t/transformations-issues/24079/6 "2020-09-24T17:18:37Z")

</div>

It isn’t quite that it ignores them entirely. It is that the coordinate space for the vertex data is based on the transformation matrix when you write “beginShape().”

If you want a torus as a connected mesh, some good approaches are:

1. use the equation for a torus, and compute the mesh coordinates without using translate / rotate – use sin/cos instead. This is how the [processing.org](http://processing.org) Toroid example does it, using QUAD\_STRIP for a square mesh. [https://processing.org/examples/toroid.html](https://processing.org/examples/toroid.html)

2. use the Shapes 3D library – it has some really great tools for this.  
[http://www.lagers.org.uk/s3d4p/index.html](http://www.lagers.org.uk/s3d4p/index.html) – @quark also has a much older tutorial on creating a toroid class [http://lagers.org.uk/javafx/toroidclass.html](http://lagers.org.uk/javafx/toroidclass.html)

---

<div class="post-metadata">

**Author:** ![bernat\_ratpenat](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bernat\_ratpenat](https://discourse.processing.org/u/bernat_ratpenat)\
**Post date:** [September 25, 2020, 8:23am UTC](https://discourse.processing.org/t/transformations-issues/24079/7 "2020-09-25T08:23:56Z")

</div>

Thanks Jeremy!

> [@jeremydouglass](#):
>
> It isn’t quite that it ignores them entirely. It is that the coordinate space for the vertex data is based on the transformation matrix when you write “beginShape().”

ahh I see, I will check the resources!
