I'm working on some models that are supposed to follow a 2d path (and Z-rotate accordingly) with varying speeds (for example if it were a horizontally elongated ellipse the models should move slower on the left and right extremes etc). I already implemented some simple paths but now I'd like to test something slightly more complicated, like this:

Is there a simple way to go about it?

## Move on rails (path)

**Moderator:** Moderators

### Re: Move on rails (path)

Hi rrTea,

K

I'd probably go with a quadratic or cubic spline, but curves are generally a personal preference kind-of-deal ( some people might prefer a B-Spline for instance ) Anyway, here's a simple quadratic example:rrTea wrote:Is there a simple way to go about it?

Code: Select all

```
<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" CameraPosition="2 2 4" FileVersion="2">
<OnLoaded>
<ZExternalLibrary ModuleName="opengl32">
<Source>
<![CDATA[//
void glBegin(int mode){}
void glEnd(){}
void glVertex2f(float x, float y){}]]>
</Source>
</ZExternalLibrary>
<ZLibrary>
<Source>
<![CDATA[//
float quadratic(float v1, float v2, float v3, float t)
{
float k = 1-t;
return k*k*v1+2*k*t*v2+t*t*v3;
}
vec2 quadraticSpline(vec4[] spline, float t)
{
int p = spline.SizeDim1-1;
float s = p*frac(t);
float f = frac(s);
int i = s;
vec4 p1, p2;
p1 = spline[i];
p2 = spline[i+1];
return vector2(quadratic(p1.x, p1.z, p2.x, f),
quadratic(p1.y, p1.w, p2.y, f));
}]]>
</Source>
</ZLibrary>
<ZExpression>
<Expression>
<![CDATA[//
Points[0] = vector4(2, 2, 0, 3);
Points[1] = vector4(0, 2, 0, 1);
Points[2] = vector4(2, 2, 4, 3);
Points[3] = vector4(4, 2, 4, 1);
Points[4] = vector4(2, 2, 0, 0);
//
createModel(Box);]]>
</Expression>
</ZExpression>
</OnLoaded>
<OnRender>
<ZExpression>
<Expression>
<![CDATA[// This is just for debugging purposes, never do this in a actual game ;-P
glBegin(0);
for(int i=0; i<128; i++)
{
float t = i/128f;
vec2 p = quadraticSpline(Points, t);
glVertex2f(p.x, p.y);
}
glEnd();]]>
</Expression>
</ZExpression>
</OnRender>
<Content>
<Model Name="Box" Scale="0.25 0.25 0.25">
<OnUpdate>
<ZExpression>
<Expression>
<![CDATA[//
float t = frac(App.Time*0.25);
//
vec2 p = quadraticSpline(Points, t);
//
Box.Position.X = p.x;
Box.Position.Y = p.y;
//
float d, t1, t2;
d = (Points.SizeDim1-1)*0.01;
t1 = frac(t-d);
t2 = frac(t+d);
vec2 p1 = quadraticSpline(Points, t1);
vec2 p2 = quadraticSpline(Points, t2);
Box.Rotation.Z = atan2(p2.y-p1.y, p2.x-p1.x)/PI/2;]]>
</Expression>
</ZExpression>
</OnUpdate>
<OnRender>
<RenderNet RenderVertexExpression="//"/>
</OnRender>
</Model>
<Array Name="Points" Type="8" SizeDim1="5"/>
</Content>
</ZApplication>
```

### Re: Move on rails (path)

At one point I was thinking of doing something similar to this Following Waypoints from an Array but I'll try to implement this example first (previous post) and see what happens.