Curves tool

Michalis Kamburelis edited this page Apr 29, 2016 · 5 revisions

The engine contains a simple tool to design curves (piecewise cubic Bezier curves). It is located in castle-engine/tools/castle-curves engine directory. The created curves can be saved into a simple XML format and loaded into your own Castle Game Engine programs and used for any purpose (for example as a track along which something moves).

Creating and editing curves

For many commands and their key shortcuts, see the program’s menu. Use mouse to create and edit curve points:

Right mouse button click

Add new curve point. If some curve and point were selected, the new point is added to the selected curve after the selected point. Otherwise, a new new curve with one point is created. So if you want to start creating 2nd curve, just deselect all (key N) and press right mouse button.

Left mouse button click

Select control point (and it’s curve).

Dragging with left mouse button

Move selected point. Hold Shift key to move whole selected curve. The smallest drags are ignored at the begging, so we avoid accidental drags when selecting, don’t worry:)

Use keys + and - to smoothly zoom in/out. Use Home to reset zoom to zero.

Hint: use a reference image (menu Background->Load) to fit your curve nicely to your use. By default we show a simple image with a helpful grid.

Save and open menu commands store curves list in a simple XML file. You can use it to load your designed curves into your Castle Game Engine programs, see below.

Requirements

It’s a GUI program, so it requires common libraries necessary by our engine:

You can download Windows DLLs from the engine page: http://castle-engine.sourceforge.net/engine.php

Using curves in your own programs

  • Load a list of curves from XML file using the TCurveList.LoadFromFile method.

  • Or load the first curve from XML file using the simpler TCurve.LoadFromFile method.

  • Query the curve using the TCurve.Point(T) method.

  • Use T values in the range [0..1] to move along the curve. In general, you should look at the valid range for T in TCurve.TBegin and TCurve.TEnd properties, but in case of castle-curves tool --- you know that TBegin is always 0 and TEnd is 1.

See the example below:

uses SysUtils, CastleVectors, CastleFilesUtils, CastleCurves;
var
  FirstCurve: TCurve;
  Curves: TCurveList;
begin
  FirstCurve := TCurve.LoadFromFile(ApplicationData('my_curves.xml'));
  try
    // That's it, you loaded the 1st curve from XML file.
    // Write some initial curve points.
    Writeln(VectorToNiceStr(FirstCurve.Point(0.0)));
    Writeln(VectorToNiceStr(FirstCurve.Point(0.1)));
  finally FreeAndNil(FirstCurve) end;

  { in more complicated scenarios, my_curves.xml may keep many curves
    inside. Load them like this: }

  Curves := TCurveList.Create(true { free objects });
  try
    Curves.LoadFromFile(ApplicationData('my_curves.xml'));
    if Curves.Count = 0 then
      raise Exception.Create('No curves defined in file');
    FirstCurve := Curves[0];
    // That's it, you have the 1st curve from XML file.
    // Write some initial curve points.
    Writeln(VectorToNiceStr(FirstCurve.Point(0.0)));
    Writeln(VectorToNiceStr(FirstCurve.Point(0.1)));
    Writeln(VectorToNiceStr(FirstCurve.Point(0.2)));
  finally FreeAndNil(Curves) end;
end.