in ansca, Carlos Icaza, Graphics

Moving an object along a Bezier Curve. Part II

Part II of our of our journey in having an object move along a Bezier curve, and moving towards re-parametrizing the curve, we first need to understand a very important aspect of a specific attribute of a Bezier curve and that is computing its tangents via its first derivative.

By using the form on my first post on Beziers,

   \mathbf{P}(t)= (1 -t) ^3 \mathbf{P}_0+3(1-t)^2t\mathbf{P}_1+3(1-t)t^2\mathbf{P}_2+t^3\mathbf{P}_3

we can easily observe the first derivative is obtained as follows

   c'(t)=-3(i-t)^2\mathbf{P}_0+3((1-t)^2-2t(1-t))\mathbf{P}_1+3(2t(1-t)-t^2)\mathbf{P}_2+3t^2\mathbf{P}_3

which in turn can be reduced to

   c'(t)=3(\mathbf{P}_1-\mathbf{P}_0)(1-t)^2+3(\mathbf{P}_2-\mathbf{P}_1)2t(1-t)+3(\mathbf{P}_3-\mathbf{P}_2)t2

lastly, giving us

   c'(t)=3(1-t)^2(\mathbf{P}_1-\mathbf{P}_0)+6t(1-t)(\mathbf{P}_2-\mathbf{P}_1)+3t2(\mathbf{P}_3-\mathbf{P}_2)
local pax = 3*(p2.x-p1.x)*((1-t) *(1-t));
local pbx = pax + 6*t*(p3.x-p2.x)*(1-t)
local pcx = pax + pbx + 3*(p4.x-p3.x)*(t*t);

local pay = 3*(p2.y-p1.y)*((1-t) *(1-t));
local pby = pay + 6*t*(p3.y-p2.y)*(1-t)
local pcy = pay + pby + 3*(p4.y-p3.y)*(t*t);

And why is this important? Because when you want to do text on a path, move the text object along the path, (as well as an object that you want to be perpendicular to the curve) it needs to project away from it. Thus the Y direction is perpendicular to the curve.

Here is an image of the tangents of a curve at a given t, perpendicular to the curve. You can also see the parametrization not being equal in the corner cases of the curve.

Write a Comment

Comment