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.

Lektion 8: BouncingBall

28. Oktober 2011

Für den Ball verwendete ich ein .svg-file, welches eine Spirale in Form eines Balles darstellt. Für diese Spirale entschied ich mich, da es dadurch spannende Überlagerungen der Bälle und interessante "ghosting-Effekte" gibt. Per MousePressed wird eine Linie gezogen, sodass man gut zielen kann, wohin der Ball fliegen soll. Per MouseReleased wird der Ball geschossen. Dieser Vorgang kann immer wiederholt werden. Dafür erstellte ich eine Klasse namens BouncingBall.           
<pre>PVector      p1 = new PVector();
PVector      p2 = new PVector();
boolean      drag = false;

ArrayList balls;

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

  balls = new ArrayList();

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

void draw()
{
  // hintergrund loeschen
  //background(255);
  // ghosting
  fill(255,255,255,60);
  rect(0,0,width,height);

  for(int i = 0; i < balls.size(); i++)
  {
    BouncingBall ball = (BouncingBall) balls.get(i);
    // zeichne die abschussrichtung
    ball.draw();
  }
  if (drag)
  {
    line(p1.x,p1.y,p2.x,p2.y);
  }
  /*
  // zeichne den ball
  ball1.draw();
  ball2.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);

  BouncingBall ball = new BouncingBall(60,60);
  balls.add(ball);
  ball.set(p1,dir,.99);
  // der ball wird neu ausgerichtet
//  ball1.set(p1,dir,.993);
//  ball2.set(p1,dir,.95);
}
Klasse BouncingBall:
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;
    imageMode(CENTER);
    _shape = loadImage("ball_spirale.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();
     imageMode(CENTER);
     image(_shape,
          _pos.x,_pos.y,
          _w,_h);
  }
}