Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

Pink Ball

28. Oktober 2011

Die Verwendung von Klassen ermöglicht das mehrfache Verwenden von Objekten.
BouncingBall ball1;
BouncingBall ball2;
ArrayList balls = new ArrayList();


PVector      p1 = new PVector();
PVector      p2 = new PVector();
boolean      drag = false;

void setup() {
  
  size(800,600); 

  
  // svg bild soll zentriert sein
  shapeMode(CENTER);
  smooth();
  
}


void draw() {
  
  // ghosting
  fill(255,255,255,60);
  rect(0,0,width,height);
  
  if (drag) { 
    // zeichne die abschussrichtung
    line(p1.x,p1.y,p2.x,p2.y);
  }
  
  // zeichne den ball
  for (int i = balls.size()-1; i >= 0; i--) {
    
    BouncingBall ball = (BouncingBall)balls.get(i);
    ball.draw();
    
  }

}


void mousePressed() {
  drag = true;
  p1.set(mouseX,mouseY,0);
  p2.set(mouseX,mouseY,0);
}


void mouseDragged() {
  p2.set(mouseX,mouseY,0);
}


void mouseReleased() {
  
  drag = false;
  
  // abschuss staerke berechnen
  PVector dir = PVector.sub(p2,p1);
  // laenge verkuerzen
  dir.mult(.09);
  
  // der ball wird neu ausgerichtet
  int tempSize = (int)random(50, 200);
  BouncingBall tempBall = new BouncingBall(tempSize, tempSize);
  tempBall.set(new PVector(mouseX,mouseY),dir,0.995);
  balls.add(tempBall);
  
}
  
/* --------------------------------------------------------------------------
 * BouncingBall
 * --------------------------------------------------------------------------
 * prog:  Max Rheiner 
 * date:  03/1/2011 (m/d/y)
 * ----------------------------------------------------------------------------
 */
 
class BouncingBall
{
  PVector _pos;
  PVector _dir;
  float   _dampV;
  PImage  _shape;
  int     _w;
  int     _h;
  
  // konstruktor
  BouncingBall(int shapeWidth,int shapeHeight)
  {
    _pos = new PVector(width/2, height/2);
    _dir = new PVector(0,0);
    _dampV = 1;
    
    _w = shapeWidth;
    _h = shapeHeight;
    
    _shape = loadImage("pink_ball.png");
  }
  
  // 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();
     image(_shape,
          _pos.x,_pos.y,
          _w,_h);
  }
}
download script