public void showCar() {
    for (int i = 0; i < 4; i++) {
      cars[i] = factory.getCar(car_names[i], assetManager);
      car_con[i] = cars[i].getControl(VehicleControl.class);
    }
    Camera camera = cam.clone();

    camera.setViewPort(.25f, .9f, .1f, .75f);
    carView = this.app.getRenderManager().createPostView("carview", camera);

    space.add(car_con[index]);
    dl = new DirectionalLight();
    localRootNode.addLight(dl);
    ai = new AmbientLight();
    localRootNode.addLight(ai);
    car_con[index].setPhysicsLocation(new Vector3f(0, 1, 0));
    localRootNode.attachChild(cars[index]);
    floor = assetManager.loadModel("Models/garage/garage.mesh.j3o");
    control = new RigidBodyControl(0);
    floor.addControl(control);
    control.setPhysicsLocation(Vector3f.ZERO);
    space.add(control);
    localRootNode.attachChild(floor);

    camera.setLocation(car_con[index].getPhysicsLocation().add(new Vector3f(3, 1f, 0)));
    camera.lookAt(car_con[index].getPhysicsLocation().add(new Vector3f(0, -1, 0)), Vector3f.UNIT_Y);
    dl.setDirection(camera.getDirection());

    carView.attachScene(localRootNode);
  }
Beispiel #2
0
  private void refreshFont() {
    Camera cam = IsoCamera.getInstance().getCam();

    Vector3f start = from.getPlanet().getPosition();
    Vector3f end = to.getPlanet().getPosition();

    float width = label.getLineWidth();
    Random r = new Random();
    //        float height = r.nextFloat() * end.subtract(start).z;

    Vector3f position = new Vector3f(width / 2, .15f, 0);
    Vector3f fontPos = start.add(end.subtract(start).mult(0.4f + 0.75f * r.nextFloat()));
    position.addLocal(fontPos);

    Vector3f up = cam.getUp().clone();
    Vector3f dir = cam.getDirection().clone().negateLocal().normalizeLocal();
    Vector3f left = cam.getLeft().clone().normalizeLocal().negateLocal();

    Quaternion look = new Quaternion();
    look.fromAxes(left, up, dir);

    label.setLocalTransform(new Transform(position, look));

    //        Vector3f camPos = IsoCamera.getInstance().getCam().getLocation();
    //        Vector3f fontPos = to.getPlanet().getPosition().
    //                subtract(from.getPlanet().getPosition());
    //        Vector3f up = IsoCamera.getInstance().getCam().getUp().clone();
    //        Vector3f dir = camPos.subtract(fontPos);
    ////        Vector3f dir = Vector3f.UNIT_Y.clone().subtract(fontPos);
    //
    //        Vector3f left = IsoCamera.getInstance().getCam().getLeft().clone();
    //        dir.normalizeLocal();
    //        left.negateLocal();
    //        left.normalizeLocal();
    //        up.normalizeLocal();
    ////        dir.negateLocal();
    //
    //        Quaternion look = new Quaternion();
    //        look.fromAxes(left, up, dir);
    //
    //        Vector3f newPos = to.getPlanet().getPosition().
    //                subtract(from.getPlanet().getPosition());
    //
    //        newPos.x -= label.getLineWidth() / 2;
    //
    //        Transform t = new Transform(newPos, look);
    //
    //        label.setLocalTransform(t);
  }
  public void addPoint() {
    Point p = new Point();
    p.position = cam.getLocation().clone();
    p.direction = cam.getDirection().clone();
    p.rotation = cam.getRotation().clone();
    p.speed = currentSpeed;
    LOG.info("point added: " + p.position + "  " + p.rotation);

    p.sphere = new Geometry("point", sphereMesh);
    p.sphere.setMaterial(sphereMat);
    p.sphere.setLocalTranslation(p.position);
    sceneNode.attachChild(p.sphere);
    p.arrow =
        new Geometry("arrow", new Arrow(p.direction.mult(TerrainHeighmapCreator.TERRAIN_SCALE)));
    p.arrow.getMesh().setLineWidth(4);
    p.arrow.setMaterial(arrowMat);
    p.arrow.setLocalTranslation(p.position);
    sceneNode.attachChild(p.arrow);

    points.add(p);
  }
  @Override
  public void updateParticleData(ParticleData[] particles, Camera cam, Matrix3f inverseRotation) {

    for (int i = 0; i < particles.length; i++) {
      ParticleData p = particles[i];
      int offset = templateVerts.capacity() * i;
      int colorOffset = templateColors.capacity() * i;
      if (p.life == 0 || !p.active) {
        for (int x = 0; x < templateVerts.capacity(); x += 3) {
          finVerts.put(offset + x, 0);
          finVerts.put(offset + x + 1, 0);
          finVerts.put(offset + x + 2, 0);
        }
        continue;
      }

      for (int x = 0; x < templateVerts.capacity(); x += 3) {
        switch (emitter.getBillboardMode()) {
          case Velocity:
            if (p.velocity.x != Vector3f.UNIT_Y.x
                && p.velocity.y != Vector3f.UNIT_Y.y
                && p.velocity.z != Vector3f.UNIT_Y.z)
              up.set(p.velocity).crossLocal(Vector3f.UNIT_Y).normalizeLocal();
            else up.set(p.velocity).crossLocal(lock).normalizeLocal();
            left.set(p.velocity).crossLocal(up).normalizeLocal();
            dir.set(p.velocity);
            break;
          case Velocity_Z_Up:
            if (p.velocity.x != Vector3f.UNIT_Y.x
                && p.velocity.y != Vector3f.UNIT_Y.y
                && p.velocity.z != Vector3f.UNIT_Y.z)
              up.set(p.velocity).crossLocal(Vector3f.UNIT_Y).normalizeLocal();
            else up.set(p.velocity).crossLocal(lock).normalizeLocal();
            left.set(p.velocity).crossLocal(up).normalizeLocal();
            dir.set(p.velocity);
            rotStore = tempQ.fromAngleAxis(-90 * FastMath.DEG_TO_RAD, left);
            left = rotStore.mult(left);
            up = rotStore.mult(up);
            break;
          case Velocity_Z_Up_Y_Left:
            up.set(p.velocity).crossLocal(Vector3f.UNIT_Y).normalizeLocal();
            left.set(p.velocity).crossLocal(up).normalizeLocal();
            dir.set(p.velocity);
            tempV3.set(left).crossLocal(up).normalizeLocal();
            rotStore = tempQ.fromAngleAxis(90 * FastMath.DEG_TO_RAD, p.velocity);
            left = rotStore.mult(left);
            up = rotStore.mult(up);
            rotStore = tempQ.fromAngleAxis(-90 * FastMath.DEG_TO_RAD, left);
            up = rotStore.mult(up);
            break;
          case Normal:
            emitter.getShape().setNext(p.triangleIndex);
            tempV3.set(emitter.getShape().getNormal());
            if (tempV3 == Vector3f.UNIT_Y) tempV3.set(p.velocity);

            up.set(tempV3).crossLocal(Vector3f.UNIT_Y).normalizeLocal();
            left.set(tempV3).crossLocal(up).normalizeLocal();
            dir.set(tempV3);
            break;
          case Normal_Y_Up:
            emitter.getShape().setNext(p.triangleIndex);
            tempV3.set(p.velocity);
            if (tempV3 == Vector3f.UNIT_Y) tempV3.set(Vector3f.UNIT_X);

            up.set(Vector3f.UNIT_Y);
            left.set(tempV3).crossLocal(up).normalizeLocal();
            dir.set(tempV3);
            break;
          case Camera:
            up.set(cam.getUp());
            left.set(cam.getLeft());
            dir.set(cam.getDirection());
            break;
          case UNIT_X:
            up.set(Vector3f.UNIT_Y);
            left.set(Vector3f.UNIT_Z);
            dir.set(Vector3f.UNIT_X);
            break;
          case UNIT_Y:
            up.set(Vector3f.UNIT_Z);
            left.set(Vector3f.UNIT_X);
            dir.set(Vector3f.UNIT_Y);
            break;
          case UNIT_Z:
            up.set(Vector3f.UNIT_X);
            left.set(Vector3f.UNIT_Y);
            dir.set(Vector3f.UNIT_Z);
            break;
        }

        tempV3.set(templateVerts.get(x), templateVerts.get(x + 1), templateVerts.get(x + 2));
        tempV3 = rotStore.mult(tempV3);
        tempV3.multLocal(p.size);

        rotStore.fromAngles(p.angles.x, p.angles.y, p.angles.z);
        tempV3 = rotStore.mult(tempV3);

        tempV3.addLocal(p.position);
        if (!emitter.getParticlesFollowEmitter()) {
          tempV3.subtractLocal(
              emitter
                  .getEmitterNode()
                  .getWorldTranslation()
                  .subtract(p.initialPosition)); // .divide(8f));
        }

        finVerts.put(offset + x, tempV3.getX());
        finVerts.put(offset + x + 1, tempV3.getY());
        finVerts.put(offset + x + 2, tempV3.getZ());
      }
      if (p.emitter.getApplyLightingTransform()) {
        for (int v = 0; v < templateNormals.capacity(); v += 3) {
          tempV3.set(
              templateNormals.get(v), templateNormals.get(v + 1), templateNormals.get(v + 2));

          rotStore.fromAngles(p.angles.x, p.angles.y, p.angles.z);
          mat3.set(rotStore.toRotationMatrix());
          float vx = tempV3.x, vy = tempV3.y, vz = tempV3.z;
          tempV3.x = mat3.get(0, 0) * vx + mat3.get(0, 1) * vy + mat3.get(0, 2) * vz;
          tempV3.y = mat3.get(1, 0) * vx + mat3.get(1, 1) * vy + mat3.get(1, 2) * vz;
          tempV3.z = mat3.get(2, 0) * vx + mat3.get(2, 1) * vy + mat3.get(2, 2) * vz;

          finNormals.put(offset + v, tempV3.getX());
          finNormals.put(offset + v + 1, tempV3.getY());
          finNormals.put(offset + v + 2, tempV3.getZ());
        }
      }
      for (int v = 0; v < templateColors.capacity(); v += 4) {
        finColors
            .put(colorOffset + v, p.color.r)
            .put(colorOffset + v + 1, p.color.g)
            .put(colorOffset + v + 2, p.color.b)
            .put(colorOffset + v + 3, p.color.a * p.alpha);
      }
    }

    this.setBuffer(VertexBuffer.Type.Position, 3, finVerts);
    if (particles[0].emitter.getApplyLightingTransform())
      this.setBuffer(VertexBuffer.Type.Normal, 3, finNormals);
    this.setBuffer(VertexBuffer.Type.Color, 4, finColors);

    updateBound();
  }
Beispiel #5
0
  /**
   * @param panel
   * @param id
   * @param pictureFile
   * @param width
   * @param height
   */
  public TextField(
      Panel panel, String id, String pictureFile, float width, float height, boolean lockScale) {
    super(panel.getWindow(), panel, pictureFile, width, height, lockScale);
    this.id = id;
    setName(id);

    // Init the text
    float xP = -getWidth() * 0.5f;
    float yP = getHeight() * 0.5f;
    float recWidth = getWidth();
    float factor = 1f;
    float recHeight = (getHeight() * 0.5f) * factor;
    float padding = 0f;

    bitmapText = window.getBitmapFont().createLabel(id);
    bitmapText.setText(" ");
    bitmapText.setBox(new Rectangle(xP + padding, yP, recWidth + padding, recHeight));
    bitmapText.setSize(fontSize * window.getScaleFactorHeight()); // font size
    bitmapText.setColor(ColorRGBA.DarkGray); // font color
    bitmapText.setAlignment(BitmapFont.Align.Left);
    bitmapText.setVerticalAlignment(BitmapFont.VAlign.Center);
    bitmapText.setLineWrapMode(LineWrapMode.NoWrap);
    widgetNode.attachChild(bitmapText);

    this.inputManager = window.getInputManager();
    this.cam = window.getApplication().getCamera();
    this.results = new CollisionResults();
    this.ray = new Ray(cam.getLocation(), cam.getDirection());

    actionListener =
        new ActionListener() {
          public void onAction(String name, boolean isPressed, float tpf) {
            results.clear();

            if (isVisible() && isEnabled()) {

              if ((TextField.this.id + "MOUSE").equals(name)) {

                // 1. calc direction
                Vector3f origin =
                    new Vector3f(
                        inputManager.getCursorPosition().x, inputManager.getCursorPosition().y, 1f);
                Vector3f direction = new Vector3f(0, 0, -1);

                // 2. Aim the ray from cam loc to cam direction.
                ray.setOrigin(origin);
                ray.setDirection(direction);

                // 3. Collect intersections between Ray and Shootables in results list.
                window.getWindowNode().collideWith(ray, results);

                // 5. Use the results (we mark the hit object)
                if (results.size() > 0) {

                  for (int i = 0; i < results.size(); i++) {
                    CollisionResult cr = results.getCollision(i);
                    //                                System.out.println("\t-> Hit: " +
                    // cr.getGeometry().getParent().getName());

                    if (widgetNode.hasChild(cr.getGeometry())) {
                      //                                    System.out.println("\t\t\tCollision -> "
                      // + TouchButton.this.id);
                      if (isPressed) {
                        wasDown = true;
                        getWindow().removeFocusFromFields();
                        focus = true;
                        fireFocusListener(TextField.this.id);
                      }
                    }
                  }
                }
              }
            }
          }
        };

    inputManager.addRawInputListener(
        new RawInputListener() {

          public void beginInput() {}

          public void endInput() {}

          public void onJoyAxisEvent(JoyAxisEvent evt) {}

          public void onJoyButtonEvent(JoyButtonEvent evt) {}

          public void onMouseMotionEvent(MouseMotionEvent evt) {}

          public void onMouseButtonEvent(MouseButtonEvent evt) {}

          public void onKeyEvent(KeyInputEvent evt) {
            //                System.out.println("Keyinput ***************** Key = " +
            // evt.getKeyCode());

            if (enabled && focus && evt.isReleased()) {
              String keyChar = keyNames.getName(evt.getKeyCode());
              //                    System.out.println("Keyinput ***************** Char = " +
              // keyChar);

              if (evt.getKeyCode() == 14) {
                if (getText().length() > 0) {
                  setText(getText().substring(0, getText().length() - 1));
                }
              } else if (evt.getKeyCode() == 15) {
                focus = false;

              } else if (keyChar != null && evt.getKeyCode() == 57) {
                setText(getText() + " ");

              } else if (keyChar != null && evt.getKeyCode() == 58) {
                caps = !caps;

              } else if (keyChar != null && keyChar.length() == 1) {
                if (!caps) {
                  keyChar = keyChar.toLowerCase();
                }
                setText(getText() + keyChar);
              }

              if (getText().length() > maxLength) {
                setText(getText().substring(0, maxLength));
              }

              fireKeyboardListener(evt);
            }
          }

          public void onTouchEvent(TouchEvent evt) {
            //                System.out.println("Touchinput ***************** Keycode = " +
            // evt.getKeyCode());

            if (enabled && focus && evt.getType().equals(TouchEvent.Type.KEY_DOWN)) {
              String keyChar = touchKeyNames.getName(evt.getKeyCode());
              System.out.println(
                  "\n\n\nTouchinput ***************** KeyCode = " + evt.getKeyCode());

              if (evt.getKeyCode() == 67) { // backspace
                if (getText().length() > 0) {
                  setText(getText().substring(0, getText().length() - 1));
                }

              } else if (keyChar != null && evt.getKeyCode() == 62) { // space
                setText(getText() + " ");

              } else if (keyChar != null && evt.getKeyCode() == 59) { // shift
                caps = !caps;

              } else if (keyChar != null && keyChar.length() == 1) {
                // TODO:
                //                        if (!caps) {
                keyChar = keyChar.toLowerCase();
                //                        }
                setText(getText() + keyChar);
              }

              if (getText().length() > maxLength) {
                setText(getText().substring(0, maxLength));
              }

              //                    if (evt.getKeyCode() == 67) {
              //                        if (getText().length() > 0) {
              //                            setText(getText().substring(0, getText().length()-1));
              //                        }
              //
              //                    } else if (evt.getKeyCode() == 59) {
              //                        if (getText().length() > 0) {
              //                            setText(getText().substring(0, getText().length()-1));
              //                        }
              //
              //                    } else if (keyChar != null && keyChar.length() == 1) {
              //                        setText(getText() + keyChar);
              //                    }
              //
              //                    if (getText().length() > maxLength) {
              //                        setText(getText().substring(0, maxLength));
              //                    }

            }
          }
        });

    // NICKI
    panel.add(this);

    //        bitmapText.setLocalTranslation(bitmapText.getLocalTranslation().x,
    // bitmapText.getLocalTranslation().y, 0.001f);
  }
Beispiel #6
0
  /**
   * Updates the points array to contain the frustum corners of the given camera. The nearOverride
   * and farOverride variables can be used to override the camera's near/far values with own values.
   *
   * <p>TODO: Reduce creation of new vectors
   *
   * @param viewCam
   * @param nearOverride
   * @param farOverride
   */
  public static void updateFrustumPoints(
      Camera viewCam, float nearOverride, float farOverride, float scale, Vector3f[] points) {

    Vector3f pos = viewCam.getLocation();
    Vector3f dir = viewCam.getDirection();
    Vector3f up = viewCam.getUp();

    float depthHeightRatio = viewCam.getFrustumTop() / viewCam.getFrustumNear();
    float near = nearOverride;
    float far = farOverride;
    float ftop = viewCam.getFrustumTop();
    float fright = viewCam.getFrustumRight();
    float ratio = fright / ftop;

    float near_height;
    float near_width;
    float far_height;
    float far_width;

    if (viewCam.isParallelProjection()) {
      near_height = ftop;
      near_width = near_height * ratio;
      far_height = ftop;
      far_width = far_height * ratio;
    } else {
      near_height = depthHeightRatio * near;
      near_width = near_height * ratio;
      far_height = depthHeightRatio * far;
      far_width = far_height * ratio;
    }

    Vector3f right = dir.cross(up).normalizeLocal();

    Vector3f temp = new Vector3f();
    temp.set(dir).multLocal(far).addLocal(pos);
    Vector3f farCenter = temp.clone();
    temp.set(dir).multLocal(near).addLocal(pos);
    Vector3f nearCenter = temp.clone();

    Vector3f nearUp = temp.set(up).multLocal(near_height).clone();
    Vector3f farUp = temp.set(up).multLocal(far_height).clone();
    Vector3f nearRight = temp.set(right).multLocal(near_width).clone();
    Vector3f farRight = temp.set(right).multLocal(far_width).clone();

    points[0].set(nearCenter).subtractLocal(nearUp).subtractLocal(nearRight);
    points[1].set(nearCenter).addLocal(nearUp).subtractLocal(nearRight);
    points[2].set(nearCenter).addLocal(nearUp).addLocal(nearRight);
    points[3].set(nearCenter).subtractLocal(nearUp).addLocal(nearRight);

    points[4].set(farCenter).subtractLocal(farUp).subtractLocal(farRight);
    points[5].set(farCenter).addLocal(farUp).subtractLocal(farRight);
    points[6].set(farCenter).addLocal(farUp).addLocal(farRight);
    points[7].set(farCenter).subtractLocal(farUp).addLocal(farRight);

    if (scale != 1.0f) {
      // find center of frustum
      Vector3f center = new Vector3f();
      for (int i = 0; i < 8; i++) {
        center.addLocal(points[i]);
      }
      center.divideLocal(8f);

      Vector3f cDir = new Vector3f();
      for (int i = 0; i < 8; i++) {
        cDir.set(points[i]).subtractLocal(center);
        cDir.multLocal(scale - 1.0f);
        points[i].addLocal(cDir);
      }
    }
  }