gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > 3D path - to use 2 bezier curves, or to use something else?

#158842 - DiscoStew - Thu Jun 19, 2008 6:59 am

At first, I was thinking I could get away with using just a 4D Bezier curve for my 3D path, having 1 value for the Zaxis, and the other 3 for positioning, which will in turn calculate the Y and X axis via position differencing and using Arctan, and for the most part, it worked.

But as I continued, I found that unless I wanted to stay within the limit of that method (to basically be limited to -90? < x < 90?), I'd have to find another method, because when using Arctan, going past those limits can make the angle jump, and even when that is corrected for the most part, the actual orientation is not known (from my perspective).

My latest idea was to just have 2 3D Bezier curves, one set for positioning, and the other for direction, and just make the direction curve match up with the position curve. Before I go full out on this idea, I'd like to know everyone else's idea about 3D paths, and what you all think might be better to work with. Basically, I want to be able to have a 3D path that an object follows, that rotates along the path whichever way it curves, including being able to go in the opposite direction, and be rotated to match it. 2 3D Bezier curves should do the trick, but I'd like to know what everyone else thinks.
_________________
DS - It's all about DiscoStew

#158847 - silent_code - Thu Jun 19, 2008 10:41 am

Did you look into hermite spline curves?
And didn't you post that already some time ago? There has been a very similar topic... ;^)
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#158914 - DiscoStew - Fri Jun 20, 2008 8:21 pm

silent_code wrote:
Did you look into hermite spline curves?
And didn't you post that already some time ago? There has been a very similar topic... ;^)


Heh, I guess you could say the two topics are similar. While the last one was how I could get Bezier curves to act a certain way, this one was for if I should even use Bezier curves in the first place. Of course you gave me the same answer, which is to try hermite spline curves. Considering this is the 2nd time you said this, I should at least try it. Looking around for it, I believe this is the correct formula....

Code:
p(t) = (2t? - 3t? + 1)p1 + (-2t? + 3t?)p2 + (t? - 2t? + t)m1 + (t? - t?)m2

where...
p1 & p2 = points
m1 & m2 = tangents
t = time (from 0 to 1)


Pretty similar to Bezier curves, except that instead of 2 added points to work off of, it uses 2 tangents for direction and speed of the curve going in and coming out.
After a little tweaking, I added it to my code, so that I only need to do coefficient calculations once, and would need to calculate the powers and multiple them by the coefficients at any point on the curve to get to resulting point...

(pseudo code)
Code:

(coefficients calculated once and saved)
coeff[0] = (2 * (p1 - p2)) + m1 + m2;
coeff[1] = (3 * (p2 - p1)) - (2 * m1) - m2;
coeff[2] = m1;

(calculation of curve between [and including] 0 and 1)
tSquared = t * t;
tCubed = t * tSquared;
pResult = (coeff[0] * tCubed) + (coeff[1] * tSquared) + (coeff[2] * t) + p1;

where...
tSquared & tCubed = powers of t
   


Then there is the inclusion of the number of points on the curve itself, so just divide that number from 1 for the "dt" (and save with the coefficients), and then (with a value between 0 and the number of points on the curve) multiply that value by "dt" for a time between (and including) 0 and 1, and continue from there.

I believe I have the calculation correct, so I'll give it a try later on today.
_________________
DS - It's all about DiscoStew

#158918 - silent_code - Fri Jun 20, 2008 11:32 pm

I wasn't sure if it was really you who make that earlier thread, but for the heck of it I posted it again. ;^)

That sounds fine. It's been a few years since I last played with curves in uni, so I'm of no big help right now. But I'd really appreciate to read about the results you'll get. :^)

Happy programming! :^)
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#159035 - DiscoStew - Tue Jun 24, 2008 5:58 pm

Been busy the past couple of days, but I was able to add it into my project, and to see the results. At the moment, it seems to act somewhat like Bezier Curves, but that is only because of how I'm using them for the time being. The code works at least. It is kinda cool though that the only difference between Hermite Curves and Bezier Curves (in my project) is how the coefficients are calculated, so I basically rolled them together into the same function.
_________________
DS - It's all about DiscoStew