- pathNodes.push_back(node);
- }
-
- if (pathNodes.size() < 1) throw std::runtime_error("Path with zero nodes");
-
- // initial position and velocity will be set with the first update, as timeToGo is initialized to 0.
- destinationNode = 0;
-
- // register this path for lookup:
- registry[name] = this;
-}
-
-Path::~Path()
-{
- registry.erase(name);
-}
-
- void
-Path::update(float elapsed_time)
-{
-
- // TODO: carry excess time over to next node? This is how it was done in camera.cpp:
- /*
- if(auto_t - elapsed_time >= 0) {
- translation += current_dir * elapsed_time;
- auto_t -= elapsed_time;
- } else {
- // do the rest of the old movement
- translation += current_dir * auto_t;
- elapsed_time -= auto_t;
- auto_t = 0;
-
- // construct path for next point
- if(auto_idx+1 >= scrollpoints.size()) {
- keep_in_bounds(translation);
- return;
- }
- Vector distance = scrollpoints[auto_idx+1].position
- - scrollpoints[auto_idx].position;
- current_dir = distance.unit() * scrollpoints[auto_idx].speed;
- auto_t = distance.norm() / scrollpoints[auto_idx].speed;
-
- // do movement for the remaining time
- translation += current_dir * elapsed_time;
- auto_t -= elapsed_time;
- auto_idx++;
- }
- */
-
- // advance to next node at scheduled time
- if (timeToGo <= 0) {
- position = pathNodes[destinationNode].position;
-
- // set destinationNode to next node
- if (forward) {
- destinationNode++;
- if (destinationNode >= (int)pathNodes.size()) {
- if (circular) {
- destinationNode = 0;
- } else {
- destinationNode = (int)pathNodes.size()-1;
- }
- }
- } else {
- destinationNode--;
- if (destinationNode < 0) {
- if (circular) {
- destinationNode = (int)pathNodes.size()-1;
- } else {
- destinationNode = 0;
- }
- }
- }
-
- PathNode dn = pathNodes[destinationNode];
- timeToGo = dn.time;
- velocity = (dn.position - position) / timeToGo;
- }
-
- // move according to stored velocity
- last_movement = velocity * elapsed_time;
- position += last_movement;
- timeToGo -= elapsed_time;
-
- // stop when we arrive at our destination
- PathNode dn = pathNodes[destinationNode];
- if ((position - dn.position).norm() < EPSILON) {
- velocity = Vector(0,0);