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);
 }
Beispiel #3
0
 public void displayByArea(DisplayArea area) {
   changeMode();
   position.add(direction);
   if (position.z > 300 || position.z < -300) { // ograniczenie poruszania się obszaru wyświetlania
     direction.mult(-1);
   }
   area.moveCenterTo(position);
   pApplet.stroke(255, 0, 0);
   pApplet.pushMatrix();
   for (int j = 0; j < model.getSegmentCount(); j++) {
     Segment segment = model.getSegment(j);
     Face[] faces = segment.getFaces();
     pApplet.beginShape(PConstants.QUADS);
     for (Face face : faces) {
       if (area.isColliding(face.getCenter())) {
         PVector[] v = face.getVertices();
         PVector[] n = face.getNormals();
         for (int k = 0; k < v.length; k++) {
           pApplet.normal(n[k].x, n[k].y, n[k].z);
           pApplet.vertex(v[k].x, v[k].y, v[k].z);
         }
       }
     }
     pApplet.endShape();
   }
   pApplet.popMatrix();
 }
  public void draw(PApplet canvas, float scale) {
    PVector orient = PVector.fromAngle(orientation);
    orient.mult(scale * getRadius());
    float x = (float) position.x * scale;
    float y = (float) position.y * scale;
    float diameter = getRadius() * 2 * scale;

    canvas.fill(teamColor);
    canvas.stroke(0);
    canvas.ellipse(x, y, diameter, diameter);
    canvas.line(x, y, x + (float) orient.x, y + (float) orient.y);

    // Delegate Decoration to Robot
    float heading = orient.heading();
    float drawScale = 100f / scale * getRadius();

    canvas.translate(x, y);
    canvas.rotate(heading);
    canvas.scale(drawScale);
    // TODO: How to resolve scale, so that teams don't have to mind it also...
    decorateRobot(canvas);
    canvas.scale(1f / drawScale);
    canvas.rotate(-heading);
    canvas.translate(-x, -y);
  }
Beispiel #5
0
    // --------------------------------------------------------
    private void calculPositionAndAngle() {

      PVector prev = new PVector(0, 0);
      PVector next;

      for (int i = 0; i < n; i++) {

        next = new PVector();

        // Position
        if (i == 0) {
          next = new PVector(0, 0);
        } else {
          next.x = prev.x + cos(random(angleMini - HALF_PI, angleMax - HALF_PI)) * distance;
          next.y = prev.y + sin(random(angleMini - HALF_PI, angleMax - HALF_PI)) * distance;

          // Angle
          float a = atan2(prev.y - next.y, prev.x - next.x) - HALF_PI;
          angle.append(a);
          if (i == n - 1) lastAngle = a;
        }

        // Add position
        position.add(next);

        distance *= 1.01f;
        prev.set(next);
      }
    }
Beispiel #6
0
 // tällä funktiolla on huono nimi ja toteutuskin o ruma.
 // tämä on se juttu mikä tekee työt endpoints()ille.
 // jos tilepisteen reunalla toinen tile vain yhdessä suunnassa,
 // niin piste on yksinään. tähän tarttis jonkun kuvan selostamaan.
 PVector head(int x, int y, int z) {
   PVector pp = new PVector(x, y, z);
   PVector[] dirs = {
     new PVector(-1, 0, 0),
     new PVector(1, 0, 0),
     new PVector(0, -1, 0),
     new PVector(0, 1, 0),
     new PVector(0, 0, -1),
     new PVector(0, 0, 1),
   };
   int blkfound = -1;
   for (int i = 0; i < dirs.length; i++) {
     PVector p = dirs[i];
     PVector next = PVector.add(pp, p);
     if (next.x >= 0
         && next.x < size
         && next.y >= 0
         && next.y < size
         && next.z >= 0
         && next.z < size) {
       if (map[at(next)] != 0) {
         if (blkfound != -1) return null;
         blkfound = i;
       }
     }
   }
   if (blkfound != -1) return PVector.sub(pp, dirs[blkfound]);
   return null;
 }
    public void update(float newY) {
      y = newY;
      vel.x = x - px;
      vel.y = y - py;

      px = x;
      py = y;
    }
Beispiel #8
0
  /**
   * Converts a point from Spherical coordinates to Cartesian (using positive Z as up) and stores
   * the results in the store var.
   */
  public static PVector sphericalToCartesianZ(PVector sphereCoords, PVector store) {
    store.z = sphereCoords.x * FastMath.sin(sphereCoords.z);
    float a = sphereCoords.x * FastMath.cos(sphereCoords.z);
    store.x = a * FastMath.cos(sphereCoords.y);
    store.y = a * FastMath.sin(sphereCoords.y);

    return store;
  }
 /** {@inheritDoc} */
 public T updateAbsolutePosition() {
   absolutePosition.set(position);
   absolutePosition.add(_myParent.getAbsolutePosition());
   for (int i = 0; i < controllers.size(); i++) {
     controllers.get(i).updateAbsolutePosition();
   }
   return me;
 }
 // Change robot's front/back left/right speed
 public void setSpeed(float x, float y) {
   if (Float.isNaN(x) || Float.isNaN(y)) {
     // System.out.println("Excep: setSpeed "+x+" "+y);
     return;
   }
   targetSpeed.set(x, y);
   targetSpeed.limit(maxSpeed);
 }
 // Method to update location
 public void update() {
   // Update velocity
   vel.add(acc);
   // Limit speed
   vel.limit(maxspeed);
   loc.add(vel);
   // Reset accelertion to 0 each cycle
   acc.mult(0);
 }
Beispiel #12
0
 public void update(float theDeltaTime, IBehaviorParticle pParent) {
   mForce.set(0, 0, 0);
   if (mNeighbors != null) {
     ArrayList<ProximityStructure> mCloseNeighbors =
         ProximityStructure.findProximityEntities(pParent, mNeighbors, mProximity);
     findAwayVector(mCloseNeighbors, mForce);
     mForce.mult(weight());
   }
 }
  public void setBounds(PVector i_topLeft, PVector i_bottomRight) {
    topLeftBound = i_topLeft.get();
    bottomRightBound = i_bottomRight.get();

    TL = topLeftBound.get();
    BR = bottomRightBound.get();
    BL = new PVector(TL.x, BR.y);
    TR = new PVector(BR.x, TL.y);
  }
Beispiel #14
0
  public void addVertex(float x, float y, float z, float u, float v) {
    PVector vert = new PVector(x, y, z);
    PVector texCoord = new PVector(u, v);

    PVector vertNorm = PVector.div(vert, vert.mag());

    vertices.add(vert);
    texCoords.add(texCoord);
    normals.add(vertNorm);
  }
 public void draw() {
   background(255);
   mouse.sub(mover.location);
   mouse.normalize();
   mouse.mult(5);
   //		mover.applyForce(wind);
   mover.applyForce(mouse);
   mover.checkEdges();
   mover.update();
   mover.display();
 }
Beispiel #16
0
 /**
  * Converts a point from Cartesian coordinates (using positive Z as up) to Spherical and stores
  * the results in the store var. (Radius, Azimuth, Polar)
  */
 public static PVector cartesianZToSpherical(PVector cartCoords, PVector store) {
   if (cartCoords.x == 0) cartCoords.x = FastMath.FLT_EPSILON;
   store.x =
       FastMath.sqrt(
           (cartCoords.x * cartCoords.x)
               + (cartCoords.y * cartCoords.y)
               + (cartCoords.z * cartCoords.z));
   store.z = FastMath.atan(cartCoords.z / cartCoords.x);
   if (cartCoords.x < 0) store.z += FastMath.PI;
   store.y = FastMath.asin(cartCoords.y / store.x);
   return store;
 }
    public void update(float db) {
      // we can use the db value here to change the speed of the particles
      loc.add(vel.mult(vel, map(db, -100, 100, .05f, 5)));
      if (loc.x < 0 || loc.x > width) {
        vel.x *= -1;
      }
      if (loc.y < 0 || loc.y > height) {
        vel.y *= -1;
      }

      radius = constrain(db, 2, 100);
    }
Beispiel #18
0
 /**
  * @param target Set to null to create a new vector
  * @return a new vector (if target was null), or target
  */
 public PVector normalize(PVector target) {
   if (target == null) {
     target = new PVector();
   }
   float m = mag();
   if (m > 0) {
     target.set(x / m, y / m, z / m);
   } else {
     target.set(x, y, z);
   }
   return target;
 }
Beispiel #19
0
  public int compare(Particle p1, Particle p2) {
    float val1 = PVector.dist(p1.pos, arbitraryPos);
    float val2 = PVector.dist(p2.pos, arbitraryPos);

    if (val1 < val2) {
      return -1;
    }
    if (val1 > val2) {
      return 1;
    }
    return 0;
  }
 /**
  * Calculate a force to push particle away from repeller
  *
  * @param ptcl the Particle to push
  * @return PVector direction
  */
 public PVector pushParticle(Particle ptcl) {
   PVector dir = PVector.sub(getLoc(), ptcl.getLoc()); // Calculate direction of
   // force
   float d = dir.mag(); // Distance between objects
   dir.normalize(); // Normalize vector (distance doesn't matter here, we
   // just want this vector for direction)
   d = PApplet.constrain(d, 5, 100); // Keep distance within a reasonable
   // range
   float force = -1 * G / (d * d); // Repelling force is inversely
   // proportional to distance
   dir.mult(force); // Get force vector --> magnitude * direction
   return dir;
 }
    public void display() {
      location.x = (sin(radians(angle)) * r) + 5;
      location.y = cos(radians(angle)) * r;

      pushMatrix();
      translate(location.x, location.y, 0);
      fill(220);
      noStroke();
      sphere(size);
      popMatrix();

      angle += speed;
    }
Beispiel #22
0
    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;
    }
Beispiel #23
0
  private PVector getNormal(PVector[] triangle) {

    /*
     *
     * v1=A-B, y
     * v2=A-C
     * y un vector perpendicular al tri‡ngulo (no tiene porquŽ ser œnico) es el propucto cruz entre estos vectores:
     * n = (v1 x v2)
     *
     */

    PVector normal =
        PVector.sub(triangle[2], triangle[1]).cross(PVector.sub(triangle[1], triangle[0]));
    return normal;
  }
Beispiel #24
0
    Background(float _sizeX, float _sizeY, Repulseur[] _r) {

      sizeX = _sizeX;
      sizeY = _sizeY;
      repulseurPoint = _r;

      for (int i = 0; i < 20000; i++) {

        PVector p = new PVector();
        p.x = random(sizeX);
        p.y = random(sizeY);

        points.add(p);
      }
    }
Beispiel #25
0
 @Override
 public void mouseWheelMoved(MouseWheelEvent e) {
   if (camDist > 1.0) {
     camDist += (0.5f * camDist) * zoomFactor * e.getWheelRotation();
     PVector newMouse = getNewMouse();
     //			PVector toAdd = new PVector(newMouse.x - p.width*0.5f,newMouse.y - p.height*0.5f);
     PVector toAdd = new PVector(newMouse.x, newMouse.y);
     toAdd.sub(new PVector(camCenter.x, camCenter.y));
     toAdd.mult(-0.05f * e.getWheelRotation());
     camPos.add(toAdd);
     //			camPos.mult(0.5f);
     camCenter.add(toAdd);
     //			camCenter.mult(0.5f);
   } else camDist = 1.01f;
 }
 // Function to update location
 public void update() {
   // As long as we aren't dragging the pendulum, let it swing!
   if (!dragging) {
     float G = 0.4f; // Arbitrary universal gravitational constant
     theta_acc =
         (-1 * G / r)
             * sin(
                 theta); // Calculate acceleration (see:
                         // http://www.myphysicslab.com/pendulum1.html)
     theta_vel += theta_acc; // Increment velocity
     theta_vel *= damping; // Arbitrary damping
     theta += theta_vel; // Increment theta
   }
   loc.set(r * sin(theta), r * cos(theta), 0); // Polar to cartesian conversion
   loc.add(origin); // Make sure the location is relative to the pendulum's origin
 }
Beispiel #27
0
  public void createParticle(int x, int y) {
    particles.add(
        new LingerParticle(
            this,
            new PVector(x, y),
            PVector.mult(new PVector(random(-0.5F, 0.5F), random(-2F, -1.5F)), TIME_SCALE_FACTOR),
            (int) (100 * timeScaleInverted),
            0.05 * TIME_SCALE_FACTOR,
            0.5,
            (int) (10 * timeScaleInverted),
            color(32, 64, 32),
            4,
            (int) (240 * timeScaleInverted),
            (int) (300 * timeScaleInverted)));

    /*
    float speed = 0;
    if ((int)(random(0,2))==0) speed = random(-5,-1); else speed = random(1,5);
    if ((int)(random(0,2))==0)
    	particles.add(new LingerParticle(this, new PVector(x,y), new PVector(speed,0),
                300, 0.05, 0.5, 10, color(32,32,32),4,120,300));
    else
    	particles.add(new LingerParticle(this, new PVector(x,y), new PVector(speed,0),
                   300, 0.05, 0.5, 10, color(32,32,32),4,120,300));*/
  }
Beispiel #28
0
    // ------------------------------------------------------
    public PVector updatedPointPosition(float _x, float _y) {

      p = new PVector(_x, _y);
      distance = dist(p.x, p.y, x, y);

      if (distance < radius) {

        angle = atan2(p.y - y, p.x - x);
        force = map(distance, 0, radius, maxForce, 0);

        p.x += cos(angle) * force;
        p.y += sin(angle) * force;
      }

      return p;
    }
  void setParent(ControllerGroup<?> theParent) {
    if (_myParent != null && _myParent != this) {
      _myParent.remove(this);
    }

    _myParent = theParent;

    if (_myParent != this) {
      _myParent.add(this);
    }
    absolutePosition = new PVector(position.x, position.y);

    absolutePosition.add(_myParent.absolutePosition);

    positionBuffer = new PVector(position.x, position.y);

    _myControlWindow = _myParent.getWindow();

    for (int i = 0; i < controllers.size(); i++) {
      if (controllers.get(i) instanceof Controller<?>) {
        ((Controller<?>) controllers.get(i))._myControlWindow = _myControlWindow;
      } else {
        ((ControllerGroup<?>) controllers.get(i))._myControlWindow = _myControlWindow;
      }
    }
    if (_myControlWindow != null) {
      setMouseOver(false);
    }
  }
  public void mousePressed() {
    if (game.screen == 1) { // see above
      velocity =
          PVector.fromAngle(
              radians(
                  (float)
                      angledegrees)); // uses PVector to make a vector from the angle of the shooter
      if (canfire == true) { // checks if user is able to fire (if ball is active)
        game.shooterlist[0] = game.shooterlist[1]; // sets active ball to the next ball
        game.shooterlist[0].setVelocity(
            velocity.x, velocity.y); // adjusts velocity for ball depending on type
        game.shooterlist[1] =
            new Ball(
                game
                    .createNewBall()); // creates new ball with random type as generated by the
                                       // createNewBall function
        canfire = false; // disables firing while ball is active
      }
    }

    if (game.screen == 0) { // see above
      if (button.pressed()) { // checks if button is pressed
        game.gotoGame(); // loads game
      }
    }

    if (game.screen == 2) {
      if (button.pressed()) {
        game.reset(); // resets game
      }
    }
  }