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
Copy file name to clipboardExpand all lines: content/tutorials/text/pvector/index.mdx
+23-19Lines changed: 23 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,11 +73,13 @@ void draw() {
73
73
}
74
74
```
75
75
76
-
In the above example, we have a very simple world -- a blank canvas with a circular shape (“ball”) traveling around. This “ball” has some properties.
76
+
In the above example, we have a very simple world — a blank canvas with a circular shape (“ball”) traveling around. This “ball” has some properties.
77
77
78
78
- LOCATION: x and y
79
79
- SPEED: xspeed and yspeed
80
-
In a more advanced sketch, we could imagine this ball and world having many more properties:
80
+
81
+
In a more advanced sketch, we could imagine this ball and world having many more properties:
82
+
81
83
- ACCELERATION: xacceleration and yacceleration
82
84
- TARGET LOCATION: xtarget and ytarget
83
85
- WIND: xwind and ywind
@@ -104,7 +106,7 @@ Vector speed;
104
106
105
107
Vectors aren't going to allow us to do anything new. Using vectors won't suddenly make your Processing sketches magically simulate physics, however, they will simplify your code and provide a set of functions for common mathematical operations that happen over and over and over again while programming motion.
106
108
107
-
As an introduction to vectors, we're going to live in 2 dimensions for quite some time (at least until we get through the first several chapters.) All of these examples can be fairly easily extended to three dimensions (and the class we will use --[**PVector**](http://processing.org/reference/PVector.html)-- allows for three dimensions.) However, for the time being, it's easier to start with just two.
109
+
As an introduction to vectors, we're going to live in 2 dimensions for quite some time (at least until we get through the first several chapters.) All of these examples can be fairly easily extended to three dimensions (and the class we will use —[**PVector**](http://processing.org/reference/PVector.html)— allows for three dimensions.) However, for the time being, it's easier to start with just two.
108
110
109
111
## Vectors: What are they to us, the Processing programmer?
110
112
@@ -118,7 +120,7 @@ Here are some vectors and possible translations:
118
120
119
121
</FixedImage>
120
122
121
-
You've probably done this before when programming motion. For every frame of animation (i.e. single cycle through Processing's (http://processing.org/reference/draw_.html)draw()] loop), you instruct each object on the screen to move a certain number of pixels horizontally and a certain number of pixels (vertically).
123
+
You've probably done this before when programming motion. For every frame of animation (i.e. single cycle through Processing's [**draw()**](http://processing.org/reference/draw_.html) loop), you instruct each object on the screen to move a certain number of pixels horizontally and a certain number of pixels (vertically).
122
124
123
125
For a Processing programmer, we can now understand a vector as the instructions for moving a shape from point A to point B, an object's “pixel velocity” so to speak.
124
126
@@ -132,7 +134,7 @@ For every frame:
132
134
133
135
</FixedImage>
134
136
135
-
If velocity is a vector (the difference between two points), what is location? Is it a vector too? Technically, one might argue that location is not a vector, it's not describing the change between two points, it's simply describing a singular point in space -- a location. And so conceptually, we think of a location as different: a single point rather than the difference between two points.
137
+
If velocity is a vector (the difference between two points), what is location? Is it a vector too? Technically, one might argue that location is not a vector, it's not describing the change between two points, it's simply describing a singular point in space — a location. And so conceptually, we think of a location as different: a single point rather than the difference between two points.
136
138
137
139
Nevertheless, another way to describe a location is as the path taken from the origin to reach that location. Hence, a location can be represented as the vector giving the difference between location and origin. Therefore, if we were to write code to describe a vector object, instead of creating separate Point and Vector classes, we can use a single class which is more convenient.
138
140
@@ -143,7 +145,7 @@ location --> x,y
143
145
velocity --> xspeed,yspeed
144
146
```
145
147
146
-
Notice how we are storing the same data for both -- two floating point numbers, an x and a y. If we were to write a vector class ourselves, we'd start with something rather basic:
148
+
Notice how we are storing the same data for both — two floating point numbers, an x and a y. If we were to write a vector class ourselves, we'd start with something rather basic:
147
149
148
150
```
149
151
class PVector {
@@ -177,7 +179,7 @@ PVector location = new PVector(100,100);
177
179
PVector velocity = new PVector(1,3.3);
178
180
```
179
181
180
-
Now that we have two vector objects (“location” and “velocity”), we're ready to implement the algorithm for motion -- location = location + velocity. In the bouncing ball example, without vectors, we had:
182
+
Now that we have two vector objects (“location” and “velocity”), we're ready to implement the algorithm for motion — location = location + velocity. In the bouncing ball example, without vectors, we had:
181
183
182
184
```
183
185
// Add the current speed to the location.
@@ -192,7 +194,7 @@ In an ideal world, we would just be able to rewrite the above to be:
192
194
location = location + velocity;
193
195
```
194
196
195
-
However, in Processing, the addition operator '+' is reserved for primitive values (integers, floats, etc.) only. Processing doesn't know how to add two **PVector**](http://processing.org/reference/PVector.html) objects together any more than it knows how to add two PFont objects or PImage objects. Fortunately for us, the [**PVector**](http://processing.org/reference/PVector.html) class is implemented with functions for common mathematical operations.
197
+
However, in Processing, the addition operator '+' is reserved for primitive values (integers, floats, etc.) only. Processing doesn't know how to add two [**PVector**](http://processing.org/reference/PVector.html) objects together any more than it knows how to add two PFont objects or PImage objects. Fortunately for us, the [**PVector**](http://processing.org/reference/PVector.html) class is implemented with functions for common mathematical operations.
196
198
197
199
## Vectors: Addition
198
200
@@ -268,7 +270,7 @@ Now that we see how add is written inside of [**PVector**](http://processing.org
268
270
location.add(velocity);
269
271
```
270
272
271
-
And here we are, ready to successfully complete our first goal -- rewrite the entire bouncing ball example using [**PVector**](http://processing.org/reference/PVector.html).
273
+
And here we are, ready to successfully complete our first goal — rewrite the entire bouncing ball example using [**PVector**](http://processing.org/reference/PVector.html).
272
274
273
275
Example: Bouncing Ball with PVector!
274
276
@@ -311,7 +313,7 @@ void draw() {
311
313
312
314
Now, you might feel somewhat disappointed. After all, this may initially appear to have made the code more complicated than the original version. While this is a perfectly reasonable and valid critique, it's important to understand that we haven't fully realized the power of programming with vectors just yet. Looking at a simple bouncing ball and only implementing vector addition is just the first step. As we move forward into looking at more a complex world of multiple objects and multiple forces (we'll cover forces in the next chapter), the benefits of [**PVector**](http://processing.org/reference/PVector.html) will become more apparent.
313
315
314
-
We should, however, make note of an important aspect of the above transition to programming with vectors. Even though we are using [**PVector**](http://processing.org/reference/PVector.html) objects to describe two values -- the x and y of location and the x and y of velocity -- we still often need to refer to the x and y components of each [**PVector**](http://processing.org/reference/PVector.html) individually. When we go to draw an object in Processing there's no means for us to say:
316
+
We should, however, make note of an important aspect of the above transition to programming with vectors. Even though we are using [**PVector**](http://processing.org/reference/PVector.html) objects to describe two values — the x and y of location and the x and y of velocity — we still often need to refer to the x and y components of each [**PVector**](http://processing.org/reference/PVector.html) individually. When we go to draw an object in Processing there's no means for us to say:
315
317
316
318
```
317
319
ellipse(location,16,16);
@@ -372,7 +374,7 @@ and the function inside [**PVector**](http://processing.org/reference/PVector.ht
372
374
373
375
</FixedImage>
374
376
375
-
Following is an example that demonstrates vector subtraction by taking the difference between two points -- the mouse location and the center of the window.
377
+
Following is an example that demonstrates vector subtraction by taking the difference between two points — the mouse location and the center of the window.
376
378
377
379
Example: Vector subtraction
378
380
@@ -421,7 +423,7 @@ The fancy terminology and symbols aside, this is really quite a simple concept.
421
423
422
424
</HighlightBlock>
423
425
424
-
Moving onto multiplication, we have to think a little bit differently. When we talk about multiplying a vector what we usually mean is **_scaling_** a vector. Maybe we want a vector to be twice its size or one-third its size, etc. In this case, we are saying “Multiply a vector by 2” or “Multiply a vector by 1/3&rquo;. Note we are multiplying a vector by a scalar, a single number, not another vector.
426
+
Moving onto multiplication, we have to think a little bit differently. When we talk about multiplying a vector what we usually mean is **_scaling_** a vector. Maybe we want a vector to be twice its size or one-third its size, etc. In this case, we are saying “Multiply a vector by 2” or “Multiply a vector by 1/3”. Note we are multiplying a vector by a scalar, a single number, not another vector.
425
427
426
428
To scale a vector by a single number, we multiply each component (x and y) by that number.
427
429
@@ -522,7 +524,9 @@ u.div(2);
522
524
As with addition, basic algebraic rules of multiplication and division apply to vectors.
523
525
524
526
The associative rule: (n\*m)**\*v** = n\*(m**\*v**)
In the bouncing ball example, all of this code happened in Processing's main tab, within [**_setup()_**](http://processing.org/reference/setup_.html) and [**_draw()_**](http://processing.org/reference/draw_.html). What we want to do now is move towards encapsulating all of the logic for motion inside of a [**_class_**](http://processing.org/reference/class.html), this way we can create a foundation for programming moving objects in Processing. We'll take a quick moment to review the basics of object-oriented programming in this context now, but this book will otherwise assume knowledge of working with objects (which will be necessary for just about every example from this point forward). However, if you need a further refresher, I encourage you to check out the [OOP tutorial](http://processing.org/tutorials/objects/).
696
700
697
-
The driving principle behind object-oriented programming is the bringing together of data and functionality. Take the prototypical OOP example: a car. A car has data -- color, size, speed, etc. A car has functionality -- drive(), turn(), stop(), etc. A car [**_class_**](http://processing.org/reference/class.html) brings all that stuff together in a template from which car instances, i.e. [**_objects_**](http://processing.org/reference/Object.html), are made. The benefit is nicely organized code that makes sense when you read it.
701
+
The driving principle behind object-oriented programming is the bringing together of data and functionality. Take the prototypical OOP example: a car. A car has data — color, size, speed, etc. A car has functionality — drive(), turn(), stop(), etc. A car [**_class_**](http://processing.org/reference/class.html) brings all that stuff together in a template from which car instances, i.e. [**_objects_**](http://processing.org/reference/Object.html), are made. The benefit is nicely organized code that makes sense when you read it.
698
702
699
703
```
700
704
Car c = new Car(red,big,fast);
@@ -858,7 +862,7 @@ class Mover {
858
862
}
859
863
```
860
864
861
-
Ok, at this point, we should feel comfortable with two things -- (1) What is a [**PVector**](http://processing.org/reference/PVector.html)? and (2) How do we use PVectors inside of an object to keep track of its location and movement? This is an excellent first step and deserves an mild round of applause. For standing ovations and screaming fans, however, we need to make one more, somewhat larger, step forward. After all, watching the Motion 101 example is fairly boring -- the circle never speeds up, never slows down, and never turns. For more interesting motion, for motion that appears in the real world around us, we need to add one more PVector to our class -- acceleration.
865
+
Ok, at this point, we should feel comfortable with two things — (1) What is a [**PVector**](http://processing.org/reference/PVector.html)? and (2) How do we use PVectors inside of an object to keep track of its location and movement? This is an excellent first step and deserves an mild round of applause. For standing ovations and screaming fans, however, we need to make one more, somewhat larger, step forward. After all, watching the Motion 101 example is fairly boring — the circle never speeds up, never slows down, and never turns. For more interesting motion, for motion that appears in the real world around us, we need to add one more PVector to our class — acceleration.
862
866
863
867
The strict definition of acceleration that we are using here is: **the rate of change of velocity**. Let's think about that definition for a moment. Is this a new concept? Not really. Velocity is defined as: **the rate of change of location**. In essence, we are developing a “trickle down” effect. Acceleration affects velocity which in turn affects location (for some brief foreshadowing, this point will become even more crucial in the next chapter when we see how forces affect acceleration which affects velocity which affects location.) In code, this reads like this:
864
868
@@ -867,7 +871,7 @@ The strict definition of acceleration that we are using here is: **the rate of c
867
871
location.add(velocity);
868
872
```
869
873
870
-
As an exercise, from this point forward, let's make a rule for ourselves. Let's write every example in the rest of this book without ever touching the value of velocity and location (except to initialize them). In other words, our goal now for programming motion is as follows -- come up with an algorithm for how we calculate acceleration and let the trickle down effect work its magic. And so we need to come up with some ways to calculate acceleration:
874
+
As an exercise, from this point forward, let's make a rule for ourselves. Let's write every example in the rest of this book without ever touching the value of velocity and location (except to initialize them). In other words, our goal now for programming motion is as follows — come up with an algorithm for how we calculate acceleration and let the trickle down effect work its magic. And so we need to come up with some ways to calculate acceleration:
871
875
872
876
ACCELERATION ALGORITHMS!
873
877
@@ -921,7 +925,7 @@ This means that when the sketch starts, the object is at rest. We don't have to
921
925
}
922
926
```
923
927
924
-
Are you thinking --“Gosh, those values seem awfully small!” Yes, that's right, they are quite tiny. It's important to realize that our acceleration values (measured in pixels) accumulate into the velocity over time, about thirty times per second depending on our sketch's frame rate. And so to keep the magnitude of the velocity vector within a reasonable range, our acceleration values should remain quite small. We can also help this cause by incorporating the PVector function [limit()](http://processing.org/reference/PVector_limit_.html).
928
+
Are you thinking —“Gosh, those values seem awfully small!” Yes, that's right, they are quite tiny. It's important to realize that our acceleration values (measured in pixels) accumulate into the velocity over time, about thirty times per second depending on our sketch's frame rate. And so to keep the magnitude of the velocity vector within a reasonable range, our acceleration values should remain quite small. We can also help this cause by incorporating the PVector function [limit()](http://processing.org/reference/PVector_limit_.html).
925
929
926
930
```
927
931
// The limit() function constrains the magnitude of a vector.
@@ -973,7 +977,7 @@ class Mover {
973
977
}
974
978
```
975
979
976
-
Ok, algorithm #2--“a totally random acceleration.” In this case, instead of initializing acceleration in the object's constructor we want to pick a new acceleration each cycle, i.e. each time update() is called.
980
+
Ok, algorithm #2—“a totally random acceleration.” In this case, instead of initializing acceleration in the object's constructor we want to pick a new acceleration each cycle, i.e. each time update() is called.
977
981
978
982
Example: Motion 101 (velocity and random acceleration)
979
983
@@ -1125,7 +1129,7 @@ With chaining the above can be written as:
1125
1129
1126
1130
## Vectors: Interactivity
1127
1131
1128
-
Ok, to finish out this tutorial, let's try something a bit more complex and a great deal more useful. Let's dynamically calculate an object's acceleration according to a rule, acceleration algorithm #4--“the object accelerates towards the mouse.”
1132
+
Ok, to finish out this tutorial, let's try something a bit more complex and a great deal more useful. Let's dynamically calculate an object's acceleration according to a rule, acceleration algorithm #4—“the object accelerates towards the mouse.”
1129
1133
1130
1134
<FixedImagewidth={650}height={210}>
1131
1135
@@ -1141,7 +1145,7 @@ Anytime we want to calculate a vector based on a rule/formula, we need to comput
1141
1145
1142
1146
</FixedImage>
1143
1147
1144
-
As illustrated in the above diagram, we see that we can get a vector (dx,dy) by subtracting the object's location from the mouse's location. After all, this is precisely where we started this chapter -- the definition of a vector is “the difference between two points in space!”
1148
+
As illustrated in the above diagram, we see that we can get a vector (dx,dy) by subtracting the object's location from the mouse's location. After all, this is precisely where we started this chapter — the definition of a vector is “the difference between two points in space!”
0 commit comments