Urho3D
Spline path

The SplinePath component

SplinePath is a component that allows to move a node along a path defined from a series of nodes acting as 'control points'. The node to move is called 'controlled node'.

Building the path

A path is built from ordered points. When setting the points from nodes using AddControlPoint(), an index is used to order them. At least two nodes are required to build a path.

Removing points from the path

Points can be removed:

Assigning the controlled node

The controlled node is assigned using SetControlledNode().

Moving the controlled node along the path

The controlled node is moved manually according to a time step, using Move() in your update function.

Behavior controls

The behavior of the node is mainly influenced by its:

  • speed.
  • interpolation mode used to follow the path. Available modes are BEZIER_CURVE, CATMULL_ROM_CURVE, LINEAR_CURVE and CATMULL_ROM_FULL_CURVE.

Taking manual control of the controlled node

The control node position can be:

  • reset to the starting position (first point in the path) using Reset().
  • set to a given position of the path using SetPosition(). Position is expressed from 0.f to 1.f, where 0 is the start and 1 is the end of the path.

Querying spline path informations

At any time you can query:

Debugging

As for any component, a debugging function is supplied to visually check the component.

Sample code

The following sample demonstrates how to build a path from 2 points, assign a controlled node and move it along the path according to speed and interpolation mode.

// Initial point
Node* startNode = scene_->CreateChild("Start");
startNode->SetPosition(Vector3(-20.0f, 0.0f, -20.0f));
// Target point
Node* targetNode = scene_->CreateChild("Target");
targetNode->SetPosition(Vector3(20.0f, 2.0f, 20.0f));
// Node to move along the path ('controlled node')
Node* movingNode = scene_->CreateChild("MovingNode");
// Spline path
Node* pathNode = scene_->CreateChild("PathNode");
SplinePath* path = pathNode->CreateComponent<SplinePath>();
path->AddControlPoint(startNode, 0);
path->AddControlPoint(targetNode, 1);
path->SetInterpolationMode(LINEAR_CURVE);
path->SetSpeed(10.0f);
path->SetControlledNode(movingNode);

In your update function, move the controlled node using Move():

path->Move(eventData[P_TIMESTEP].GetFloat());