From 0e31b2e402fc5f7f72cef58d2975d3dd6d1c46b7 Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Mon, 18 Mar 2013 15:23:11 -0400 Subject: [PATCH 1/6] reworked Cubes example with OOP and PVector --- .../Topics/Motion/CubesWithinCube/Cube.pde | 128 ++++++++++++------ .../CubesWithinCube/CubesWithinCube.pde | 101 +++----------- 2 files changed, 102 insertions(+), 127 deletions(-) diff --git a/java/examples/Topics/Motion/CubesWithinCube/Cube.pde b/java/examples/Topics/Motion/CubesWithinCube/Cube.pde index 271792623..190722770 100644 --- a/java/examples/Topics/Motion/CubesWithinCube/Cube.pde +++ b/java/examples/Topics/Motion/CubesWithinCube/Cube.pde @@ -1,72 +1,114 @@ // Custom Cube Class -class Cube{ +class Cube { + // Position, velocity vectors + PVector position; + PVector velocity; + // Also using PVector to hold rotation values for 3 axes + PVector rotation; + + // Vertices of the cube PVector[] vertices = new PVector[24]; + // width, height, depth float w, h, d; - // Default constructor - Cube(){ } + // colors for faces of cube + color[] quadBG = new color[6]; - // Constructor 2 Cube(float w, float h, float d) { this.w = w; this.h = h; this.d = d; + + // Colors are hardcoded + quadBG[0] = color(0); + quadBG[1] = color(51); + quadBG[2] = color(102); + quadBG[3] = color(153); + quadBG[4] = color(204); + quadBG[5] = color(255); + + // Start in center + position = new PVector(); + // Random velocity vector + velocity = PVector.random3D(); + // Random rotation + rotation = new PVector(random(40, 100), random(40, 100), random(40, 100)); // cube composed of 6 quads //front - vertices[0] = new PVector(-w/2,-h/2,d/2); - vertices[1] = new PVector(w/2,-h/2,d/2); - vertices[2] = new PVector(w/2,h/2,d/2); - vertices[3] = new PVector(-w/2,h/2,d/2); + vertices[0] = new PVector(-w/2, -h/2, d/2); + vertices[1] = new PVector(w/2, -h/2, d/2); + vertices[2] = new PVector(w/2, h/2, d/2); + vertices[3] = new PVector(-w/2, h/2, d/2); //left - vertices[4] = new PVector(-w/2,-h/2,d/2); - vertices[5] = new PVector(-w/2,-h/2,-d/2); - vertices[6] = new PVector(-w/2,h/2,-d/2); - vertices[7] = new PVector(-w/2,h/2,d/2); + vertices[4] = new PVector(-w/2, -h/2, d/2); + vertices[5] = new PVector(-w/2, -h/2, -d/2); + vertices[6] = new PVector(-w/2, h/2, -d/2); + vertices[7] = new PVector(-w/2, h/2, d/2); //right - vertices[8] = new PVector(w/2,-h/2,d/2); - vertices[9] = new PVector(w/2,-h/2,-d/2); - vertices[10] = new PVector(w/2,h/2,-d/2); - vertices[11] = new PVector(w/2,h/2,d/2); + vertices[8] = new PVector(w/2, -h/2, d/2); + vertices[9] = new PVector(w/2, -h/2, -d/2); + vertices[10] = new PVector(w/2, h/2, -d/2); + vertices[11] = new PVector(w/2, h/2, d/2); //back - vertices[12] = new PVector(-w/2,-h/2,-d/2); - vertices[13] = new PVector(w/2,-h/2,-d/2); - vertices[14] = new PVector(w/2,h/2,-d/2); - vertices[15] = new PVector(-w/2,h/2,-d/2); + vertices[12] = new PVector(-w/2, -h/2, -d/2); + vertices[13] = new PVector(w/2, -h/2, -d/2); + vertices[14] = new PVector(w/2, h/2, -d/2); + vertices[15] = new PVector(-w/2, h/2, -d/2); //top - vertices[16] = new PVector(-w/2,-h/2,d/2); - vertices[17] = new PVector(-w/2,-h/2,-d/2); - vertices[18] = new PVector(w/2,-h/2,-d/2); - vertices[19] = new PVector(w/2,-h/2,d/2); + vertices[16] = new PVector(-w/2, -h/2, d/2); + vertices[17] = new PVector(-w/2, -h/2, -d/2); + vertices[18] = new PVector(w/2, -h/2, -d/2); + vertices[19] = new PVector(w/2, -h/2, d/2); //bottom - vertices[20] = new PVector(-w/2,h/2,d/2); - vertices[21] = new PVector(-w/2,h/2,-d/2); - vertices[22] = new PVector(w/2,h/2,-d/2); - vertices[23] = new PVector(w/2,h/2,d/2); - } - void create(){ + vertices[20] = new PVector(-w/2, h/2, d/2); + vertices[21] = new PVector(-w/2, h/2, -d/2); + vertices[22] = new PVector(w/2, h/2, -d/2); + vertices[23] = new PVector(w/2, h/2, d/2); + } + + // Cube shape itself + void drawCube() { // Draw cube - for (int i=0; i<6; i++){ - beginShape(QUADS); - for (int j=0; j<4; j++){ - vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z); - } - endShape(); - } - } - void create(color[]quadBG){ - // Draw cube - for (int i=0; i<6; i++){ + for (int i=0; i<6; i++) { fill(quadBG[i]); beginShape(QUADS); - for (int j=0; j<4; j++){ + for (int j=0; j<4; j++) { vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z); } endShape(); } } + + // Update location + void update() { + position.add(velocity); + + // Check wall collisions + if (position.x > bounds/2 || position.x < -bounds/2) { + velocity.x*=-1; + } + if (position.y > bounds/2 || position.y < -bounds/2) { + velocity.y*=-1; + } + if (position.z > bounds/2 || position.z < -bounds/2) { + velocity.z*=-1; + } + } + + + // Display method + void display() { + pushMatrix(); + translate(position.x, position.y, position.z); + rotateX(frameCount*PI/rotation.x); + rotateY(frameCount*PI/rotation.y); + rotateZ(frameCount*PI/rotation.z); + noStroke(); + drawCube(); // Farm out shape to another method + popMatrix(); + } } - diff --git a/java/examples/Topics/Motion/CubesWithinCube/CubesWithinCube.pde b/java/examples/Topics/Motion/CubesWithinCube/CubesWithinCube.pde index b20420fcc..2259ecd5f 100644 --- a/java/examples/Topics/Motion/CubesWithinCube/CubesWithinCube.pde +++ b/java/examples/Topics/Motion/CubesWithinCube/CubesWithinCube.pde @@ -4,114 +4,47 @@ * * Collision detection against all * outer cube's surfaces. - * Uses the Point3D and Cube classes. */ -Cube stage; // external large cube -int cubies = 20; -Cube[]c = new Cube[cubies]; // internal little cubes -color[][]quadBG = new color[cubies][6]; +// 20 little internal cubes +Cube[] cubies = new Cube[20]; -// Controls cubie's movement -float[]x = new float[cubies]; -float[]y = new float[cubies]; -float[]z = new float[cubies]; -float[]xSpeed = new float[cubies]; -float[]ySpeed = new float[cubies]; -float[]zSpeed = new float[cubies]; - -// Controls cubie's rotation -float[]xRot = new float[cubies]; -float[]yRot = new float[cubies]; -float[]zRot = new float[cubies]; - -// Size of external cube +// Size of outer cube float bounds = 300; void setup() { size(640, 360, P3D); - - for (int i = 0; i < cubies; i++){ - // Each cube face has a random color component - float colorShift = random(-75, 75); - quadBG[i][0] = color(0); - quadBG[i][1] = color(51); - quadBG[i][2] = color(102); - quadBG[i][3] = color(153); - quadBG[i][4] = color(204); - quadBG[i][5] = color(255); + for (int i = 0; i < cubies.length; i++) { // Cubies are randomly sized float cubieSize = random(5, 15); - c[i] = new Cube(cubieSize, cubieSize, cubieSize); - - // Initialize cubie's position, speed and rotation - x[i] = 0; - y[i] = 0; - z[i] = 0; - - xSpeed[i] = random(-1, 1); - ySpeed[i] = random(-1, 1); - zSpeed[i] = random(-1, 1); - - xRot[i] = random(40, 100); - yRot[i] = random(40, 100); - zRot[i] = random(40, 100); + cubies[i] = new Cube(cubieSize, cubieSize, cubieSize); } - - // Instantiate external large cube - stage = new Cube(bounds, bounds, bounds); + } -void draw(){ +void draw() { background(50); lights(); - + // Center in display window translate(width/2, height/2, -130); - - // Outer transparent cube - noFill(); - + // Rotate everything, including external large cube rotateX(frameCount * 0.001); rotateY(frameCount * 0.002); rotateZ(frameCount * 0.001); stroke(255); - - // Draw external large cube - stage.create(); + + + // Outer transparent cube, just using box() method + noFill(); + box(bounds); // Move and rotate cubies - for (int i = 0; i < cubies; i++){ - pushMatrix(); - translate(x[i], y[i], z[i]); - rotateX(frameCount*PI/xRot[i]); - rotateY(frameCount*PI/yRot[i]); - rotateX(frameCount*PI/zRot[i]); - noStroke(); - c[i].create(quadBG[i]); - x[i] += xSpeed[i]; - y[i] += ySpeed[i]; - z[i] += zSpeed[i]; - popMatrix(); - - // Draw lines connecting cubbies - stroke(0); - if (i < cubies-1){ - line(x[i], y[i], z[i], x[i+1], y[i+1], z[i+1]); - } - - // Check wall collisions - if (x[i] > bounds/2 || x[i] < -bounds/2){ - xSpeed[i]*=-1; - } - if (y[i] > bounds/2 || y[i] < -bounds/2){ - ySpeed[i]*=-1; - } - if (z[i] > bounds/2 || z[i] < -bounds/2){ - zSpeed[i]*=-1; - } + for (Cube c : cubies) { + c.update(); + c.display(); } } From 6e58223c955780639baa500d4f2d473791443f63 Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Mon, 18 Mar 2013 15:39:47 -0400 Subject: [PATCH 2/6] comments for reflection1 example --- .../Topics/Motion/Reflection1/Reflection1.pde | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/java/examples/Topics/Motion/Reflection1/Reflection1.pde b/java/examples/Topics/Motion/Reflection1/Reflection1.pde index 730e0ed0b..a6bc6073b 100644 --- a/java/examples/Topics/Motion/Reflection1/Reflection1.pde +++ b/java/examples/Topics/Motion/Reflection1/Reflection1.pde @@ -7,17 +7,21 @@ * vector. */ +// Position of left hand side of floor PVector base1; +// Position of right hand side of floor PVector base2; +// Length of floor float baseLength; +// An array of subpoints along the floor path PVector[] coords; +// Variables related to moving ball PVector position; -float r = 6; -PVector direction; -float speed = 3.5; PVector velocity; +float r = 6; +float speed = 3.5; void setup() { size(640, 360); @@ -30,10 +34,9 @@ void setup() { // start ellipse at middle top of screen position = new PVector(width/2, 0); - // calculate initial random direction - direction = PVector.random2D(); - - velocity = new PVector(); + // calculate initial random velocity + velocity = PVector.random2D(); + velocity.mult(speed); } void draw() { @@ -42,11 +45,6 @@ void draw() { noStroke(); rect(0, 0, width, height); - for (int i=0; i width-r) { position.x = width-r; - direction.x *= -1; + velocity.x *= -1; } // left if (position.x < r) { position.x = r; - direction.x *= -1; + velocity.x *= -1; } // top if (position.y < r) { position.y = r; - direction.y *= -1; + velocity.y *= -1; // randomize base top base1.y = random(height-100, height); base2.y = random(height-100, height); @@ -111,6 +107,8 @@ void draw() { } } + +// Calculate variables for the ground void createGround() { // calculate length of base top baseLength = PVector.dist(base1, base2); From 4754ec6adc5946fa674e68a1f6950786af479b58 Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Mon, 18 Mar 2013 15:42:25 -0400 Subject: [PATCH 3/6] comments reflection2 --- .../Topics/Motion/Reflection2/Ground.pde | 4 ---- .../Topics/Motion/Reflection2/Orb.pde | 5 ++++- .../Topics/Motion/Reflection2/Reflection2.pde | 19 ++++++++++++------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/java/examples/Topics/Motion/Reflection2/Ground.pde b/java/examples/Topics/Motion/Reflection2/Ground.pde index 55d8497e2..acf7d3637 100644 --- a/java/examples/Topics/Motion/Reflection2/Ground.pde +++ b/java/examples/Topics/Motion/Reflection2/Ground.pde @@ -2,10 +2,6 @@ class Ground { float x1, y1, x2, y2; float x, y, len, rot; - // Default constructor - Ground(){ - } - // Constructor Ground(float x1, float y1, float x2, float y2) { this.x1 = x1; diff --git a/java/examples/Topics/Motion/Reflection2/Orb.pde b/java/examples/Topics/Motion/Reflection2/Orb.pde index a3e0d534a..570fc081a 100644 --- a/java/examples/Topics/Motion/Reflection2/Orb.pde +++ b/java/examples/Topics/Motion/Reflection2/Orb.pde @@ -1,7 +1,9 @@ class Orb { + // Orb has positio and velocity PVector position; PVector velocity; float r; + // A damping of 80% slows it down when it hits the ground float damping = 0.8; Orb(float x, float y, float r_) { @@ -22,7 +24,8 @@ class Orb { fill(200); ellipse(position.x, position.y, r*2, r*2); } - + + // Check boundaries of window void checkWallCollision() { if (position.x > width-r) { position.x = width-r; diff --git a/java/examples/Topics/Motion/Reflection2/Reflection2.pde b/java/examples/Topics/Motion/Reflection2/Reflection2.pde index e1967fe8d..d227e736b 100644 --- a/java/examples/Topics/Motion/Reflection2/Reflection2.pde +++ b/java/examples/Topics/Motion/Reflection2/Reflection2.pde @@ -9,16 +9,17 @@ Orb orb; PVector gravity = new PVector(0,0.05); +// The ground is an array of "Ground" objects int segments = 40; Ground[] ground = new Ground[segments]; -float[] peakHeights = new float[segments+1]; void setup(){ size(640, 360); + // An orb object that will fall and bounce around orb = new Orb(50, 50, 3); - // Calculate ground peak heights + float[] peakHeights = new float[segments+1]; for (int i=0; i Date: Mon, 18 Mar 2013 16:16:24 -0400 Subject: [PATCH 4/6] a new proposed example demonstrating morphing from one shape to another --- java/examples/Topics/Motion/Morph/Morph.pde | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 java/examples/Topics/Motion/Morph/Morph.pde diff --git a/java/examples/Topics/Motion/Morph/Morph.pde b/java/examples/Topics/Motion/Morph/Morph.pde new file mode 100644 index 000000000..1c6a65f81 --- /dev/null +++ b/java/examples/Topics/Motion/Morph/Morph.pde @@ -0,0 +1,63 @@ +/** + * Morph. + * + * Changing one shape into another by interpolating + * vertices from one to another + */ + +ArrayList circle = new ArrayList(); +ArrayList square = new ArrayList(); + +ArrayList morph = new ArrayList(); + +boolean state = false; + +void setup() { + size(640, 360); + + for (int angle = 0; angle < 360; angle += 9) { + PVector v = PVector.fromAngle(radians(angle-135)); + v.mult(100); + circle.add(v); + morph.add(new PVector()); + } + + for (int x = -50; x < 50; x += 10) { + square.add(new PVector(x, -50)); + } + for (int y = -50; y < 50; y += 10) { + square.add(new PVector(50, y)); + } + for (int x = 50; x > -50; x -= 10) { + square.add(new PVector(x, 50)); + } + for (int y = 50; y > -50; y -= 10) { + square.add(new PVector(-50, y)); + } +} + +void draw() { + background(51); + + for (int i = 0; i < circle.size(); i++) { + PVector v1; + if (state) v1 = circle.get(i); + else v1 = square.get(i); + PVector v2 = morph.get(i); + v2.lerp(v1, 0.1); + float d = PVector.dist(v1, v2); + if (d < 0.01) { + state = !state; + } + } + + translate(width/2, height/2); + beginShape(); + noFill(); + stroke(255); + for (PVector v : morph) { + vertex(v.x, v.y); + } + endShape(CLOSE); +} + From 0c35ad10a5f2a8b3e774b86fd48eeebe0165549c Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Mon, 18 Mar 2013 21:56:23 -0400 Subject: [PATCH 5/6] some comments for the new morph example --- java/examples/Topics/Motion/Morph/Morph.pde | 48 +++++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/java/examples/Topics/Motion/Morph/Morph.pde b/java/examples/Topics/Motion/Morph/Morph.pde index 1c6a65f81..e34fbad1b 100644 --- a/java/examples/Topics/Motion/Morph/Morph.pde +++ b/java/examples/Topics/Motion/Morph/Morph.pde @@ -5,32 +5,47 @@ * vertices from one to another */ +// Two ArrayLists to store the vertices for two shapes +// This example assumes that each shape will have the same +// number of vertices, i.e. the size of each ArrayList will be the same ArrayList circle = new ArrayList(); ArrayList square = new ArrayList(); +// An ArrayList for a third set of vertices, the ones we will be drawing +// in the window ArrayList morph = new ArrayList(); +// This boolean variable will control if we are morphing to a circle or square boolean state = false; void setup() { size(640, 360); + // Create a circle using vectors pointing from center for (int angle = 0; angle < 360; angle += 9) { + // Note we are not starting from 0 in order to match the + // path of a circle. PVector v = PVector.fromAngle(radians(angle-135)); v.mult(100); circle.add(v); + // Let's fill out morph ArrayList with blank PVectors while we are at it morph.add(new PVector()); } + // A square is a bunch of vertices along straight lines + // Top of square for (int x = -50; x < 50; x += 10) { square.add(new PVector(x, -50)); } + // Right side for (int y = -50; y < 50; y += 10) { square.add(new PVector(50, y)); } + // Bottom for (int x = 50; x > -50; x -= 10) { square.add(new PVector(x, 50)); } + // Left side for (int y = 50; y > -50; y -= 10) { square.add(new PVector(-50, y)); } @@ -39,19 +54,36 @@ void setup() { void draw() { background(51); + // We will keep how far the vertices are from their target + float totalDistance = 0; + + // Look at each vertex for (int i = 0; i < circle.size(); i++) { PVector v1; - if (state) v1 = circle.get(i); - else v1 = square.get(i); - PVector v2 = morph.get(i); - v2.lerp(v1, 0.1); - float d = PVector.dist(v1, v2); - if (d < 0.01) { - state = !state; + // Are we lerping to the circle or square? + if (state) { + v1 = circle.get(i); } + else { + v1 = square.get(i); + } + // Get the vertex we will draw + PVector v2 = morph.get(i); + // Lerp to the target + v2.lerp(v1, 0.1); + // Check how far we are from target + totalDistance += PVector.dist(v1, v2); } - + + // If all the vertices are close, switch shape + if (totalDistance < 0.1) { + state = !state; + } + + // Draw relative to center translate(width/2, height/2); + strokeWeight(4); + // Draw a polygon that makes up all the vertices beginShape(); noFill(); stroke(255); From 9a466e1866888c4c2e1d5f958d6ac0b806a39d79 Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Mon, 18 Mar 2013 22:12:27 -0400 Subject: [PATCH 6/6] misc PVector and example cleanup --- .../Fractals and L-Systems/Koch/KochFractal.pde | 2 +- .../Topics/Fractals and L-Systems/Koch/KochLine.pde | 9 +-------- java/examples/Topics/Simulate/Flocking/Boid.pde | 13 +++++-------- .../Simulate/GravitationalAttraction3D/Sun.pde | 5 ++--- .../AccelerationWithVectors.pde | 1 - .../Topics/Vectors/VectorMath/VectorMath.pde | 1 + 6 files changed, 10 insertions(+), 21 deletions(-) diff --git a/java/examples/Topics/Fractals and L-Systems/Koch/KochFractal.pde b/java/examples/Topics/Fractals and L-Systems/Koch/KochFractal.pde index b538959ab..22f8f9c0a 100644 --- a/java/examples/Topics/Fractals and L-Systems/Koch/KochFractal.pde +++ b/java/examples/Topics/Fractals and L-Systems/Koch/KochFractal.pde @@ -7,7 +7,7 @@ class KochFractal { ArrayList lines; // A list to keep track of all the lines int count; - public KochFractal() { + KochFractal() { start = new PVector(0,height-20); end = new PVector(width,height-20); lines = new ArrayList(); diff --git a/java/examples/Topics/Fractals and L-Systems/Koch/KochLine.pde b/java/examples/Topics/Fractals and L-Systems/Koch/KochLine.pde index b9eafc012..038549da7 100644 --- a/java/examples/Topics/Fractals and L-Systems/Koch/KochLine.pde +++ b/java/examples/Topics/Fractals and L-Systems/Koch/KochLine.pde @@ -48,13 +48,12 @@ class KochLine { PVector p = a.get(); p.add(v); - rotate(v,-radians(60)); + v.rotate(-radians(60)); p.add(v); return p; } - // Easy, just 2/3 of the way PVector kochright() { PVector v = PVector.sub(a, b); @@ -64,11 +63,5 @@ class KochLine { } } - public void rotate(PVector v, float theta) { - float xTemp = v.x; - // Might need to check for rounding errors like with angleBetween function? - v.x = v.x*cos(theta) - v.y*sin(theta); - v.y = xTemp*sin(theta) + v.y*cos(theta); - } diff --git a/java/examples/Topics/Simulate/Flocking/Boid.pde b/java/examples/Topics/Simulate/Flocking/Boid.pde index 787ef41ce..4d2a5677a 100644 --- a/java/examples/Topics/Simulate/Flocking/Boid.pde +++ b/java/examples/Topics/Simulate/Flocking/Boid.pde @@ -11,7 +11,7 @@ class Boid { Boid(float x, float y) { acceleration = new PVector(0,0); - velocity = new PVector(random(-1,1),random(-1,1)); + velocity = PVector.random2D(); location = new PVector(x,y); r = 2.0; maxspeed = 2; @@ -60,9 +60,8 @@ class Boid { // STEER = DESIRED MINUS VELOCITY PVector seek(PVector target) { PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mult(maxspeed); + // Scale to maximum speed + desired.setMag(maxspeed); // Steering = Desired minus Velocity PVector steer = PVector.sub(desired,velocity); steer.limit(maxforce); // Limit to maximum steering force @@ -120,8 +119,7 @@ class Boid { // As long as the vector is greater than 0 if (steer.mag() > 0) { // Implement Reynolds: Steering = Desired - Velocity - steer.normalize(); - steer.mult(maxspeed); + steer.setMag(maxspeed); steer.sub(velocity); steer.limit(maxforce); } @@ -143,8 +141,7 @@ class Boid { } if (count > 0) { sum.div((float)count); - sum.normalize(); - sum.mult(maxspeed); + sum.setMag(maxspeed); PVector steer = PVector.sub(sum,velocity); steer.limit(maxforce); return steer; diff --git a/java/examples/Topics/Simulate/GravitationalAttraction3D/Sun.pde b/java/examples/Topics/Simulate/GravitationalAttraction3D/Sun.pde index 3253bc02f..10fcc0373 100644 --- a/java/examples/Topics/Simulate/GravitationalAttraction3D/Sun.pde +++ b/java/examples/Topics/Simulate/GravitationalAttraction3D/Sun.pde @@ -18,10 +18,9 @@ class Sun { PVector attract(Planet m) { PVector force = PVector.sub(location,m.location); // Calculate direction of force float d = force.mag(); // Distance between objects - d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects - force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction) + d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude - force.mult(strength); // Get force vector --> magnitude * direction + force.setMag(strength); // Get force vector --> magnitude * direction return force; } diff --git a/java/examples/Topics/Vectors/AccelerationWithVectors/AccelerationWithVectors.pde b/java/examples/Topics/Vectors/AccelerationWithVectors/AccelerationWithVectors.pde index c92540373..dd8e00b77 100644 --- a/java/examples/Topics/Vectors/AccelerationWithVectors/AccelerationWithVectors.pde +++ b/java/examples/Topics/Vectors/AccelerationWithVectors/AccelerationWithVectors.pde @@ -15,7 +15,6 @@ Mover mover; void setup() { size(640,360); - smooth(); mover = new Mover(); } diff --git a/java/examples/Topics/Vectors/VectorMath/VectorMath.pde b/java/examples/Topics/Vectors/VectorMath/VectorMath.pde index fcc609234..3a3804881 100644 --- a/java/examples/Topics/Vectors/VectorMath/VectorMath.pde +++ b/java/examples/Topics/Vectors/VectorMath/VectorMath.pde @@ -30,6 +30,7 @@ void draw() { translate(width/2,height/2); // Draw the resulting vector stroke(255); + strokeWeight(4); line(0,0,mouse.x,mouse.y); }