
Bezier splines between way points
Hi!
im coding some path finding algorithm to just test my skills. anyway , my pathfinding algorithm is based on way points along the way , so in order to make my units movement look somewhat realistic and smooth i tried to connect the way points with bezier splines. However , the movement delta''s are not equal along the way, so when the bezier start to curve the unit moves slower than when the units moves on the "more" linear parts of the bezier (as i expected).
so... does anyone knows/have any idea how can i manipulate it so it will move equally on the whole bezier? coz its really looks cool
.
thanks.

------------------------------- Goblineye Entertainment------------------------------
If you love maths, then you can always use the arc length formula: it is the integral from a to b of sqrt(f''(x)^2 + 1). Find t such that the arc length is a constant.
I haven''t really worked with splines, but this is probably a hard solution. It''s probably much easier to find the derivative vector of your spline at your unit''s position, and assign it to your unit''s direction. Then, the unit can easily move at constant speed. The hard part will probably be to find where your unit is on the curve, especially since it will no longer be following it exactly.
Good luck!
Cédric
I haven''t really worked with splines, but this is probably a hard solution. It''s probably much easier to find the derivative vector of your spline at your unit''s position, and assign it to your unit''s direction. Then, the unit can easily move at constant speed. The hard part will probably be to find where your unit is on the curve, especially since it will no longer be following it exactly.
Good luck!
Cédric
hey , thanks for replaying
.
lets me see if i understood you:
lets say we are talking about a bezier built from 3 points , so those are the three macros of the three functions that builds the bezier spline equation:
#define F1(t) (1 - t)*(1 - t)
#define F2(t) 2*t*(1 - t)
#define F3(t) t*t
so in order to get x and y values on the spline i''ll do that:
spline_x = p1->x * F1(t) + p2->x * F2(t) + p3->x * F3(t);
spline_y = p1->y * F1(t) + p2->y * F2(t) + p3->y * F3(t);
now , what you are says is that i should find such t so the unit speed will be constant right?
and that with using the integral of this function:
sqrt(f''(x)^2 + 1).
well , i dont really understood how it completes to the whole picture , i''ll be glad if you will be able to expline me how to use it
.
thanks a lot.

lets me see if i understood you:
lets say we are talking about a bezier built from 3 points , so those are the three macros of the three functions that builds the bezier spline equation:
#define F1(t) (1 - t)*(1 - t)
#define F2(t) 2*t*(1 - t)
#define F3(t) t*t
so in order to get x and y values on the spline i''ll do that:
spline_x = p1->x * F1(t) + p2->x * F2(t) + p3->x * F3(t);
spline_y = p1->y * F1(t) + p2->y * F2(t) + p3->y * F3(t);
now , what you are says is that i should find such t so the unit speed will be constant right?

and that with using the integral of this function:
sqrt(f''(x)^2 + 1).
well , i dont really understood how it completes to the whole picture , i''ll be glad if you will be able to expline me how to use it

thanks a lot.
------------------------------- Goblineye Entertainment------------------------------
quote:
well , i dont really understood how it completes to the whole picture
It doesn''t. I''m sorry. I was just throwing an idea, but I tried applying it, and I realized (5 seconds later) that there is (AFAIK) no way you are going to be able to find an analytical answer to your problem, at least not with the technique I gave you.
I still think that the second solution can work, but maybe are you better off solving the distance equation approximately through iteration. The basic idea remains to travel a constant distance for each frame (assuming the time between each frame is constant):
1. First guess: Try some "random" t value (use the solution for the last frame if available)
2. Check if the distance between the new position and the old position is smaller or larger than the desired distance
- if smaller, multiply t by two
- if larger, divide t by two
3. Repeat number two until you are close enough to the answer
In pseudo-code,
t0 = current t position on the curve
t = t0 + dt (
do{
MoveTo(t)
Distance = [Distance between new position and old position]
dt = dt / 2
if(Distance < DesiredDistance){
t = t + dt
}else{
t = t - dt
}
}while(DesiredDistance - Distance > limit)
next_frame_dt = t - t0
This solution won''t work if the initial guess is less than half of the real answer, but you can adapt it.
I hope this post is more useful than the preceding...
Cédric
ok , i got you know , i''ll try to implement it , thanks for the help

------------------------------- Goblineye Entertainment------------------------------
What you are trying to do is called "arc length parameterization", which requires integral calculus.
Unless it is generated at runtime and perhaps even then I would suggest converting it to a series of line segments. Then use the lengths of the line segments to approximate the arc length of the curve.
Keys to success: Ability, ambition and opportunity.
Converting it to line segments? Hmm... The OP asked for a trajectory. Wouldn''t it be just more trouble maintaining the segment list? And since this is just an exercise, speed is probably not much of a concern.
On a related note, I just thought that it could be considerably faster to interpolate between the bounds of the interval rather than using the current algorithm. The idea is to keep the minimum and maximum t of the interval, and then to guess a t between those two. If the length at the new point is larger than the desired length, replace the maximum bound, else replace the minimum bound. The educated guess t can be interpolated by
new_t = min_t + (desired_length - max_t_length) / (max_t_length + min_t_length) * (max_t - mint_t)
I think this is the idea behind the Newton-Raphson algorithm.
This should improve speed slightly. It''s probably not important at all, but I felt like writing tonight
Cédric
On a related note, I just thought that it could be considerably faster to interpolate between the bounds of the interval rather than using the current algorithm. The idea is to keep the minimum and maximum t of the interval, and then to guess a t between those two. If the length at the new point is larger than the desired length, replace the maximum bound, else replace the minimum bound. The educated guess t can be interpolated by
new_t = min_t + (desired_length - max_t_length) / (max_t_length + min_t_length) * (max_t - mint_t)
I think this is the idea behind the Newton-Raphson algorithm.
This should improve speed slightly. It''s probably not important at all, but I felt like writing tonight

Cédric
So what is a general formula for the anti-derivative of (a*t^4 + b*t^3 + c*t^2 + d*t + e)^(1/2)? Finding a formula for the arc length requires finding that anti-derivative. It is the square root that screws you over and screws you over in almost every practical situation where you need to derive a formula for the arc length. The suggestion is to use numeric integration. Using line segment lengths is numeric integration of the arc length. The exact arc length is the limit as the number of segements goes to infinity.
Keys to success: Ability, ambition and opportunity.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement