You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
alt="an illustration showing a 2d coordinate system on the left, showing an origin of (0,0) and a 3d coordinate system on the right, showing an origin of (0,0,0)">
71
+
76
72
<h2>Transformations</h2>
77
73
78
-
<p>p5.js has a few methods, <aclass="code"href="{{root}}/reference/#/p5/translate">translate()</a>, <aclass="code"href="{{root}}/reference/#/p5/rotate">rotate()</a>, and <aclass="code"href="{{root}}/reference/#/p5/scale">scale()</a>, that we can use to position and orient objects
79
-
within space. Collectively these are known as the <em>transformation</em> of an object. These methods are
74
+
<p>p5.js has a few methods, <aclass="code"href="{{root}}/reference/#/p5/translate">translate()</a>, <a
75
+
class="code"href="{{root}}/reference/#/p5/rotate">rotate()</a>, and <aclass="code"
76
+
href="{{root}}/reference/#/p5/scale">scale()</a>, that we can use to position and orient objects
77
+
within space. Each of these methods affect what is known as the <em>model matrix</em>. Collectively these are
78
+
known as the <em>transformation</em> of an object. These methods are
80
79
available for
81
80
both 2D and 3D drawing.</p>
82
81
@@ -90,6 +89,14 @@ slug: learn/
90
89
given direction. Anything drawn after we call translate will be
91
90
positioned relative to that point.</p>
92
91
92
+
<pre><codeclass="language-javascript">
93
+
...
94
+
// draw a box 100 units to the right
95
+
translate(100,0,0);
96
+
box();
97
+
..
98
+
</code></pre>
99
+
93
100
<h3>rotate()</h3>
94
101
95
102
{{!-- sketch illustrating rotation of geometry --}}
@@ -99,14 +106,51 @@ slug: learn/
99
106
<p><aclass="code"href="{{root}}/reference/#/p5/rotate">rotate(angle, [axis])</a> reorients whatever ever is
100
107
drawn after it.</p>
101
108
102
-
<p>When given one argument it rotates around the Z-axis, but you can rotate
103
-
in any direction by passing a vector as the second argument. Methods like <aclass="code"
<p>There are a few methods that we can use to rotate an object in 3d. Most of the time it's easiest to call
110
+
methods like like <aclass="code"href="{{root}}/reference/#/p5/rotateX">rotateX(angle)</a>, <aclass="code"
105
111
href="{{root}}/reference/#/p5/rotateY">rotateY(angle)</a>, and <aclass="code"
106
-
href="{{root}}/reference/#/p5/rotateZ">rotateZ(angle)</a> apply an angle to the
107
-
specific axis, but each can be called in combination. </p>
112
+
href="{{root}}/reference/#/p5/rotateZ">rotateZ(angle)</a>, which allow you to rotate around a specific axis.
113
+
</p>
114
+
115
+
<pre><codeclass="language-javascript">
116
+
...
117
+
// rotate each axis by 45 degrees
118
+
rotateX(PI/4);
119
+
rotateY(PI/4);
120
+
rotateZ(PI/4);
121
+
box();
122
+
..
123
+
</code></pre>
108
124
109
-
{{!-- TODO explain Euler angles --}}
125
+
<p>By default p5.js will want you to supply an angle in radians. Radians use numbers from 0 - 2PI to specify an
126
+
angle. If you want to use degrees, you can either convert degrees to radians using radians(), or you can use
127
+
angleMode(DEGREES).</p>
128
+
129
+
<pre><codeclass="language-javascript">
130
+
...
131
+
// rotate each axis by 45 degrees
132
+
rotateX(radians(45));
133
+
box();
134
+
//or
135
+
angleMode(DEGREES);
136
+
rotateY(45);
137
+
box();
138
+
..
139
+
</code></pre>
140
+
141
+
<p>You can also use <aclass="code"href="{{root}}/reference/#/p5/rotate">rotate(angle, [axis])</a>, which allows
142
+
you to specify which axis you'd like to rotate around using a vector as the second argument.</p>
143
+
144
+
<pre><codeclass="language-javascript">
145
+
...
146
+
// rotate 90 degrees around the Y axis
147
+
let axis = createVector(0,1,0);
148
+
rotate(PI/2, axis);
149
+
box();
150
+
..
151
+
</code></pre>
152
+
153
+
{{!-- TODO explain Euler angles or maybe note that quaternions exist but not yet in p5.js? --}}
110
154
111
155
<h3>scale()</h3>
112
156
@@ -125,19 +169,62 @@ slug: learn/
125
169
the object itself.</p>
126
170
127
171
<p>The order that you use will depend on what you’re trying to do, but a good place to start is to follow the
128
-
order of translate, rotate, and then scale. If you perform these out of order you might find that your
129
-
transformations don’t act the way that you expect them to.</p>
172
+
order of <strong>translate</strong>, <strong>rotate</strong>, and then <strong>scale</strong>. If you perform
173
+
these out of order you might find that your
174
+
transformations don’t act the way that you expect them to.
130
175
</p>
131
176
132
-
<p>Another tool we have in p5.js is <aclass="code"href="{{root}}/reference/#/p5/push">push()</a> and <aclass="code"href="{{root}}/reference/#/p5/pop">pop()</a>. Since transformations accumulate and affect each other, you
133
-
might want to control which objects are affected by a certain transformation. push() saves and sets aside our
134
-
current transformations (and certain styling, like stroke() or fill()), which allows you to perform other
135
-
transformations. pop() restores those transformations.</p>
136
-
</p>
177
+
<p>In the below example, try changing the order of translate and rotate and see how it affects where the object is
0 commit comments