Exemplo n.º 1
0
  private Spatial createFollowBox() {
    // add spatial representing the position the driving car is steering towards
    Box box = new Box(new Vector3f(0, 0, 0), 1f, 1f, 1f);
    Geometry followBox = new Geometry("followBox", box);
    followBox.setLocalTranslation(0, 0, 0);
    Material followBoxMaterial =
        new Material(sim.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
    followBoxMaterial.setColor("Color", ColorRGBA.Green);
    followBox.setMaterial(followBoxMaterial);
    followBox.setLocalScale(0.4f);
    sim.getSceneNode().attachChild(followBox);

    if (!settings.isPathVisible()) followBox.setCullHint(CullHint.Always);

    return followBox;
  }
Exemplo n.º 2
0
  public FollowBox(Simulator sim, final TrafficCar vehicle, FollowBoxSettings settings) {
    this.sim = sim;
    this.vehicle = vehicle;
    this.settings = settings;

    waypointList = settings.getWayPoints();
    maxDistance = settings.getMaxDistance();

    motionPath = new MotionPath();

    motionPath.setCycle(settings.isPathCyclic());

    for (Waypoint wayPoint : waypointList) motionPath.addWayPoint(wayPoint.getPosition());

    motionPath.setPathSplineType(SplineType.CatmullRom); // --> default: CatmullRom
    motionPath.setCurveTension(settings.getCurveTension());

    if (settings.isPathVisible())
      motionPath.enableDebugShape(sim.getAssetManager(), sim.getSceneNode());

    motionPath.addListener(
        new MotionPathListener() {
          public void onWayPointReach(MotionTrack control, int wayPointIndex) {
            // set speed limit for next way point
            int index = wayPointIndex % waypointList.size();
            float speed = waypointList.get(index).getSpeed();
            setSpeed(speed);

            // if last way point reached
            if (motionPath.getNbWayPoints() == wayPointIndex + 1) {
              // reset vehicle to first way point if not cyclic
              if (!motionPath.isCycle()) setToWayPoint(0);
            }
          }
        });

    followBox = createFollowBox();
    motionControl = new MotionTrack(followBox, motionPath);

    // get start way point
    int startWayPointIndex = settings.getStartWayPointIndex();

    // set start speed
    float initialSpeed = waypointList.get(startWayPointIndex).getSpeed();
    setSpeed(initialSpeed);

    // set start position
    setToWayPoint(startWayPointIndex);

    // move object along path considering rotation
    motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);

    // loop movement of object
    motionControl.setLoopMode(LoopMode.Loop);

    // rotate moving object
    // motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI,
    // Vector3f.UNIT_Y));

    // set moving object to position "20 seconds"
    // motionPath.interpolatePath(20, motionControl);

    // start movement
    // motionControl.play(); // already contained in update method
  }