| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - /** | ||
| 1 | + /** | ||
| 2 | 2 | * Non-orthogonal Reflection | |
| 3 | 3 | * by Ira Greenberg. | |
| 4 | 4 | * | |
@@ -7,121 +7,120 @@ | |||
| 7 | 7 | * vector. | |
| 8 | 8 | */ | |
| 9 | 9 | ||
| 10 | - float baseX1, baseY1, baseX2, baseY2; | ||
| 10 | + PVector base1; | ||
| 11 | + PVector base2; | ||
| 11 | 12 | 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; | ||
| 17 | 13 | ||
| 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() { | ||
| 19 | 23 | size(640, 360); | |
| 20 | 24 | ||
| 21 | 25 | 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(); | ||
| 26 | 29 | ||
| 27 | 30 | // start ellipse at middle top of screen | |
| 28 | - ellipseX = width/2; | ||
| 29 | - | ||
| 31 | + position = new PVector(width/2, 0); | ||
| 32 | + | ||
| 30 | 33 | // calculate initial random direction | |
| 31 | - directionX = random(0.1, 0.99); | ||
| 32 | - directionY = random(0.1, 0.99); | ||
| 34 | + direction = PVector.random2D(); | ||
| 33 | 35 | ||
| 34 | - // normalize direction vector | ||
| 35 | - float directionVectLength = sqrt(directionX*directionX + directionY*directionY); | ||
| 36 | - directionX /= directionVectLength; | ||
| 37 | - directionY /= directionVectLength; | ||
| 36 | + velocity = new PVector(); | ||
| 38 | 37 | } | |
| 39 | 38 | ||
| 40 | - void draw(){ | ||
| 39 | + void draw() { | ||
| 41 | 40 | // draw background | |
| 42 | 41 | fill(0, 12); | |
| 43 | 42 | noStroke(); | |
| 44 | 43 | rect(0, 0, width, height); | |
| 45 | 44 | ||
| 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; | ||
| 55 | 48 | } | |
| 56 | 49 | ||
| 57 | 50 | // draw base | |
| 58 | 51 | 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); | ||
| 60 | 53 | ||
| 61 | 54 | // 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); | ||
| 66 | 58 | ||
| 67 | 59 | // draw ellipse | |
| 68 | 60 | noStroke(); | |
| 69 | 61 | fill(255); | |
| 70 | - ellipse(ellipseX, ellipseY, ellipseRadius*2, ellipseRadius*2); | ||
| 62 | + ellipse(position.x, position.y, r*2, r*2); | ||
| 71 | 63 | ||
| 72 | 64 | // calculate ellipse velocity | |
| 73 | - velocityX = directionX * ellipseSpeed; | ||
| 74 | - velocityY = directionY * ellipseSpeed; | ||
| 65 | + velocity.set(direction); | ||
| 66 | + velocity.mult(speed); | ||
| 75 | 67 | ||
| 76 | 68 | // move elipse | |
| 77 | - ellipseX += velocityX; | ||
| 78 | - ellipseY += velocityY; | ||
| 69 | + position.add(velocity); | ||
| 79 | 70 | ||
| 80 | 71 | // normalized incidence vector | |
| 81 | - float incidenceVectorX = -directionX; | ||
| 82 | - float incidenceVectorY = -directionY; | ||
| 72 | + PVector incidence = PVector.mult(direction, -1); | ||
| 83 | 73 | ||
| 84 | 74 | // detect and handle collision | |
| 85 | - for (int i=0; i<xCoords.length; i++){ | ||
| 75 | + for (int i=0; i<coords.length; i++) { | ||
| 86 | 76 | // 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) { | ||
| 88 | 78 | ||
| 89 | 79 | // calculate dot product of incident vector and base top normal | |
| 90 | - float dot = incidenceVectorX*normalX + incidenceVectorY*normalY; | ||
| 80 | + float dot = incidence.dot(normal); | ||
| 91 | 81 | ||
| 92 | 82 | // calculate reflection vector | |
| 93 | - float reflectionVectorX = 2*normalX*dot - incidenceVectorX; | ||
| 94 | - float reflectionVectorY = 2*normalY*dot - incidenceVectorY; | ||
| 95 | - | ||
| 96 | 83 | // 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); | ||
| 99 | 85 | ||
| 100 | 86 | // draw base top normal at collision point | |
| 101 | 87 | 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); | ||
| 104 | 89 | } | |
| 105 | 90 | } | |
| 106 | 91 | ||
| 107 | 92 | // detect boundary collision | |
| 108 | 93 | // 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; | ||
| 112 | 97 | } | |
| 113 | 98 | // left | |
| 114 | - if (ellipseX < ellipseRadius){ | ||
| 115 | - ellipseX = ellipseRadius; | ||
| 116 | - directionX *= -1; | ||
| 99 | + if (position.x < r) { | ||
| 100 | + position.x = r; | ||
| 101 | + direction.x *= -1; | ||
| 117 | 102 | } | |
| 118 | 103 | // top | |
| 119 | - if (ellipseY < ellipseRadius){ | ||
| 120 | - ellipseY = ellipseRadius; | ||
| 121 | - directionY *= -1; | ||
| 104 | + if (position.y < r) { | ||
| 105 | + position.y = r; | ||
| 106 | + direction.y *= -1; | ||
| 122 | 107 | // 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; | ||
| 125 | 124 | } | |
| 126 | 125 | } | |
| 127 | 126 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments