failed: draw trail for each of a group of bouncing points using begainShape/endShape
in
Programming Questions
•
5 months ago
I'd like to draw the trail for each of a group of bouncing points, but I can't get it work by using begainShape/endShape. No continuous line segments are created, instead points are still shown as individual points.
I assume the problem is related to the arraylist to store position history ...
Advices are appreciated!
reference for drawing bouncing points: "The Nature of Code" Exercise 2.1
reference for drawing trail from "Form and Code":
http://formandcode.com/code-examples/simulate-particles
code: (pls hit the "p" key after lunching the code to switch to line mode)
I assume the problem is related to the arraylist to store position history ...
Advices are appreciated!
reference for drawing bouncing points: "The Nature of Code" Exercise 2.1
reference for drawing trail from "Form and Code":
http://formandcode.com/code-examples/simulate-particles
code: (pls hit the "p" key after lunching the code to switch to line mode)
- // keys:
- // [space] : toggle animation
- // [p] : toggle point or line
- Mover[] movers = new Mover[50];
- boolean animationToggle = false;
- boolean displayPt = true;
- void setup(){
- size(700,350);
- for(int i=0; i<movers.length; i++){
- movers[i] = new Mover(random(0.5,3),
- new PVector(random(0,width), random(0,height/2)),
- color(floor(random(255)),floor(random(255)),floor(random(255)))
- );
- }
- }
- void draw() {
- PVector wind = new PVector(0.03,0);
- PVector gravity = new PVector(0,0.4);
- if (animationToggle == true){
- //___op1: with tail
- //fill(0,10);
- //rect(0,0,width,height);
- //___op2: no tail
- //background(0);
- for(int i=0; i<movers.length; i++){
- movers[i].applyForce(wind);
- movers[i].applyForce(gravity);
- movers[i].update();
- movers[i].display();
- movers[i].checkEdges();
- }
- }
- }
- class Mover{
- PVector location;
- PVector velocity;
- PVector acceleration;
- float mass;
- color c;
- ArrayList<PVector> hist; //array list to store position history
- int cnt = 0; //counter
- Mover(float m_, PVector loc_, color c_) {
- location = new PVector(loc_.x, loc_.y);
- velocity = new PVector(0,0);
- acceleration = new PVector(0,0);
- mass = m_;
- c = c_;
- hist = new ArrayList<PVector>();
- hist.add(location);
- }
- void applyForce(PVector force){
- PVector f = PVector.div(force,mass);
- acceleration.add(f);
- }
- void update() {
- velocity.add(acceleration);
- location.add(velocity);
- acceleration.mult(0);
- // //_____op1: save location every 10 frames
- // if (frameCount % 10 == 0 && cnt<hist.size()) {
- // hist.add(location);
- // cnt++;
- // }
- //_____op2: save location every frames
- hist.add(location);
- }
- void display() {
- noStroke();
- fill(c);
- if (displayPt == true){// draw pt
- ellipse(location.x,location.y,mass*10,mass*10);//scale by mass
- } else {// draw line
- noFill();
- stroke(255);
- strokeWeight(3);
- beginShape();
- for (int i=0; i<hist.size(); i++) {
- vertex(hist.get(i).x, hist.get(i).y);
- }
- vertex(location.x, location.y);
- endShape();
- }
- }
- void checkEdges() {
- if (location.x > (width-mass*5)) {
- location.x = width-mass*5;
- velocity.x *= -1;
- } else if (location.x < (0-mass*5)) {
- velocity.x *= -1;
- location.x = mass*5;
- }
- if (location.y > (height-mass*5)) {
- velocity.y *= -1;
- location.y = height-mass*5;
- }
- }
- }
- void keyPressed(){
- if ( key == ' ' ) {
- animationToggle = (animationToggle == true) ? (false):(true);
- }
- if ( key == 'p' ) {
- displayPt = (displayPt == true) ? (false):(true);
- }
- }
1