Edited By Pixelthief on 11/13/2010
This is a very simple Lua file that can be used in XLua to calculate bezier curves quite easily in MMF2 this is handy for graphical effects and smooth movements. If you're interested in how bezier curves work check here:
http://en.wikipedia.org/wiki/B%C3%A9zier_curve
And this is merely an implementation of de Casteljau's algorithm:
http://en.wikipedia.org/wiki/De_Casteljau's_algorithm
In addition, this can normalize any generic bezier curve to find equidistant points to generate a constant velocity movement along this path. This is particularly handy in games, as camera controls and unit movements often want to follow a visually pleasing path (bezier curve) but don't want to move with pseudoNDA acceleration.
It has the following controls:
function addBezierNode(x,y)
When invoked, this adds a node to the bezier curve. This should be used for the first and last nodes, done before adding any helper nodes or just add each in order. The X/Y parameters are the position for this node
function addBezierHelper(x,y)
This adds a node second to the last on the chain. This is useful if you want to declare your endpoints first with addBezierNode, and then add any amount of helper nodes in order without changing the endpoints.
function adjustBezierNode(x,y,index)
this modifies the node at the given index. Keep in mind this is a 0based index, and helper nodes are inserted outoforder at second to the last when used. Your start will always be at 0, endpoint at (# of nodes  1)
function clearBezierNodes()
this resets the list of nodes.
function calculateBezier(t)
This returns the position along the curve at timestep t. T should be given as a floating decimal [0,1]. Please note that you'll need to use XLua's "Pass float" instead of "Pass integer" for this input. The position is then returned via a DoCall, set to function "ReturnBezier". Its return parameters are the X and Y position in that order.
function initBezierMove(dist)
This initializes and resets the integrated bezier movement, starting at the first node and incrementing by roughly 'dist' pixels each step.
function incrBezierMove()
This moves the index ahead by approximately the initialized amount of pixels a single time, and generates a callback returning the X & Y position on the curve at the current timestep. In other words, this moves X pixels along the line.
function fullBezierMove()
This iterates through the entire curve, generating one callback for each node at roughly X pixels apart, where X was initialized. It will automatically halt when it reaches the end of the curve.
Feel free to use this for whatever purposes you want without credit! Or if you want, credit de Casteljau. Please note that this is a recursive algorithm with O(N^2) time complexity, so while it is quite fast with 5 nodes or so, you can easily break your program if you try any silly curves.
In addition the bezier curve integration feature is NOT a closed solution 'quick method' as no such solution exists. As such, it is very processing intensive, and I would recommend moderation in its use! For reference invoke the O(N^2) de Casteljau's algorithm twice per node, making it O(N^3)! And using N square roots, for what thats worth.
Will require XLua to open!
Review This Download
http://sites.google.com/site/claniraq/stuff/BezierTester3.mfa (57 kkb )
