Creating a path for objects to follow

BlitzMax Forums/BlitzMax Beginners Area/Creating a path for objects to follow

How do you create a destined path for an object to follow?

If I were to draw by freehand a path, how would I get an object to follow that exact path?

Seeing as how I can think of many ways it could be done, what do you think would be most efficient?

Hmm, plotting your points and getting your object to move from Point A to Point B etc. Obviously, the more points you have, the smoother your path will be. You will probably be better off using types to track each point of your path :)

Hmm, plotting your points and getting your object to move from Point A to Point B etc. Obviously, the more points you have, the smoother your path will be. You will probably be better off using types to track each point of your path :)

Sorry, i must have hit the post button twice ^_^

Point Plotting! Nice.. So I can code the plots and how they affect the ship when it reaches the plot. As of now I can't think of how to do bezier curves or anything.. Any suggestions?

I think bezier curves may have something to do with using sin and cos but yes, you can control what happens when your object reaches a certain point on your path by returning an id of that particular point.

There's plenty of code in the archives showing how to work with Bezier curves or splines as they are also called. They are very easy to use and give excellent (i.e. natural) results for paths. And... you don't need sin and cos functions in order to work with splines...

Barney

so the keyword I need to search for is splines.. the word we call splines.

Here's a function for plotting bezier curves. cp0-cp3 are your control points and t is a value from 0 to 1. The idea is to decide how many steps will be needed (the more steps, the smoother the line) then call BezierPoint to get the x-y point at that step.
Type TPoint
 Field X:Int
 Field Y:Int
End Type

Function BezierPoint:TPoint(t:Float, cp0:TPoint, cp1:TPoint, cp2:TPoint, cp3:TPoint)
Local ax:Float, bx:Float, cx:Float
Local ay:Float, by:Float, cy:Float

Local TSquared:Float, TCubed:Float
Local PointResult:TPoint = New TPoint

cx = 3.0 * (cp1.x - cp0.x)
bx = 3.0 * (cp2.x - cp1.x) - cx
ax = cp3.x - cp0.x - cx - bx
        
cy = 3.0 * (cp1.y - cp0.y)
by = 3.0 * (cp2.y - cp1.y) - cy
ay = cp3.y - cp0.y - cy - by         
tSquared = t * t;
tCubed = tSquared * t;
        
Pointresult.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp0.x;
Pointresult.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp0.y;
        
Return Pointresult
End Function

Here is an example program using it.
Const NumPoints = 100
Const td:Float = 1.0/NumPoints
Include "Bezier.bmx"

Graphics 640,480,32

Local cp0:TPoint = New TPoint
Local cp1:TPoint = New TPoint
Local cp2:TPoint = New TPoint
Local cp3:TPoint = New TPoint
Local result:TPoint

cp0.x = 0
cp0.y = 300
cp1.x = 100
cp1.y = 200
cp2.x = 500
cp2.y = 200
cp3.x = 600
cp3.y = 300

While Not KeyHit(KEY_ESCAPE)
 If KeyDown(KEY_LEFT)
  cp1.x :- 1
 End If
 If KeyDown(KEY_RIGHT)
  cp1.x :+ 1
 End If
 If KeyDown(KEY_K)
  cp2.x :- 1
 End If
 If KeyDown(KEY_L)
  cp2.x :+ 1
 End If
 If KeyDown(KEY_UP)
  cp1.y :- 1
 End If
 If KeyDown(KEY_DOWN)
  cp1.y :+ 1
 End If
 If KeyDown(KEY_O)
  cp2.y :- 1
 End If
 If KeyDown(KEY_COMMA)
  cp2.y :+ 1
 End If


 Cls
 SetColor 255,255,255
 For i = 0 Until numpoints
  t:Float = i*td
  result = BezierPoint(t,cp0,cp1,cp2,cp3)
  Plot(result.x,result.y)
 Next
 SetColor 255,0,0
 DrawRect(cp0.x-2,cp0.y-2,4,4)
 DrawRect(cp1.x-2,cp1.y-2,4,4)
 DrawRect(cp2.x-2,cp2.y-2,4,4)
 DrawRect(cp3.x-2,cp3.y-2,4,4)
 FlushMem(); Flip
Wend

Just use the arrow keys and KLO, to move the control points around