public void checkCollision(Paddle[] pads) {

      // check against walls
      if (y >= height || y < 0) {
        speed.y *= -1;
      }
      if (x >= width) {
        restart();
        paddles[1].addPoint();
        timer = 40;
      }
      if (x < 0) {
        restart();
        paddles[0].addPoint();
        timer = 40;
      }
      // check against paddles
      for (int i = 0; i < pads.length; i++) {
        if (x > pads[i].x - (pads[i].w / 2)
            && x < pads[i].x + (pads[i].w / 2)
            && y > pads[i].y - (pads[i].h / 2)
            && y < pads[i].y + (pads[i].h / 2)) {
          speed.x *= -1;

          float friction = 0.5f;
          println(pads[i].vel.y);
          speed.y += friction * pads[i].vel.y;
        }
      }
    }
 // A function to rotate a vector
 public void rotateVector(PVector v, float theta) {
   float m = v.mag();
   float a = v.heading2D();
   a += theta;
   v.x = m * PApplet.cos(a);
   v.y = m * PApplet.sin(a);
 }
    public void update(float newY) {
      y = newY;
      vel.x = x - px;
      vel.y = y - py;

      px = x;
      py = y;
    }
Exemple #4
0
    public void wallCollisions() {

      // x
      if (x > (bounds.x / 2)) {
        velocity.x *= -1;
        //      velocity.x *= damping;
      }

      if (x < -(bounds.x / 2)) {
        velocity.x *= -1;
        //      velocity.x *= damping;
      }

      // y
      if (g) {
        velocity.y += gravity;
        if (y > bounds.y / 2) {
          velocity.y *= -0.95f;
          y = bounds.y / 2;
        }
      } else {
        if (y > (bounds.y / 2)) {
          velocity.y *= -1;
          velocity.y *= damping;
        }

        if (y < -(bounds.y / 2)) {
          velocity.y *= -1;
          velocity.y *= damping;
        }
      }

      // z
      if (z > (bounds.z / 2)) {
        velocity.z *= -1;
        //      velocity.z *= damping;
      }

      if (z < -(bounds.z / 2)) {
        velocity.z *= -1;
        //      velocity.z *= damping;
      }
    }
    public PVector getSmoothedVelocity() {

      PVector total = new PVector(0, 0, 0);
      for (int i = 0; i < buffer.length; i++) {
        total.x += buffer[i].x;
        total.y += buffer[i].y;
        total.z += buffer[i].z;
      }

      PVector smthd =
          new PVector(total.x / buffer.length, total.y / buffer.length, total.z / buffer.length);
      return smthd;
    }
    public void updateAll(PVector newPos) {
      // update current joint position
      currentPos = newPos;
      // map velocity to depth image size - I can't find how to get the depth of this context
      velocity.x = map(newPos.x - prevPos.x, -context.depthWidth(), context.depthWidth(), -1, 1);
      velocity.y = map(newPos.y - prevPos.y, -context.depthHeight(), context.depthHeight(), -1, 1);
      velocity.z = newPos.z - prevPos.z;

      // overwrite the circular buffer
      buffer[bufferIndex] = velocity;
      // and incrememnt the buffer index
      bufferIndex++;
      if (bufferIndex >= buffer.length) {
        bufferIndex = 0;
      }
      prevPos = newPos;
      smoothedVelocity = getSmoothedVelocity();
    }
Exemple #7
0
  public void mouseDragged() {
    if (paused) {
      ArrayList objects = timeline.getStatefulObjects();
      for (int i = 0; i < objects.size(); i++) {
        CelestialObject obj = (CelestialObject) objects.get(i);
        if (obj.isMouseOver()) {
          dragging = true;
          PVector pos = obj.getPosition();
          pos.x = mouseX;
          pos.y = mouseY;

          timeline.reset();
          timeline.setCurrentState(objects);
          sliderTimeline.setValue(0);
          break;
        }
      }
    }
  }
 // Wraparound
 public void borders() {
   if (loc.x < -r) loc.x = width + r;
   if (loc.y < -r) loc.y = height + r;
   if (loc.x > width + r) loc.x = -r;
   if (loc.y > height + r) loc.y = -r;
 }