FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

reworking Reflection1 to use PVector · frankleonrose/processing@b7f3cb2 · GitHub

Repository navigation

Commit b7f3cb2

Browse files
committed
reworking Reflection1 to use PVector
1 parent 3af2d7b commit b7f3cb2

1 file changed

Lines changed: 62 additions & 63 deletions

File tree

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/**
22
* Non-orthogonal Reflection
33
* by Ira Greenberg.
44
*
@@ -7,121 +7,120 @@
77
* vector.
88
*/
99

10-
float baseX1, baseY1, baseX2, baseY2;
10+
PVector base1;
11+
PVector base2;
1112
float baseLength;
12-
float[] xCoords, yCoords;
13-
float ellipseX, ellipseY, ellipseRadius = 6;
14-
float directionX, directionY;
15-
float ellipseSpeed = 3.5;
16-
float velocityX, velocityY;
1713

18-
void setup(){
14+
PVector[] coords;
15+
16+
PVector position;
17+
float r = 6;
18+
PVector direction;
19+
float speed = 3.5;
20+
PVector velocity;
21+
22+
void setup() {
1923
size(640, 360);
2024

2125
fill(128);
22-
baseX1 = 0;
23-
baseY1 = height-150;
24-
baseX2 = width;
25-
baseY2 = height;
26+
base1 = new PVector(0, height-150);
27+
base2 = new PVector(width, height);
28+
createGround();
2629

2730
// start ellipse at middle top of screen
28-
ellipseX = width/2;
29-
31+
position = new PVector(width/2, 0);
32+
3033
// calculate initial random direction
31-
directionX = random(0.1, 0.99);
32-
directionY = random(0.1, 0.99);
34+
direction = PVector.random2D();
3335

34-
// normalize direction vector
35-
float directionVectLength = sqrt(directionX*directionX + directionY*directionY);
36-
directionX /= directionVectLength;
37-
directionY /= directionVectLength;
36+
velocity = new PVector();
3837
}
3938

40-
void draw(){
39+
void draw() {
4140
// draw background
4241
fill(0, 12);
4342
noStroke();
4443
rect(0, 0, width, height);
4544

46-
// calculate length of base top
47-
baseLength = dist(baseX1, baseY1, baseX2, baseY2);
48-
xCoords = new float[ceil(baseLength)];
49-
yCoords = new float[ceil(baseLength)];
50-
51-
// fill base top coordinate array
52-
for (int i=0; i<xCoords.length; i++){
53-
xCoords[i] = baseX1 + ((baseX2-baseX1)/baseLength)*i;
54-
yCoords[i] = baseY1 + ((baseY2-baseY1)/baseLength)*i;
45+
for (int i=0; i<coords.length; i++) {
46+
coords[i].x = base1.x + ((base2.x-base1.x)/baseLength)*i;
47+
coords[i].y = base1.y + ((base2.y-base1.y)/baseLength)*i;
5548
}
5649

5750
// draw base
5851
fill(200);
59-
quad(baseX1, baseY1, baseX2, baseY2, baseX2, height, 0, height);
52+
quad(base1.x, base1.y, base2.x, base2.y, base2.x, height, 0, height);
6053

6154
// calculate base top normal
62-
float baseDeltaX = (baseX2-baseX1)/baseLength;
63-
float baseDeltaY = (baseY2-baseY1)/baseLength;
64-
float normalX = -baseDeltaY;
65-
float normalY = baseDeltaX;
55+
PVector baseDelta = PVector.sub(base2, base1);
56+
baseDelta.normalize();
57+
PVector normal = new PVector(-baseDelta.y, baseDelta.x);
6658

6759
// draw ellipse
6860
noStroke();
6961
fill(255);
70-
ellipse(ellipseX, ellipseY, ellipseRadius*2, ellipseRadius*2);
62+
ellipse(position.x, position.y, r*2, r*2);
7163

7264
// calculate ellipse velocity
73-
velocityX = directionX * ellipseSpeed;
74-
velocityY = directionY * ellipseSpeed;
65+
velocity.set(direction);
66+
velocity.mult(speed);
7567

7668
// move elipse
77-
ellipseX += velocityX;
78-
ellipseY += velocityY;
69+
position.add(velocity);
7970

8071
// normalized incidence vector
81-
float incidenceVectorX = -directionX;
82-
float incidenceVectorY = -directionY;
72+
PVector incidence = PVector.mult(direction, -1);
8373

8474
// detect and handle collision
85-
for (int i=0; i<xCoords.length; i++){
75+
for (int i=0; i<coords.length; i++) {
8676
// check distance between ellipse and base top coordinates
87-
if (dist(ellipseX, ellipseY, xCoords[i], yCoords[i]) < ellipseRadius){
77+
if (PVector.dist(position, coords[i]) < r) {
8878

8979
// calculate dot product of incident vector and base top normal
90-
float dot = incidenceVectorX*normalX + incidenceVectorY*normalY;
80+
float dot = incidence.dot(normal);
9181

9282
// calculate reflection vector
93-
float reflectionVectorX = 2*normalX*dot - incidenceVectorX;
94-
float reflectionVectorY = 2*normalY*dot - incidenceVectorY;
95-
9683
// assign reflection vector to direction vector
97-
directionX = reflectionVectorX;
98-
directionY = reflectionVectorY;
84+
direction.set(2*normal.x*dot - incidence.x, 2*normal.y*dot - incidence.y, 0);
9985

10086
// draw base top normal at collision point
10187
stroke(255, 128, 0);
102-
line(ellipseX, ellipseY, ellipseX-normalX*100,
103-
ellipseY-normalY*100);
88+
line(position.x, position.y, position.x-normal.x*100, position.y-normal.y*100);
10489
}
10590
}
10691

10792
// detect boundary collision
10893
// right
109-
if (ellipseX > width-ellipseRadius){
110-
ellipseX = width-ellipseRadius;
111-
directionX *= -1;
94+
if (position.x > width-r) {
95+
position.x = width-r;
96+
direction.x *= -1;
11297
}
11398
// left
114-
if (ellipseX < ellipseRadius){
115-
ellipseX = ellipseRadius;
116-
directionX *= -1;
99+
if (position.x < r) {
100+
position.x = r;
101+
direction.x *= -1;
117102
}
118103
// top
119-
if (ellipseY < ellipseRadius){
120-
ellipseY = ellipseRadius;
121-
directionY *= -1;
104+
if (position.y < r) {
105+
position.y = r;
106+
direction.y *= -1;
122107
// randomize base top
123-
baseY1 = random(height-100, height);
124-
baseY2 = random(height-100, height);
108+
base1.y = random(height-100, height);
109+
base2.y = random(height-100, height);
110+
createGround();
111+
}
112+
}
113+
114+
void createGround() {
115+
// calculate length of base top
116+
baseLength = PVector.dist(base1, base2);
117+
118+
// fill base top coordinate array
119+
coords = new PVector[ceil(baseLength)];
120+
for (int i=0; i<coords.length; i++) {
121+
coords[i] = new PVector();
122+
coords[i].x = base1.x + ((base2.x-base1.x)/baseLength)*i;
123+
coords[i].y = base1.y + ((base2.y-base1.y)/baseLength)*i;
125124
}
126125
}
127126

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL