Esempio n. 1
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;
 }
Esempio n. 2
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);
      }
    }
 public void draw() {
   stroke(0, 0, 0);
   change = round(random(1, 10));
   if (change == 10) {
     direction = round(random(0, 8));
     point(pointX, pointY);
     point(pointX + 1, pointY + 1);
     point(pointX - 1, pointY - 1);
     point(pointX - 1, pointY + 1);
     point(pointX + 1, pointY - 1);
     stroke(200, 200, 200);
     point(pointX + 1, pointY);
     point(pointX - 1, pointY);
     point(pointX, pointY + 1);
     point(pointX, pointY - 1);
     stroke(0, 0, 0);
   }
   if (direction == 1) {
     speed.set(0, -1, 0);
   }
   if (direction == 2) {
     speed.set(1, 0, 0);
   }
   if (direction == 3) {
     speed.set(0, 1, 0);
   }
   if (direction == 4) {
     speed.set(-1, 0, 0);
   }
   if (direction == 5) {
     speed.set(-1, -1, 0);
   }
   if (direction == 6) {
     speed.set(1, 1, 0);
   }
   if (direction == 7) {
     speed.set(-1, 1, 0);
   }
   if (direction == 8) {
     speed.set(1, -1, 0);
   }
   if (pointX == width) {
     pointX = 1;
   }
   if (pointX == 0) {
     pointX = width - 1;
   }
   if (pointY == height) {
     pointY = 1;
   }
   if (pointY == 0) {
     pointY = height - 1;
   }
   pointX = pointX + speed.x;
   pointY = pointY + speed.y;
   point(pointX, pointY);
 }
Esempio n. 4
0
 // 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);
 }
 /** {@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;
 }
Esempio n. 6
0
 public static PVector mult(PVector v1, PVector v2, PVector target) {
   if (target == null) {
     target = new PVector(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
   } else {
     target.set(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
   }
   return target;
 }
Esempio n. 7
0
 public static PVector div(PVector v1, PVector v2, PVector target) {
   if (target == null) {
     target = new PVector(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z);
   } else {
     target.set(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z);
   }
   return target;
 }
Esempio n. 8
0
 /**
  * Subtract one vector from another and store in another vector
  *
  * @param v1 the x, y, and z components of a PVector object
  * @param v2 the x, y, and z components of a PVector object
  * @param target PVector to store the result
  */
 public static PVector sub(PVector v1, PVector v2, PVector target) {
   if (target == null) {
     target = new PVector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
   } else {
     target.set(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
   }
   return target;
 }
Esempio n. 9
0
 /**
  * Divide a vector by a scalar and store the result in another vector.
  *
  * @param v any variable of type PVector
  * @param n the number to divide with the vector
  * @param target PVector to store the result
  */
 public static PVector div(PVector v, float n, PVector target) {
   if (target == null) {
     target = new PVector(v.x / n, v.y / n, v.z / n);
   } else {
     target.set(v.x / n, v.y / n, v.z / n);
   }
   return target;
 }
Esempio n. 10
0
 /**
  * Multiply a vector by a scalar, and write the result into a target PVector.
  *
  * @param target PVector to store the result
  */
 public static PVector mult(PVector v, float n, PVector target) {
   if (target == null) {
     target = new PVector(v.x * n, v.y * n, v.z * n);
   } else {
     target.set(v.x * n, v.y * n, v.z * n);
   }
   return target;
 }
Esempio n. 11
0
 /**
  * Add two vectors into a target vector
  *
  * @param target the target vector (if null, a new vector will be created)
  */
 public static PVector add(PVector v1, PVector v2, PVector target) {
   if (target == null) {
     target = new PVector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
   } else {
     target.set(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
   }
   return target;
 }
Esempio n. 12
0
 public void update(float theDeltaTime, IBehaviorParticle pParent) {
   if (mActive) {
     /* 2D warning -- ignoring z-axis for now */
     PVector mDirection = teilchen.util.Util.clone(pParent.velocity());
     if (teilchen.util.Util.lengthSquared(mDirection) > 0) {
       mDirection.normalize();
       PVector r = new PVector();
       PVector.cross(mDirection, mUPVector, r);
       mDirection.set(r);
       mDirection.mult(mSteering);
       mForce.set(mDirection);
     } else {
       mForce.set(0, 0, 0);
     }
   } else {
     mForce.set(0, 0, 0);
   }
 }
Esempio n. 13
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());
   }
 }
Esempio n. 14
0
 /**
  * a Tooltip is activated when the mouse enters a controller.
  *
  * @param theController
  */
 protected void activate(Controller<?> theController) {
   if (map.containsKey(theController)) {
     startTime = System.nanoTime();
     _myController = theController;
     currentPosition.set(
         theController.getControlWindow().mouseX, theController.getControlWindow().mouseY, 0);
     updateText(map.get(_myController));
     _myMode = ControlP5.WAIT;
   }
 }
Esempio n. 15
0
 private static void findAwayVector(
     ArrayList<ProximityStructure> mCloseNeighbors, final PVector pForce) {
   /* find away vector */
   if (!mCloseNeighbors.isEmpty()) {
     pForce.set(0, 0, 0);
     /**
      * @todo the vectors could be weighted according to distance: 1.0 - distance ( for example )
      */
     for (ProximityStructure p : mCloseNeighbors) {
       pForce.add(p.distanceVec);
     }
     pForce.mult(1.0f / mCloseNeighbors.size());
     pForce.normalize();
     if (isNaN(pForce)) {
       pForce.set(0, 0, 0);
     }
   } else {
     pForce.set(0, 0, 0);
   }
 }
Esempio n. 16
0
  /**
   * @param v any variable of type PVector
   * @param target PVector to store the result
   */
  public PVector cross(PVector v, PVector target) {
    float crossX = y * v.z - v.y * z;
    float crossY = z * v.x - v.z * x;
    float crossZ = x * v.y - v.x * y;

    if (target == null) {
      target = new PVector(crossX, crossY, crossZ);
    } else {
      target.set(crossX, crossY, crossZ);
    }
    return target;
  }
Esempio n. 17
0
  /**
   * @param v1 any variable of type PVector
   * @param v2 any variable of type PVector
   * @param target PVector to store the result
   */
  public static PVector cross(PVector v1, PVector v2, PVector target) {
    float crossX = v1.y * v2.z - v2.y * v1.z;
    float crossY = v1.z * v2.x - v2.z * v1.x;
    float crossZ = v1.x * v2.y - v2.x * v1.y;

    if (target == null) {
      target = new PVector(crossX, crossY, crossZ);
    } else {
      target.set(crossX, crossY, crossZ);
    }
    return target;
  }
Esempio n. 18
0
 /**
  * a Tooltip is activated when the mouse enters a controller.
  *
  * @param theController
  */
 protected void activate(Controller<?> theController) {
   if (map.containsKey(theController)) {
     startTime = System.nanoTime();
     controller = theController;
     currentPosition.set(
         theController.getControlWindow().getPointerX(),
         theController.getControlWindow().getPointerY(),
         0);
     updateText(map.get(controller));
     mode = Skatolo.WAIT;
   }
 }
Esempio n. 19
0
  /** Initializes the preview applet layout according to the graph's dimension. */
  private void initAppletLayout() {
    //            graphSheet.setMargin(MARGIN);
    if (model != null && model.getDimensions() != null && model.getTopLeftPosition() != null) {

      // initializes zoom
      Dimension dimensions = model.getDimensions();
      Point topLeftPostition = model.getTopLeftPosition();
      PVector box = new PVector((float) dimensions.getWidth(), (float) dimensions.getHeight());
      float ratioWidth = width / box.x;
      float ratioHeight = height / box.y;
      scaling = ratioWidth < ratioHeight ? ratioWidth : ratioHeight;

      // initializes move
      PVector semiBox = PVector.div(box, 2);
      PVector topLeftVector = new PVector((float) topLeftPostition.x, (float) topLeftPostition.y);
      PVector center = new PVector(width / 2f, height / 2f);
      PVector scaledCenter = PVector.add(topLeftVector, semiBox);
      trans.set(center);
      trans.sub(scaledCenter);
      lastMove.set(trans);
    }
  }
 // 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
 }
Esempio n. 21
0
 /** @exclude {@inheritDoc} */
 @ControlP5.Invisible
 public T updateEvents() {
   if (isOpen) {
     for (int i = controllers.size() - 1; i >= 0; i--) {
       ((ControllerInterface<?>) controllers.get(i)).updateEvents();
     }
   }
   if (isVisible) {
     if ((isMousePressed == _myControlWindow.mouselock)) {
       if (isMousePressed && cp5.keyHandler.isAltDown() && isMoveable) {
         if (!cp5.isMoveable) {
           positionBuffer.x += _myControlWindow.mouseX - _myControlWindow.pmouseX;
           positionBuffer.y += _myControlWindow.mouseY - _myControlWindow.pmouseY;
           if (cp5.keyHandler.isShiftDown) {
             position.x = ((int) (positionBuffer.x) / 10) * 10;
             position.y = ((int) (positionBuffer.y) / 10) * 10;
           } else {
             position.set(positionBuffer);
           }
           updateAbsolutePosition();
         }
       } else {
         if (isInside) {
           setMouseOver(true);
         }
         if (inside()) {
           if (!isInside) {
             isInside = true;
             onEnter();
             setMouseOver(true);
           }
         } else {
           if (isInside && !isMousePressed) {
             onLeave();
             isInside = false;
             setMouseOver(false);
           }
         }
       }
     }
   }
   return me;
 }
Esempio n. 22
0
  public void getFaceMapInfraredData() {
    ArrayList<FaceData> faceData = kinect.getFaceData();

    for (int i = 0; i < faceData.size(); i++) {
      FaceData faceD = faceData.get(i);
      if (faceD.isFaceTracked()) {
        PVector[] facePointsInfrared = faceD.getFacePointsInfraredMap();

        KRectangle rectFace = faceD.getBoundingRectInfrared();

        FaceFeatures[] faceFeatures = faceD.getFaceFeatures();

        PVector nosePos = new PVector();
        noStroke();

        int col = getIndexColor(i);

        fill(col);
        for (int j = 0; j < facePointsInfrared.length; j++) {
          if (j == KinectPV2.Face_Nose)
            nosePos.set(facePointsInfrared[j].x, facePointsInfrared[j].y);

          ellipse(facePointsInfrared[j].x, facePointsInfrared[j].y, 15, 15);
        }

        if (nosePos.x != 0 && nosePos.y != 0)
          for (int j = 0; j < 8; j++) {
            int st = faceFeatures[j].getState();
            int type = faceFeatures[j].getFeatureType();

            String str = getStateTypeAsString(st, type);

            fill(255);
            text(str, nosePos.x + 150, nosePos.y - 70 + j * 25);
          }
        stroke(255, 0, 0);
        noFill();
        rect(rectFace.getX(), rectFace.getY(), rectFace.getWidth(), rectFace.getHeight());
      }
    }
  }
Esempio n. 23
0
  public void move() {
    // TODO avoid running out of the frame not that dirty
    if (pos.x < 0 || pos.x > 640 || pos.y < 0 || pos.y > 480) {
      posXStabilizerCounter = 0;
      posYStabilizerCounter = 0;
    }

    // TODO check for other bubbles to avoid collisions
    // TODO play with max/min values
    if (posXStabilizerCounter == 0) {
      // generate int values of interval [min;max]: Min + (int)(Math.random() * ((Max - Min) + 1))
      posXStabilizerCounter = 10 + (int) (Math.random() * 41);
      posXOffset = -1 + (int) (Math.random() * 3);
    } else {
      posXStabilizerCounter--;
    }
    if (posYStabilizerCounter == 0) {
      posYStabilizerCounter = 10 + (int) (Math.random() * 41);
      posYOffset = -1 + (int) (Math.random() * 3);
    } else {
      posYStabilizerCounter--;
    }
    pos.set(pos.x + posXOffset, pos.y + posYOffset);
  }
Esempio n. 24
0
 // Change robot forward/backward speed only
 public void setSpeed(float x) {
   targetSpeed.set(x, 0);
   targetSpeed.limit(maxSpeed);
 }
Esempio n. 25
0
  /** @param theWindow */
  void draw(ControlWindow theWindow) {
    if (enabled) {

      if (_myMode >= ControlP5.WAIT) {

        previousPosition.set(currentPosition);
        currentPosition.set(theWindow.mouseX, theWindow.mouseY, 0);

        if (_myController != null) {
          if (_myController.getControlWindow().equals(theWindow)) {
            switch (_myMode) {
              case (ControlP5.WAIT):
                if (moved()) {
                  startTime = System.nanoTime();
                }

                if (System.nanoTime() > startTime + (_myDelayInMillis * 1000000)) {

                  position.set(currentPosition);
                  _myAlignH = ControlP5.RIGHT;
                  if (position.x
                      > (_myController.getControlWindow().papplet().width - (getWidth() + 20))) {
                    position.sub(new PVector(getWidth(), 0, 0));
                    _myAlignH = ControlP5.LEFT;
                  }
                  _myMode = ControlP5.FADEIN;
                  startTime = System.nanoTime();
                  _myAlpha = 0;
                }
                break;
              case (ControlP5.FADEIN):
                float t1 = System.nanoTime() - startTime;
                _myAlpha = (int) PApplet.map(t1, 0, 200 * 1000000, 0, _myMaxAlpha);
                if (_myAlpha >= 250) {
                  _myMode = ControlP5.IDLE;
                  _myAlpha = 255;
                }
                break;
              case (ControlP5.IDLE):
                break;
              case (ControlP5.FADEOUT):
                float t2 = System.nanoTime() - startTime;
                _myAlpha = (int) PApplet.map(t2, 0, 200 * 1000000, _myMaxAlpha, 0);
                if (_myAlpha <= 0) {
                  _myMode = ControlP5.DONE;
                }
                break;
              case (ControlP5.DONE):
                _myController = null;
                _myMode = ControlP5.INACTIVE;
                position.set(-1000, -1000, 0);
            }

            _myAlpha = PApplet.max(0, PApplet.min(_myAlpha, _myMaxAlpha));

            if (_myMode >= ControlP5.WAIT) {
              _myAlpha = (_myMode == ControlP5.WAIT) ? 0 : _myAlpha;
              theWindow.papplet().pushMatrix();
              theWindow.papplet().translate(position.x, position.y);
              theWindow.papplet().translate(offset.x, offset.y);
              _myView.display(theWindow.papplet(), null);
              theWindow.papplet().popMatrix();
            }
            if (_myMode < ControlP5.FADEOUT) {
              if (moved()) {
                deactivate(0);
              }
            }
          }
        }
      }
    }
  }
Esempio n. 26
0
  /** @param theWindow */
  void draw(ControlWindow theWindow) {
    // System.out.println(previousPosition+"\t"+currentPosition+"\t"+position);
    if (enabled) {

      if (mode >= Skatolo.WAIT) {

        previousPosition.set(currentPosition);
        currentPosition.set(theWindow.getPointerX(), theWindow.getPointerY(), 0);

        if (controller != null) {
          if (controller.getControlWindow().equals(theWindow)) {
            switch (mode) {
              case (Skatolo.WAIT):
                if (moved()) {
                  startTime = System.nanoTime();
                }

                if (System.nanoTime() > startTime + (delayInMillis * 1000000)) {

                  position.set(currentPosition);
                  alignH = Skatolo.RIGHT;
                  if (position.x
                      > (controller.getControlWindow().papplet().width - (getWidth() + 20))) {
                    position.sub(new PVector(getWidth(), 0, 0));
                    alignH = Skatolo.LEFT;
                  }
                  mode = Skatolo.FADEIN;
                  startTime = System.nanoTime();
                  currentAlpha = 0;
                }
                break;
              case (Skatolo.FADEIN):
                float t1 = System.nanoTime() - startTime;
                currentAlpha = (int) PApplet.map(t1, 0, 200 * 1000000, 0, maxAlpha);
                if (currentAlpha >= 250) {
                  mode = Skatolo.IDLE;
                  currentAlpha = 255;
                }
                break;
              case (Skatolo.IDLE):
                break;
              case (Skatolo.FADEOUT):
                float t2 = System.nanoTime() - startTime;
                currentAlpha = (int) PApplet.map(t2, 0, 200 * 1000000, maxAlpha, 0);
                if (currentAlpha <= 0) {
                  mode = Skatolo.DONE;
                }
                break;
              case (Skatolo.DONE):
                controller = null;
                mode = Skatolo.INACTIVE;
                position.set(-1000, -1000, 0);
            }

            currentAlpha = PApplet.max(0, PApplet.min(currentAlpha, maxAlpha));

            if (mode >= Skatolo.WAIT) {
              currentAlpha = (mode == Skatolo.WAIT) ? 0 : currentAlpha;
              theWindow.graphics().pushMatrix();
              theWindow.graphics().translate(position.x, position.y);
              theWindow.graphics().translate(offset.x, offset.y);
              controllerView.display(theWindow.graphics(), null); // TODO: Warning HERE !
              theWindow.graphics().popMatrix();
            }
            if (mode < Skatolo.FADEOUT) {
              if (moved()) {
                deactivate(0);
              }
            }
          }
        }
      }
    }
  }
Esempio n. 27
0
    // --------------------------------------------------------
    private void calculPositionAndAngle() {

      PVector prev = new PVector(0, 0);
      PVector next = new PVector(0, 0);
      float lastAngle = 0, randomAngle, tempAmplitude;

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

        if (i == 0) {

          position.add(next);

        } else {

          tempAmplitude = amplitude;
          next = new PVector(0, 0);

          do {

            randomAngle = random(-tempAmplitude, tempAmplitude) + lastAngle;
            next.x = prev.x + cos(randomAngle + beginAngle) * distance;
            next.y = prev.y + sin(randomAngle + beginAngle) * distance;
            tempAmplitude += radians(1);
          } while (outside(next, border));

          // Angle
          float a = atan2(prev.y - next.y, prev.x - next.x);
          lastAngle = randomAngle;

          // Add position
          position.add(next);

          // Set prev to next
          prev.set(next);
        }
      }

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

        float a;

        if (i == 0) {

          // a = atan2(position.get(0).y-position.get(1).y,position.get(0).x-position.get(1).x);
          angle.append(beginAngle - PI);

        } else if (i == n - 1) {

          a =
              atan2(
                  position.get(n - 2).y - position.get(n - 1).y,
                  position.get(n - 2).x - position.get(n - 1).x);
          angle.append(a);

        } else {

          a =
              atan2(
                  position.get(i - 1).y - position.get(i + 1).y,
                  position.get(i - 1).x - position.get(i + 1).x);
          angle.append(a);
        }
      }
    }
Esempio n. 28
0
 /**
  * set the position of this controller.
  *
  * @param theX float
  * @param theY float
  */
 public T setPosition(float theX, float theY) {
   position.set((int) theX, (int) theY, 0);
   positionBuffer.set(position);
   updateAbsolutePosition();
   return me;
 }
Esempio n. 29
0
 /** @exclude {@inheritDoc} */
 @ControlP5.Invisible
 public T setAbsolutePosition(PVector thePVector) {
   absolutePosition.set(thePVector.x, thePVector.y, thePVector.z);
   return me;
 }
Esempio n. 30
0
 public void setArbitraryPos(PApplet app, PGraphics context) {
   arbitraryPos.set(app.random(0, context.width), app.random(0, context.height), 0);
 }