double buffering
in
Programming Questions
•
5 months ago
May I ask how the following script can be revised using double-buffering technique so that it can run more smoothly when a far larger amount of points (say, 10000) are generated ? especially how this technique should be applied on each mover object's display method level?
Thanks!
code reference: Daniel Shiffman's The Nature of Code exercise_2_1
Thanks!
code reference: Daniel Shiffman's The Nature of Code exercise_2_1
- // keys:
- // [space] : toggle animation
- Mover[] movers = new Mover[100];
- boolean animationToggle = false;
- void setup(){
- size(700,350);
- for(int i=0; i<movers.length; i++){
- movers[i] = new Mover(random(0.5,3),random(0,width),random(0,height),
- color(floor(random(255)),floor(random(255)),floor(random(255)))
- );
- }
- }
- void draw() {
- PVector wind = new PVector(0.01,0);
- PVector gravity = new PVector(0,0.1);
- if (animationToggle == true){
- fill(0,10);
- rect(0,0,width,height);
- 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;
- Mover(float m_, float x_, float y_, color c_) {
- location = new PVector(x_,y_);
- velocity = new PVector(0,0);
- acceleration = new PVector(0,0);
- mass = m_;
- c = c_;
- }
- void applyForce(PVector force){
- PVector f = PVector.div(force,mass);
- acceleration.add(f);
- }
- void update() {
- velocity.add(acceleration);
- location.add(velocity);
- acceleration.mult(0);
- }
- void display() {
- noStroke();
- fill(c);
- ellipse(location.x,location.y,mass*10,mass*10);//scale by mass
- }
- 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);
- }
- }
1