mirror of
https://github.com/processing/processing4.git
synced 2026-02-14 19:05:34 +01:00
Merge branch 'master' of github.com:processing/processing
This commit is contained in:
@@ -7,7 +7,7 @@ class KochFractal {
|
||||
ArrayList<KochLine> 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<KochLine>();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
95
java/examples/Topics/Motion/Morph/Morph.pde
Normal file
95
java/examples/Topics/Motion/Morph/Morph.pde
Normal file
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Morph.
|
||||
*
|
||||
* Changing one shape into another by interpolating
|
||||
* 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<PVector> circle = new ArrayList<PVector>();
|
||||
ArrayList<PVector> square = new ArrayList<PVector>();
|
||||
|
||||
// An ArrayList for a third set of vertices, the ones we will be drawing
|
||||
// in the window
|
||||
ArrayList<PVector> morph = new ArrayList<PVector>();
|
||||
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
// 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);
|
||||
for (PVector v : morph) {
|
||||
vertex(v.x, v.y);
|
||||
}
|
||||
endShape(CLOSE);
|
||||
}
|
||||
|
||||
@@ -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<coords.length; i++) {
|
||||
coords[i].x = base1.x + ((base2.x-base1.x)/baseLength)*i;
|
||||
coords[i].y = base1.y + ((base2.y-base1.y)/baseLength)*i;
|
||||
}
|
||||
|
||||
// draw base
|
||||
fill(200);
|
||||
quad(base1.x, base1.y, base2.x, base2.y, base2.x, height, 0, height);
|
||||
@@ -61,15 +59,12 @@ void draw() {
|
||||
fill(255);
|
||||
ellipse(position.x, position.y, r*2, r*2);
|
||||
|
||||
// calculate ellipse velocity
|
||||
velocity.set(direction);
|
||||
velocity.mult(speed);
|
||||
|
||||
// move elipse
|
||||
position.add(velocity);
|
||||
|
||||
// normalized incidence vector
|
||||
PVector incidence = PVector.mult(direction, -1);
|
||||
PVector incidence = PVector.mult(velocity, -1);
|
||||
incidence.normalize();
|
||||
|
||||
// detect and handle collision
|
||||
for (int i=0; i<coords.length; i++) {
|
||||
@@ -81,7 +76,8 @@ void draw() {
|
||||
|
||||
// calculate reflection vector
|
||||
// assign reflection vector to direction vector
|
||||
direction.set(2*normal.x*dot - incidence.x, 2*normal.y*dot - incidence.y, 0);
|
||||
velocity.set(2*normal.x*dot - incidence.x, 2*normal.y*dot - incidence.y, 0);
|
||||
velocity.mult(speed);
|
||||
|
||||
// draw base top normal at collision point
|
||||
stroke(255, 128, 0);
|
||||
@@ -93,17 +89,17 @@ void draw() {
|
||||
// right
|
||||
if (position.x > 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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<peakHeights.length; i++){
|
||||
peakHeights[i] = random(height-40, height-30);
|
||||
}
|
||||
@@ -28,8 +29,7 @@ void setup(){
|
||||
display window, regardless of segment number. */
|
||||
float segs = segments;
|
||||
for (int i=0; i<segments; i++){
|
||||
ground[i] = new Ground(width/segs*i, peakHeights[i],
|
||||
width/segs*(i+1), peakHeights[i+1]);
|
||||
ground[i] = new Ground(width/segs*i, peakHeights[i], width/segs*(i+1), peakHeights[i+1]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,9 +40,17 @@ void draw(){
|
||||
fill(0, 15);
|
||||
rect(0, 0, width, height);
|
||||
|
||||
// Move and display the orb
|
||||
orb.move();
|
||||
orb.display();
|
||||
// Check walls
|
||||
orb.checkWallCollision();
|
||||
|
||||
// Check against all the ground segments
|
||||
for (int i=0; i<segments; i++){
|
||||
orb.checkGroundCollision(ground[i]);
|
||||
}
|
||||
|
||||
|
||||
// Draw ground
|
||||
fill(127);
|
||||
@@ -56,9 +64,6 @@ void draw(){
|
||||
endShape(CLOSE);
|
||||
|
||||
|
||||
for (int i=0; i<segments; i++){
|
||||
orb.checkGroundCollision(ground[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ Mover mover;
|
||||
|
||||
void setup() {
|
||||
size(640,360);
|
||||
smooth();
|
||||
mover = new Mover();
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user