I finally managed to implement one of the many spline movements I was looking into and lagrange is the one I think will work best for me. However, I have a problem with reversing the prediction.
The lagrange works on a set of waypoints (my use of this is waypoints) but when I want to reverse the system, it "bounces" rather than reverses the wave.
I thought it would be easy to make a variation for a reverse case, but it has stumped me. I'm sure others will benefit from this great interpolation system, but I really need it to be reversible... can anyone help?
Here's an implementation:
I set up an array of co-ordinates which works ok until I need to return in the other direction.
SetVirtualResolution (960,640)
global TestSprite as Integer
global x# as float
type vector2
x# as float
y# as float
endtype
global dim path[10] as vector2
TestSprite=createSprite(0)
setSpriteScale(TestSprite,4,4)
a=createSprite(0)
SetSpriteColor(a,255,0,0,255)
setSpriteScale(a,2,2)
SetSpritePositionByOffset(a,100,160)
path[0].x#=100
path[0].y#=160
a=cloneSprite(a)
SetSpritePositionByOffset(a,200,170)
path[1].x#=200
path[1].y#=170
a=cloneSprite(a)
SetSpritePositionByOffset(a,300,180)
path[2].x#=300
path[2].y#=180
a=cloneSprite(a)
SetSpritePositionByOffset(a,400,130)
path[3].x#=400
path[3].y#=130
a=cloneSprite(a)
SetSpritePositionByOffset(a,500,220)
path[4].x#=500
path[4].y#=220
a=cloneSprite(a)
SetSpritePositionByOffset(a,600,210)
path[5].x#=600
path[5].y#=210
a=cloneSprite(a)
SetSpritePositionByOffset(a,700,230)
path[6].x#=700
path[6].y#=230
x#=100.0
xdir#=3.3
do
print("x="+str(GetSpriteX(TestSprite)))
print("y="+str(GetSpritey(TestSprite)))
if xdir#<0
y#=lagrangerev(x#)
else
y#=lagrange(x#)
endif
SetSpritePositionByOffset(TestSprite,x#,y#)
x#=x#+xdir#
if x#>700.0 then xdir#=xdir#*-1.0
if x#<100.0 then xdir#=xdir#*-1.0
Sync()
loop
end
function lagrange(x0#)
y0#=0.0
for j=0 to 6
t#=1.0
for i=0 to 6
if i<>j then t#=t#*(x0#-path[i].x#)/(path[j].x#-path[i].x#)
next i
y0#=y0#+t#*path[j].y#
next j
endfunction y0#
function lagrangerev(x0#)
y0#=0.0
for j=0 to 6
t#=1.0
for i=0 to 6
if i<>j then t#=t#*(x0#-path[i].x#)/(path[j].x#-path[i].x#)
next i
y0#=y0#+(t#*path[j].y#)
next j
endfunction y0#
You can see I set up another version of the function to test reverse scenarios