Jmonkey animation-jump to waypoint

116 views Asked by At

I’ve been messing with this for a while and can not seem to find out how to do this. The problem is that when i start a MotionControl, it will Always spawn the spatial at the first waypoint and move from there. However, I really need to make the spatial and motion to start at a user-defined waypoint (for example: waypoint 10) and go from there.

Could you help?

Thanks

1

There are 1 answers

0
1000ml On

The only method i can find in MotionEvent that actually changes the position on the track is setTime(float). Sane values lie between 0.0f (0%) and initialDuration (100%).

Each time you add a waypoint to a MotionPath it will update the underlying Spline, its total length and the lengths of each segment.

Using this information you can calculate the corresponding "time marks" for your waypoints:

public float calcWaypointTime(MotionPath path, MotionEvent motionEvent, int waypointIndex) {
    // Distance between start and waypoint
    float wpDistance = 0;

    // Sum up segment lengths until waypoint is reached
    List<Float> segLen = path.getSpline().getSegmentsLength();
    for(int i=0; i<segLen.size() && i<waypointIndex; ++i)
        wpDistance += segLen.get(i);

    // Transform distance to time
    return (wpDistance / path.getSpline().getTotalLength()) * motionEvent.getInitialDuration();
}