Skip to content

Commit

Permalink
Initial commit. This is essentially the code Yushuo, expect I moved t…
Browse files Browse the repository at this point in the history
…he Path and Vehicle classes into their own files.
  • Loading branch information
rjm11010 committed Apr 30, 2018
0 parents commit 4825e7b
Show file tree
Hide file tree
Showing 3 changed files with 476 additions and 0 deletions.
43 changes: 43 additions & 0 deletions path.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Path {

// A Path is an arraylist of points (PVector objects)
ArrayList<PVector> points;
// A path has a radius, i.e how far is it ok for the boid to wander off
float radius;

Path() {
// Arbitrary radius of 20
radius = 30;
points = new ArrayList<PVector>();
}

// Add a point to the path
void addPoint(float x, float y) {
PVector point = new PVector(x, y);
points.add(point);
}

// Draw the path
void display() {
strokeJoin(ROUND);

// Draw thick line for radius
stroke(175);
strokeWeight(radius*2);
noFill();
beginShape();
for (PVector v : points) {
vertex(v.x, v.y);
}
endShape(CLOSE);
// Draw thin line for center of path
stroke(0);
strokeWeight(1);
noFill();
beginShape();
for (PVector v : points) {
vertex(v.x, v.y);
}
endShape(CLOSE);
}
}
80 changes: 80 additions & 0 deletions simulation.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

boolean debug = true;

// A path object (series of connected points)
Path path;

// Two vehicles
Vehicle car1;
Vehicle car2;

void setup()
{
size(1000, 1000);
// Call a function to generate new Path object
newPath();

// Each vehicle has different maxspeed and maxforce for demo purposes
car1 = new Vehicle(new PVector(0, height/2), 1, 0.04);
car2 = new Vehicle(new PVector(0, height/2), 1, 2);
}

void draw()
{
background(255);
// Display the path
path.display();
// The boids follow the path
car1.follow(path);
car2.follow(path);
// Call the generic run method (update, borders, display, etc.)
car1.run();
car2.run();

//car1.borders(path);
//car2.borders(path);
//car1.borders();
//car2.borders();
// Instructions
fill(0);

}

// Here define the element of the track
/*void newPath()
{
// A path is a series of connected points
// A more sophisticated path might be a curve, if you want to have a try.
path = new Path();
path.addPoint(100, height/2);
path.addPoint(100,height/2+200);
path.addPoint(300,height/2+200);
path.addPoint(300,height/2);
// path.addPoint(200, height/2+5);
}*/


void newPath() {
// A path is a series of connected points
// A more sophisticated path might be a curve
path = new Path();
float offset = 30;
path.addPoint(offset,offset);
path.addPoint(width-offset,offset);
path.addPoint(width-offset,height-offset);
path.addPoint(width/2,height-offset*3);
path.addPoint(offset,height-offset);
}


public void keyPressed()
{
if (key == ' ') {
debug = !debug;
}
}

public void mousePressed()
{
newPath();
}
Loading

0 comments on commit 4825e7b

Please sign in to comment.