MotionTrack strange behaviour ^(?)^

So when I run the code below I would expect the box to run on a triangle path in constant speed. The actual behaviour is that till to the first checkpoint the box speeds up extremely then waits ?

[java]package test;

import java.util.Timer;

import java.util.TimerTask;

import java.util.concurrent.Callable;

import com.jme3.app.SimpleApplication;

import com.jme3.cinematic.MotionPath;

import com.jme3.cinematic.MotionPathListener;

import com.jme3.cinematic.events.MotionTrack;

import com.jme3.light.DirectionalLight;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

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 MotionPathTest extends SimpleApplication {

public static void main(String[] args) {

MotionPathTest test = new MotionPathTest();

test.setShowSettings(false);

test.start();

}

@Override

public void simpleInitApp() {

setDefaultLightSettings();

setDefaultCamSettings();

getRootNode().attachChild(createBox());

// delay of motion start to be able to see what’s going on

new Timer().schedule(new TimerTask() {

@Override

public void run() {

enqueue(new Callable<Void>() {

@Override

public Void call() throws Exception {

createMotion().setEnabled(true);

return null;

}

});

}

}, 1000);

}

private MotionTrack createMotion() {

Spatial spatial = getRootNode().getChild(“boxGeo”);

MotionPath path = new MotionPath();

path.setCycle(true);

// how do I start at current position of spatial??

// like it is here, the spatial behaves strangely

path.addWayPoint(spatial.getLocalTranslation());

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

path.addWayPoint(new Vector3f(20.0f, 0.5f, 0));

path.setPathSplineType(SplineType.Linear);

path.addListener(new MotionPathListener() {

@Override

public void onWayPointReach(MotionTrack motionControl, int wayPointIndex) {

System.out.println("Checkpoint: " + wayPointIndex);

}

});

// it is bad that this creates a strange exception when called before

// the way points have been set

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

MotionTrack track = new MotionTrack(spatial, path) {

@Override

public void onUpdate(float tpf) {

// output is also strange when start way point is

// spatial.getLocalTranslation()

// System.out.println(currentValue);

super.onUpdate(tpf);

}

};

track.setDirectionType(MotionTrack.Direction.Path);

return track;

}

private void setDefaultCamSettings() {

flyCam.setMoveSpeed(50);

cam.setLocation(new Vector3f(-20, 20, 20));

cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);

}

private void setDefaultLightSettings() {

DirectionalLight sun = new DirectionalLight();

sun.setDirection(new Vector3f(1, -1, -1));

rootNode.addLight(sun);

}

private Geometry createBox() {

Box box = new Box(Vector3f.ZERO, 0.5f, 0.5f, 0.5f);

Geometry boxGeo = new Geometry(“boxGeo”, box);

Material boxMat = new Material(assetManager,

“Common/MatDefs/Misc/Unshaded.j3md”);

boxMat.setColor(“Color”, ColorRGBA.Gray);

boxGeo.setMaterial(boxMat);

boxGeo.setLocalTranslation(0f, 0.5f, -19f);

return boxGeo;

}

}[/java]

I’ve got it now… I needed to clone the spatials translation to make it work…



[java]path.addWayPoint(spatial.getLocalTranslation().clone());[/java]

mhhh maybe i should clone it in the addWayPoint…This could lead to hair pulling situations…

Dunno…

ah yeh the dreaded reference to the spatial’s properties, has caught me a number of times

Btw. is LoopMode.Cycle not yet implemented?

I’ve encountered the same problem. Motion is not smooth and this soulution:


I’ve got it now… I needed to clone the spatials translation to make it work…

1
path.addWayPoint(spatial.getLocalTranslation().clone());


doesn´t work. I wonder why it should work? I think, there is something else in it.

I have MotionPath with 1 sec delay between points. (glider flight trajectory). The test Spatial (box) goes fine through the waypoints, but it faces in not accurate angle relative to the path. And what is even worse, it unexpectedly changes its facing to the strange rotated position from time to time.
Another problem is that the box stops moving after irregular period of time with only part of path passed through.

Properties set to motionPath and motionTrack:

[java]
path.setPathSplineType(SplineType.Linear);
path.setCurveTension(0);
path.setCycle(false);
...
motionTrack.setDirectionType(MotionTrack.Direction.Path);
motionTrack.setInitialDuration(points[points.length-1].getTime()-points[0].getTime()); //time in seconds
motionTrack.setSpeed(10f);
[/java]

I forgot to post a video :slight_smile:

http://www.youtube.com/watch?v=8doiFecAVe4

@douter said:
I've got it now... I needed to clone the spatials translation to make it work...
[java]path.addWayPoint(spatial.getLocalTranslation().clone());[/java]

@drhml said:
I wonder why it should work? I think, there is something else in it.

Its an issue using the localTranslation Vector directly because it gets updated by the spatial. This means one of the waypoints will always be moving along with the Spatial.

So as I understand, the important path point to clone is the first point of the path, which is supposed to be on the movable Spatial initial position.

Am I right?

I didn’t noticed any behaviour change when I tried anyway…



But I realized, that the Spatial stops moving after as much shorter passed trajectory from the start as the speed is higher.

What should I focus on?

Your points seems to be very close one from another…and you’re using a linear path, so there is not trajectory interpolation at all.

the jittering in the facing might be due to slight variations of the Y values of your points.

Maybe you should just use 1 point out of 10 (or more) and set the path type to catmull-rom.

Thanks, you are probably right. There can be little inappropriate diffs at the Y axis…but its real data and I need it all.

Hmm…Catmull-rom algorithm takes too much memory for my test case and test machine so that it is difficult to test it. Anyway, it doesn’t explain too early end of the motion I think.