21. November 2011
Man kann indem man die Maus über den Bildschirm zieht neue Formen aufziehen. Eine Achse bewegt sich in die Richtung in die man die Maus gezogen hat und prallt bei anstoss an die Wand wieder ab. Ich habe viel mit den Parametern gespiel, schnell wird es aber zur unordnung (Screensot 1).ArrayList balls; PVector p1 = new PVector(); PVector p2 = new PVector(); boolean drag = false; void setup() { size(800, 800); balls = new ArrayList(); // Create an empty ArrayList balls.add(new BouncingBall(48, 48, width/2, height/2,23,234,12)); // Start by adding one element shapeMode(CENTER); smooth(); } void draw() { // hintergrund loeschen background(0); // ghosting fill(0, 0, 0, 60); rect(0, 0, width, height); for (int i = balls.size()-1; i >= 0; i--) { // An ArrayList doesn't know what it is storing so we have // to cast the object coming out BouncingBall ball = (BouncingBall) balls.get(i); ball.draw(); if (drag) { // zeichne die abschussrichtung line(p1.x, p1.y, mouseX, mouseY); } } } void mousePressed() { drag = true; p1.set(mouseX, mouseY, 0); } void mouseReleased() { drag=false; // A new ball object is added to the ArrayList, by default to the end PVector dir = PVector.sub(new PVector(mouseX, mouseY), p1); // laenge verkuerzen dir.mult(.09); float rand = random(0, 300); float randR = random(0, 255); float randG = random(0, 255); float randB = random(0, 255); if (mouseX>400){ BouncingBall temp = new BouncingBall(rand, rand, mouseX, mouseY, mouseX, mouseY, randB); temp.set(p1, dir, 0.993); balls.add(temp); } else{ BallEx temp = new BallEx(rand, rand, mouseX, mouseY, mouseX, mouseY, randB); temp.set(p1, dir, 0.993); balls.add(temp); } }Code: Klasse
class BouncingBall { PVector _pos; PVector _dir; float _dampV; PShape _shape; float _w; float _h; float _posiX; float _posiY; float _randR; float _randG; float _randB; float _rand; // konstruktor BouncingBall(float shapeWidth,float shapeHeight,float posiX, float posiY, float randR, float randG,float randB) { _pos = new PVector(width/2, height/2); _dir = new PVector(0,0); _dampV = 1; _w = shapeWidth; _h = shapeHeight; _pos.x = posiX; _pos.y = posiY; _randR = randR; _randG = randG; _randB = randB; } // setzt die neue pos + richtung + daempfung void set(PVector pos,PVector dir,float dampV) { _pos = pos.get(); _dir.add(dir); _dampV = dampV; } // erneuert die aktuelle position void calcPos() { // aktuelle position verschieben _pos.add(_dir); // bewegungs vektor veraendert _dir.mult(_dampV); // teste horizontal if(_pos.x + _w/2 > width) { _dir.x *= -1; _pos.x = width - _w/2; } else if(_pos.x - _w/2 < 0) { _dir.x *= -1; _pos.x = _w/2; } // teste vertikal if(_pos.y + _h/2 > height) { _dir.y *= -1; _pos.y = height - _w/2; } else if(_pos.y - _h/2 < 0) { _dir.y *= -1; _pos.y = _h/2; } } // zeichnet den ball void draw() { calcPos(); noStroke(); //fill(_randR,_randG,_randB,50); //ellipse(_pos.x,_pos.y,_w,_h); fill(255,255,255,50); beginShape(); vertex(_pos.x,_pos.y); vertex(300,300); vertex(500,500); //_pos.y,_pos.x endShape(CLOSE); } } class BallEx extends BouncingBall { BallEx(float shapeWidth,float shapeHeight,float posiX, float posiY, float randR, float randG,float randB) { super(shapeWidth,shapeHeight,posiX,posiY,randR,randG,randB); } void draw() { calcPos(); fill(100,_randR,_randB,50); beginShape(); vertex(_pos.x,_pos.y); //23,89 / 500,100 vertex(300,300); vertex(_pos.y,_pos.x); vertex(265,45); endShape(CLOSE); } }