Cinematic changes :

- more consistency in the way to handle time
- Complete refactoring of MotionPath interpolation (simpler and way faster)
- Time seeking should now work correctly.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9168 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
rem..om 13 years ago
parent 72561e7a8b
commit 9836918a07
  1. 25
      engine/src/core/com/jme3/cinematic/Cinematic.java
  2. 57
      engine/src/core/com/jme3/cinematic/MotionPath.java
  3. 27
      engine/src/core/com/jme3/cinematic/events/AbstractCinematicEvent.java
  4. 1
      engine/src/core/com/jme3/cinematic/events/AnimationTrack.java
  5. 39
      engine/src/core/com/jme3/cinematic/events/MotionTrack.java
  6. 6
      engine/src/core/com/jme3/cinematic/events/SoundTrack.java

@ -35,6 +35,7 @@ import com.jme3.animation.LoopMode;
import com.jme3.app.Application; import com.jme3.app.Application;
import com.jme3.app.state.AppState; import com.jme3.app.state.AppState;
import com.jme3.app.state.AppStateManager; import com.jme3.app.state.AppStateManager;
import com.jme3.asset.TextureKey;
import com.jme3.cinematic.events.AbstractCinematicEvent; import com.jme3.cinematic.events.AbstractCinematicEvent;
import com.jme3.cinematic.events.CinematicEvent; import com.jme3.cinematic.events.CinematicEvent;
import com.jme3.cinematic.events.CinematicEventListener; import com.jme3.cinematic.events.CinematicEventListener;
@ -68,7 +69,6 @@ public class Cinematic extends AbstractCinematicEvent implements AppState {
private CameraNode currentCam; private CameraNode currentCam;
private boolean initialized = false; private boolean initialized = false;
private Map<String, Map<String, Object>> eventsData; private Map<String, Map<String, Object>> eventsData;
private int scheduledPause = -1;
public Cinematic() { public Cinematic() {
} }
@ -95,8 +95,6 @@ public class Cinematic extends AbstractCinematicEvent implements AppState {
@Override @Override
public void onPlay() { public void onPlay() {
if (isInitialized()) { if (isInitialized()) {
scheduledPause = -1;
//enableCurrentCam(true);
if (playState == PlayState.Paused) { if (playState == PlayState.Paused) {
for (int i = 0; i < cinematicEvents.size(); i++) { for (int i = 0; i < cinematicEvents.size(); i++) {
CinematicEvent ce = cinematicEvents.get(i); CinematicEvent ce = cinematicEvents.get(i);
@ -197,22 +195,8 @@ public class Cinematic extends AbstractCinematicEvent implements AppState {
} }
} }
private void step() {
if (playState != PlayState.Playing) {
play();
scheduledPause = 2;
}
}
@Override @Override
public void onUpdate(float tpf) { public void onUpdate(float tpf) {
if (scheduledPause >= 0) {
if (scheduledPause == 0) {
pause();
}
scheduledPause--;
}
for (int i = 0; i < cinematicEvents.size(); i++) { for (int i = 0; i < cinematicEvents.size(); i++) {
CinematicEvent ce = cinematicEvents.get(i); CinematicEvent ce = cinematicEvents.get(i);
ce.internalUpdate(tpf); ce.internalUpdate(tpf);
@ -242,15 +226,16 @@ public class Cinematic extends AbstractCinematicEvent implements AppState {
KeyFrame keyFrame = timeLine.get(i); KeyFrame keyFrame = timeLine.get(i);
if (keyFrame != null) { if (keyFrame != null) {
for (CinematicEvent ce : keyFrame.getCinematicEvents()) { for (CinematicEvent ce : keyFrame.getCinematicEvents()) {
if (playState == PlayState.Playing) {
ce.play(); ce.play();
}
ce.setTime(time - timeLine.getKeyFrameTime(keyFrame)); ce.setTime(time - timeLine.getKeyFrameTime(keyFrame));
} }
} }
} }
if (playState != PlayState.Playing) {
pause();
}
step(); // step();
} }
public KeyFrame addCinematicEvent(float timeStamp, CinematicEvent cinematicEvent) { public KeyFrame addCinematicEvent(float timeStamp, CinematicEvent cinematicEvent) {

@ -81,56 +81,23 @@ public class MotionPath implements Savable {
TempVars vars = TempVars.get(); TempVars vars = TempVars.get();
Vector3f temp = vars.vect1; Vector3f temp = vars.vect1;
Vector3f tmpVector = vars.vect2; Vector3f tmpVector = vars.vect2;
switch (spline.getType()) { //computing traveled distance according to new time
case CatmullRom: traveledDistance = time * (getLength() / control.getInitialDuration());
//getting waypoint index and current value from new traveled distance
Vector2f v = getWayPointIndexForDistance(traveledDistance);
//setting values
control.setCurrentWayPoint((int) v.x);
control.setCurrentValue(v.y);
//this iterative process is done to keep the spatial travel at a constant speed on the path even if //interpolating new position
//waypoints are not equally spread over the path getSpline().interpolate(control.getCurrentValue(), control.getCurrentWayPoint(), temp);
// we compute the theorical distance that the spatial should travel on this frame
val = (time * (spline.getTotalLength() / control.getDuration())) - control.getTraveledDistance();
//adding and epsilon value to the control currents value
control.setCurrentValue(control.getCurrentValue() + eps);
//computing the new position at current value
spline.interpolate(control.getCurrentValue(), control.getCurrentWayPoint(), temp);
//computing traveled distance at current value
float dist = getDist(control, temp, tmpVector);
//While the distance traveled this frame is below the theorical distance we iterate the obove computation
while (dist < val) {
//storing the distance traveled this frame
traveledDistance = dist;
control.setCurrentValue(control.getCurrentValue() + eps);
spline.interpolate(control.getCurrentValue(), control.getCurrentWayPoint(), temp);
dist = getDist(control, temp, tmpVector);
}
//compute the direction of the spline
if (control.needsDirection()) { if (control.needsDirection()) {
tmpVector.set(temp); tmpVector.set(temp);
control.setDirection(tmpVector.subtractLocal(control.getSpatial().getLocalTranslation()).normalizeLocal()); control.setDirection(tmpVector.subtractLocal(control.getSpatial().getLocalTranslation()).normalizeLocal());
} }
//updating traveled distance to the total distance traveled by the spatial since the start
traveledDistance += control.getTraveledDistance();
break;
case Linear:
//distance traveled this frame
val = (time * (spline.getTotalLength() / control.getDuration())) - control.getTraveledDistance();
// computing total traveled distance
traveledDistance = control.getTraveledDistance() + val;
//computing interpolation ratio for this frame
val = val / spline.getSegmentsLength().get(control.getCurrentWayPoint());
control.setCurrentValue(Math.min(control.getCurrentValue() + val, 1.0f));
//interpolationg position
spline.interpolate(control.getCurrentValue(), control.getCurrentWayPoint(), temp);
//computing line direction
if (control.needsDirection()) {
tmpVector.set(spline.getControlPoints().get(control.getCurrentWayPoint() + 1));
control.setDirection(tmpVector.subtractLocal(spline.getControlPoints().get(control.getCurrentWayPoint())).normalizeLocal());
}
break;
default:
break;
}
control.getSpatial().setLocalTranslation(temp); control.getSpatial().setLocalTranslation(temp);
vars.release(); vars.release();
return traveledDistance; return traveledDistance;

@ -39,8 +39,6 @@ import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter; import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter; import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule; import com.jme3.export.OutputCapsule;
import com.jme3.system.NanoTimer;
import com.jme3.system.Timer;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -59,12 +57,8 @@ public abstract class AbstractCinematicEvent implements CinematicEvent {
protected float initialDuration = 10; protected float initialDuration = 10;
protected LoopMode loopMode = LoopMode.DontLoop; protected LoopMode loopMode = LoopMode.DontLoop;
protected float time = 0; protected float time = 0;
protected Timer timer; protected boolean resuming = false;
protected float start = 0;
/**
* the last time the event was paused
*/
protected float elapsedTimePause = 0;
/** /**
* the list of listeners * the list of listeners
*/ */
@ -108,12 +102,6 @@ public abstract class AbstractCinematicEvent implements CinematicEvent {
public void play() { public void play() {
onPlay(); onPlay();
playState = PlayState.Playing; playState = PlayState.Playing;
if (timer == null) {
//only when used as a control
timer = new NanoTimer();
}
start = timer.getTimeInSeconds();
//timer.reset();
if (listeners != null) { if (listeners != null) {
for (int i = 0; i < listeners.size(); i++) { for (int i = 0; i < listeners.size(); i++) {
CinematicEventListener cel = listeners.get(i); CinematicEventListener cel = listeners.get(i);
@ -133,7 +121,7 @@ public abstract class AbstractCinematicEvent implements CinematicEvent {
*/ */
public void internalUpdate(float tpf) { public void internalUpdate(float tpf) {
if (playState == PlayState.Playing) { if (playState == PlayState.Playing) {
time = time+ (tpf * speed); time = time + (tpf * speed);
//time = elapsedTimePause + (timer.getTimeInSeconds() - start) * speed; //time = elapsedTimePause + (timer.getTimeInSeconds() - start) * speed;
onUpdate(tpf); onUpdate(tpf);
@ -157,7 +145,6 @@ public abstract class AbstractCinematicEvent implements CinematicEvent {
onStop(); onStop();
time = 0; time = 0;
playState = PlayState.Stopped; playState = PlayState.Stopped;
elapsedTimePause = 0;
if (listeners != null) { if (listeners != null) {
for (int i = 0; i < listeners.size(); i++) { for (int i = 0; i < listeners.size(); i++) {
CinematicEventListener cel = listeners.get(i); CinematicEventListener cel = listeners.get(i);
@ -177,7 +164,6 @@ public abstract class AbstractCinematicEvent implements CinematicEvent {
public void pause() { public void pause() {
onPause(); onPause();
playState = PlayState.Paused; playState = PlayState.Paused;
elapsedTimePause = time;
if (listeners != null) { if (listeners != null) {
for (int i = 0; i < listeners.size(); i++) { for (int i = 0; i < listeners.size(); i++) {
CinematicEventListener cel = listeners.get(i); CinematicEventListener cel = listeners.get(i);
@ -291,8 +277,6 @@ public abstract class AbstractCinematicEvent implements CinematicEvent {
* @param cinematic * @param cinematic
*/ */
public void initEvent(Application app, Cinematic cinematic) { public void initEvent(Application app, Cinematic cinematic) {
timer = app.getContext().getTimer();
//timer = new NanoTimer();
} }
/** /**
@ -327,10 +311,7 @@ public abstract class AbstractCinematicEvent implements CinematicEvent {
* @param time the time to fast forward to * @param time the time to fast forward to
*/ */
public void setTime(float time) { public void setTime(float time) {
elapsedTimePause = time / speed; this.time = time / speed;
if (playState == PlayState.Playing) {
start = timer.getTimeInSeconds();
}
} }
public float getTime() { public float getTime() {

@ -122,6 +122,7 @@ public class AnimationTrack extends AbstractCinematicEvent {
} }
channel.setTime(t); channel.setTime(t);
channel.getControl().update(0);
} }
@Override @Override

@ -165,10 +165,12 @@ public class MotionTrack extends AbstractCinematicEvent implements Control {
if (isControl) { if (isControl) {
if (playState == PlayState.Playing) { if (playState == PlayState.Playing) {
time = (elapsedTimePause + timer.getTimeInSeconds() - start) * speed; time = time + (tpf * speed);
onUpdate(tpf);
if (time >= initialDuration && loopMode == loopMode.DontLoop) { if (time >= initialDuration && loopMode == loopMode.DontLoop) {
stop(); stop();
} else {
onUpdate(tpf);
} }
} }
} }
@ -183,40 +185,12 @@ public class MotionTrack extends AbstractCinematicEvent implements Control {
@Override @Override
public void setTime(float time) { public void setTime(float time) {
super.setTime(time); super.setTime(time);
onUpdate(0);
//computing traveled distance according to new time
traveledDistance = time * (path.getLength() / initialDuration);
TempVars vars = TempVars.get();
Vector3f temp = vars.vect1;
//getting waypoint index and current value from new traveled distance
Vector2f v = path.getWayPointIndexForDistance(traveledDistance);
//setting values
currentWayPoint = (int) v.x;
setCurrentValue(v.y);
//interpolating new position
path.getSpline().interpolate(getCurrentValue(), getCurrentWayPoint(), temp);
//setting new position to the spatial
spatial.setLocalTranslation(temp);
vars.release();
} }
public void onUpdate(float tpf) { public void onUpdate(float tpf) {
traveledDistance = path.interpolatePath(time, this); traveledDistance = path.interpolatePath(time, this);
computeTargetDirection(); computeTargetDirection();
if (currentValue >= 1.0f) {
currentValue = 0;
currentWayPoint++;
path.triggerWayPointReach(currentWayPoint, this);
}
if (currentWayPoint == path.getNbWayPoints() - 1) {
if (loopMode == LoopMode.Loop) {
currentWayPoint = 0;
} else {
stop();
}
}
} }
@Override @Override
@ -346,7 +320,10 @@ public class MotionTrack extends AbstractCinematicEvent implements Control {
* *
*/ */
public void setCurrentWayPoint(int currentWayPoint) { public void setCurrentWayPoint(int currentWayPoint) {
if (this.currentWayPoint != currentWayPoint) {
this.currentWayPoint = currentWayPoint; this.currentWayPoint = currentWayPoint;
path.triggerWayPointReach(currentWayPoint, this);
}
} }
/** /**

@ -115,7 +115,11 @@ public class SoundTrack extends AbstractCinematicEvent {
@Override @Override
public void setTime(float time) { public void setTime(float time) {
super.setTime(time); super.setTime(time);
//TODO has to be implemented in the audioRenderer //can occur on rewind
if (time < 0) {
stop();
}
audioNode.setTimeOffset(time);
} }
@Override @Override

Loading…
Cancel
Save