@Override public void simpleInitApp() { JmeCanvasContext ctx = (JmeCanvasContext) getContext(); gui = new GUI(this, ctx.getCanvas()); gui.show(); flyCam.setEnabled(false); Box b = new Box(1, 1, 1); Geometry geom = new Geometry("Box", b); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.setColor("Color", ColorRGBA.Blue); geom.setMaterial(mat); rootNode.attachChild(geom); cinematic = new Cinematic(rootNode, 5); MotionPath path = new MotionPath(); path.addWayPoint(Vector3f.ZERO.clone()); path.addWayPoint(new Vector3f(10, 0, 0)); MotionEvent motionEvent = new MotionEvent(geom, path, 5); cinematic.addCinematicEvent(0, motionEvent); cinematic.fitDuration(); cinematic.setLoopMode(LoopMode.Loop); stateManager.attach(cinematic); }
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 }
/** Business logic */ public static MotionPath createMotionPath( TerrainQuad terrain, Cell[][] cells, Cell from, Cell to, Creature creature) throws NoReachablePathException { if (creature instanceof AirborneCreature) { AirborneCreature airCreature = (AirborneCreature) creature; airCreature.takeOff(); } if (!to.creatureAllowed(creature)) { throw new NoReachablePathException(); } MotionPath motionPath = new MotionPath(); /** Different for air creatures */ if (creature instanceof AirborneCreature) { // Add take off waypoint Vector3f takeOffWayPoint = new Vector3f( from.getWorldCoordinates().x, airCreatureHeight, from.getWorldCoordinates().z); motionPath.addWayPoint(creature.getModel().getLocalTranslation()); motionPath.addWayPoint(takeOffWayPoint); int numSteps = 100; float diffX = (to.getWorldCoordinates().x - from.getWorldCoordinates().x) / (float) numSteps; float diffZ = (to.getWorldCoordinates().z - from.getWorldCoordinates().z) / (float) numSteps; for (int i = 0; i < numSteps; i++) { float x = from.getWorldCoordinates().x + i * diffX; float z = from.getWorldCoordinates().z + i * diffZ; // float y = terrain.getHeight(new Vector2f(x, z)) + airCreatureHeight; motionPath.addWayPoint(new Vector3f(x, airCreatureHeight, z)); } } else { List<Cell> findPath = findPath(cells, from, to, creature); Cell previousCell = null; for (Cell cell : findPath) { if (previousCell != null) { // Smoothen path int numParts = 5; float xDiff = (cell.getWorldCoordinates().x - previousCell.getWorldCoordinates().x) / (float) numParts; float zDiff = (cell.getWorldCoordinates().z - previousCell.getWorldCoordinates().z) / (float) numParts; for (int i = 0; i < numParts; i++) { float x = previousCell.getWorldCoordinates().x + i * xDiff; float z = previousCell.getWorldCoordinates().z + i * zDiff; Vector3f part = null; if (creature instanceof SeaCreature) { System.out.println("test"); part = new Vector3f(x, 0, z); } else { part = new Vector3f(x, terrain.getHeight(new Vector2f(x, z)) - 100, z); } motionPath.addWayPoint(part); } } if (creature instanceof SeaCreature) { Vector3f old = cell.getWorldCoordinates(); motionPath.addWayPoint( new Vector3f(old.x, creature.getModel().getLocalTranslation().y, old.z)); } else { motionPath.addWayPoint(cell.getWorldCoordinates()); } previousCell = cell; } } motionPath.setCycle(false); return motionPath; }