Using Cinematics with MotionPath

I am trying to use motion path with cinematics. It works except at the end of motion path the spatial goes back to the motion path starting location which messes up everything that comes after. Is something wrong with my code cz I can’t figure it out?



Here’s the code I wrote:

[java]

public void RotateWhileMovingSpatial(String id, float trigger_time, float newX,

float newY, float newZ, float oldX, float oldY, float oldZ,

double oldTheta, float duration) {



final Spatial spatialToMove = myApp.getRootNode().getChild(id);



MotionPath path = new MotionPath();

path.addWayPoint(new Vector3f(oldX, oldY, oldZ));

path.addWayPoint(new Vector3f((newX - oldX), (newY - oldY)/3, newZ));

path.addWayPoint(new Vector3f(newX, newY, newZ));

path.setCurveTension(0.5f);

path.enableDebugShape(myApp.getAssetManager(),myApp.getRootNode());



// motion path

MotionTrack motionControl;

motionControl = new MotionTrack(spatialToMove,path);

motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);

motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));

motionControl.setInitialDuration(duration);



// add cinematic event to cinematic buffer

cinematic.addCinematicEvent(trigger_time, motionControl);

}

[/java]

thanks

what revision of JME do you use?

I’m looking into a proper way to travel backward on a motion path.

I’ll post updates here.

sorry just saw that… I am using nightly Mid march

Am i missing anything in my code? even if I put two consecutive events right after each other the object restarts very briefly but since I am providing the old position it fixes itself , then restarts back to the initial position.

I also have an issue with this but it’s different. Mine does not restart like homsi but it rotates weirdly in some occasions, I can’t explain it so here’s a test case (by modifying the wiki code). Notice how the teapot rotates weirdly when going vertical. Also it comes back with a different directions, I just want it to go back in reverse.



[java]package mygame;





import com.jme3.app.SimpleApplication;

import com.jme3.cinematic.MotionPath;

import com.jme3.cinematic.MotionPathListener;

import com.jme3.cinematic.events.MotionTrack;

import com.jme3.font.BitmapText;

import com.jme3.input.ChaseCamera;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.light.DirectionalLight;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.FastMath;

import com.jme3.math.Quaternion;

import com.jme3.math.Spline.SplineType;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Spatial;

import com.jme3.scene.shape.Box;



public class TestMotionPath extends SimpleApplication {



private Spatial box;

private Spatial teapot;

private boolean active = true;

private boolean playing = false;

private MotionPath path;

private MotionTrack motionControl;



public static void main(String[] args) {

TestMotionPath app = new TestMotionPath();

app.start();

}



@Override

public void simpleInitApp() {

createScene();

cam.setLocation(new Vector3f(8.4399185f, 11.189463f, 14.267577f));

path = new MotionPath();

path.addWayPoint(new Vector3f(0, 0, 0));

path.addWayPoint(new Vector3f(10, 0, 0));

path.addWayPoint(new Vector3f(10 , 5 , 0 ));

path.addWayPoint(new Vector3f(15, 5, 0));

path.addWayPoint(new Vector3f(10, 5, 0));

path.addWayPoint(new Vector3f(10, 0, 0));

path.addWayPoint(new Vector3f(0, 0, 0));

path.setCurveTension(0.2f);

path.enableDebugShape(assetManager, rootNode);



motionControl = new MotionTrack(teapot,path);

motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);

motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));

motionControl.setInitialDuration(10f);

motionControl.setSpeed(2f);



guiFont = assetManager.loadFont(“Interface/Fonts/Default.fnt”);

final BitmapText wayPointsText = new BitmapText(guiFont, false);

wayPointsText.setSize(guiFont.getCharSet().getRenderedSize());



guiNode.attachChild(wayPointsText);



path.addListener(new MotionPathListener() {



public void onWayPointReach(MotionTrack control, int wayPointIndex) {

if (path.getNbWayPoints() == wayPointIndex + 1) {

wayPointsText.setText(control.getSpatial().getName() + "Finished!!! ");

} else {

wayPointsText.setText(control.getSpatial().getName() + " Reached way point " + wayPointIndex);

}

wayPointsText.setLocalTranslation((cam.getWidth() - wayPointsText.getLineWidth()) / 2, cam.getHeight(), 0);

}

});



flyCam.setEnabled(false);

//ChaseCamera chaser = new ChaseCamera(cam, teapot);

ChaseCamera chaser = new ChaseCamera(cam, box);



// chaser.setEnabled(false);

chaser.registerWithInput(inputManager);

initInputs();



}



private void createScene() {

Material mat = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

mat.setFloat(“Shininess”, 1f);

mat.setBoolean(“UseMaterialColors”, true);

mat.setColor(“Ambient”, ColorRGBA.Black);

mat.setColor(“Diffuse”, ColorRGBA.DarkGray);

mat.setColor(“Specular”, ColorRGBA.White.mult(0.6f));

Material matSoil = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

matSoil.setBoolean(“UseMaterialColors”, true);

matSoil.setColor(“Ambient”, ColorRGBA.Black);

matSoil.setColor(“Diffuse”, ColorRGBA.Black);

matSoil.setColor(“Specular”, ColorRGBA.Black);

teapot = assetManager.loadModel(“Models/Teapot.j3o”);

teapot.setName(“Teapot”);

teapot.setLocalScale(3);

teapot.setMaterial(mat);

rootNode.attachChild(teapot);



Box b = new Box(Vector3f.ZERO, .5f, .5f, .5f); // create cube shape at the origin

box = new Geometry(“Box”, b); // create cube geometry from the shape

Material mateo = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”); // create a simple material

mateo.setColor(“Color”, ColorRGBA.Blue); // set color of material to blue

box.setMaterial(mateo); // set the cube’s material

rootNode.attachChild(box);



Geometry soil = new Geometry(“soil”, new Box(new Vector3f(0, -1.0f, 0), 50, 1, 50));

soil.setMaterial(matSoil);



rootNode.attachChild(soil);

DirectionalLight light = new DirectionalLight();

light.setDirection(new Vector3f(0, -1, 0).normalizeLocal());

light.setColor(ColorRGBA.White.mult(1.5f));

rootNode.addLight(light);

}



private void initInputs() {

inputManager.addMapping(“display_hidePath”, new KeyTrigger(KeyInput.KEY_P));

inputManager.addMapping(“SwitchPathInterpolation”, new KeyTrigger(KeyInput.KEY_I));

inputManager.addMapping(“tensionUp”, new KeyTrigger(KeyInput.KEY_U));

inputManager.addMapping(“tensionDown”, new KeyTrigger(KeyInput.KEY_J));

inputManager.addMapping(“play_stop”, new KeyTrigger(KeyInput.KEY_SPACE));

ActionListener acl = new ActionListener() {



public void onAction(String name, boolean keyPressed, float tpf) {

if (name.equals(“display_hidePath”) && keyPressed) {

if (active) {

active = false;

path.disableDebugShape();

} else {

active = true;

path.enableDebugShape(assetManager, rootNode);

}

}

if (name.equals(“play_stop”) && keyPressed) {

if (playing) {

playing = false;

motionControl.stop();

} else {

playing = true;

motionControl.play();

}

}



if (name.equals(“SwitchPathInterpolation”) && keyPressed) {

if (path.getPathSplineType() == SplineType.CatmullRom){

path.setPathSplineType(SplineType.Linear);

} else {

path.setPathSplineType(SplineType.CatmullRom);

}

}



if (name.equals(“tensionUp”) && keyPressed) {

path.setCurveTension(path.getCurveTension() + 0.1f);

System.err.println("Tension : " + path.getCurveTension());

}

if (name.equals(“tensionDown”) && keyPressed) {

path.setCurveTension(path.getCurveTension() - 0.1f);

System.err.println("Tension : " + path.getCurveTension());

}





}

};



inputManager.addListener(acl, “display_hidePath”, “play_stop”, “SwitchPathInterpolation”, “tensionUp”, “tensionDown”);



}

} [/java]

ur trying to go back in reverse, not sure that MotionPath supports that, but I see your problem when attempting to go vertical.

this is so weird, this code works fine in JMP but in Eclipse the spatial restarts to the starting position

[java]path = new MotionPath();

path.addWayPoint(new Vector3f(0, 0, 0));

path.addWayPoint(new Vector3f(10, 0, 0));

path.addWayPoint(new Vector3f(10 , 5 , 0 ));

path.setCurveTension(0.2f);

path.enableDebugShape(assetManager, rootNode);



motionControl = new MotionTrack(teapot,path);

motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);

motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));

motionControl.setInitialDuration(10f);

[/java]

@homsi said:
this is so weird, this code works fine in JMP but in Eclipse the spatial restarts to the starting position

Clean and build your project

nope that doesn’t do it… I even restarted the whole machine, cleaned and compiled.

Maybe you don’t have the same java version for JMP and eclipse…

What are they?

1- I am trying it now with JMP stable and Java ver 1.6 using Eclipse Indigo. This is very strange, I don’t see why it would restart from a previous position b/c of a version difference!?



2- like nightwolf said why the spatial does not go in reverse when going backwards (example he provided)? how can u make it go in reverse rather than turn around?

1- I don’t know why but that would be a lead to follow…JMP or eclipse does not make any difference on what’s happening in the engine…the only thing that can differ is the jre used…



2- tried speed = -1?

thanks @nehon for the answer however:



1- I am using MotionPath, MotionTrack, and MotionPathListener from the latest nightly and still getting that restart behavior that only happens in Eclipse. I am using the same SDK for both, JMP and Eclipse.



2- trying speeed = -1 will have the spatial go in reverse indefinitely till it disappears from the screen.

  1. answer the question please : What jre version do you have with your eclipse project and what jre version do you have with your JMP project?


  2. you can check when it reach the 1st waypoint and stop it.

sorry thought I answered that:



In Eclipse and JMP I am running the same Jre >>>> 1.6.0_21



checked by inputting this code in both projects: [java]System.out.println(System.getProperty("java.vendor"));

System.out.println(System.getProperty("java.vendor.url"));

System.out.println(System.getProperty("java.version"));[/java]



and for 2- evne with a negative speed the spatials are still upside down. Note that I am using a coordinate system where Y and Z are flipped

@homsi said:
Note that I am using a coordinate system where Y and Z are flipped

Why??? it was too easy the normal way?

i'll test nightwolf911 test case for the reverse thing, but, i can't promise it will be this week, I have a lot to do on a personal project.

I started my project like that long time ago and didn’t want to deal with reverting everything back… it would be a lot of work…


i’ll test nightwolf911 test case for the reverse thing, but, i can’t promise it will be this week, I have a lot to do on a personal project.
Thanks so much!!

Any hint where I could start debugging for the spatial restarting in the same position it started?

and @homsi sorry to hijack ur post but we totally have the same problem lol

I can revert them but will that fix the problem? I dont think so :slight_smile:

thanks guys



edit: nightwolf is ur spatial restarting as well?

Hey I know this is an old topic but I’m also experiencing issues with going backwards (negative speed). It never finds the end waypoint and just jettisons off into space when it reaches the beginning. Any help?