Archived

This topic is now archived and is closed to further replies.

ehmdjii

combining cubic bezier splines

Recommended Posts

hello, im using cubic bezier splines to define a fly-path for a spaceship. each spline consists of four control points and technically when the tangents in the last point and in the first point of the next spline are of same length and direction each spline should go over into the next one smoothly. but in my case they are not. this is the code im using: XYZ CubicBezier(XYZ p0,XYZ p1,XYZ p2,XYZ p3,double mu) { XYZ a,b,c,p; c.x = 3 * (p1.x - p0.x); c.y = 3 * (p1.y - p0.y); c.z = 3 * (p1.z - p0.z); b.x = 3 * (p2.x - p1.x) - c.x; b.y = 3 * (p2.y - p1.y) - c.y; b.z = 3 * (p2.z - p1.z) - c.z; a.x = p3.x - p0.x - c.x - b.x; a.y = p3.y - p0.y - c.y - b.y; a.z = p3.z - p0.z - c.z - b.z; p.x = a.x * mu * mu * mu + b.x * mu * mu + c.x * mu + p0.x; p.y = a.y * mu * mu * mu + b.y * mu * mu + c.y * mu + p0.y; p.z = a.z * mu * mu * mu + b.z * mu * mu + c.z * mu + p0.z; return(p); } mu is the timer. then my control points look something like this: 1st spline: 5,0,-5 5,0,6 -6,0,6 -5,0,-8 2nd spline: -5,0,-8 -4,0,-22 -20,0,-10 -10,0,0 shouldnt this work? thanks for any tips!

Share this post


Link to post
Share on other sites
You are correct. For a Bezier (capitalized since its a person''s name) spline if the endpoints match and the tangents are identical, the two splines should give be C1 continuituous at the common point. And, your points are good and should provide this.

So, there is most likely a coding error. Now, I checked out your code below and it looks fine. The curve seems correct. So, my guess is that it is mu is wrong. You need to make sure that the value of mu you send to the function is 0.0 when you want to be at the first point of the curve and 1.0 when you want to be at the last point of the curve. This means each time you jump from one spline to the other you have to subtract off the time when you reach the first point of that spline. So, say you reach the second spline at 17.3 seconds. Then the value of mu to send the function will be:

mu = (time - 17.3) * some_scaling_factor

As long as you stay on the spline use that function, e.g., as long as the new mu that you calculate is <= 1.0 continue to use 17.3.

Then, when mu > 1.0, you would switch off to a new spline. If the scaling factor is 1.0 (more later) then the next switch will occur at 18.3 seconds and the new formula for mu on the next spline will be:

mu = (time - 18.3) * some_scaling_factor

until mu > 1.0 again, etc.

In general,

mu = (current_time - time_when_object_reached_beginning_of_spline) * some_scaling_factor

Now, that scaling factor might be 1.0. You can try that and at least the object will follow the curve smoothly. Its something to start with. But, the funny thing about interpolating along curves is that the object''s velocity will not remain constant. If you choose 1.0 as your scaling factor then you will indeed see your object speed up and slow down. If you wish to have the object travel at a constant speed then you will have to create another curve that maps time to position along the curve based on the curvature along the curve. You can read more about that here (since I don''t have time to go into that discussion myself now):

Dave Eberly on arc length parameterization of splines for constant velocity motion

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites
thanks, that was it!

i used mu from 0 to 1 for the camera but a mu+0.1 for the spaceship

(so the spaceship is always ahead of the camera)

now camera and spaceship have their own mu. :-)

thanks again!

Share this post


Link to post
Share on other sites