PDA

View Full Version : Bezier curve and Catmull-Rom spline in C++


adamw
01-10-2006, 06:54 PM
So I was using my Google-fu last night looking for these libraries. I couldn't find a shred in C++ (well, alright, a shred, but nothing substantial). What's the deal? I saw a nice library in Java and even some in Perl (and more than one in other odd math languages). I'm sitting here writing my own and it seems downright silly.

Anybody have a library in C/C++ that they favor? Or are we all recreating the preverbial wheel here?

vjvj
01-10-2006, 07:12 PM
I'm not sure you're going to find much, as parametric surfaces are rarely used in games these days (past 7 years or so in fact). The only libraries I can think of that support them are commercial, with Renderware being one example.

Gamasutra has some articles on the subject... Not quite a library per se, but it's a good start.

adamw
01-10-2006, 08:14 PM
Saw the articles. Got the t-shirt. But I'm exceedingly lazy - show me the code! Sometimes Gamasutra is too pedantic.

I'm using these for nice, smooth motions in my decidedly 2D particle engine.

soniCron
01-10-2006, 08:21 PM
I'm not sure I fully understand what you want, but this (http://forums.indiegamer.com/showthread.php?p=66079#post66079) may be what you're looking for. After all, it's just an equation, so I'm not sure what functionality you're looking for in a class.

adamw
01-10-2006, 09:04 PM
Yep. That's a piece of it - thanks. I had hopes of something like this (http://freshmeat.net/projects/curves/) - but in C++.

But really, it's OK. I'm just cranky. I should just get the heck back to work on my particle engine.

techbear
01-11-2006, 07:51 AM
Here's my Catmull-Rom code. Hope it helps!




void PointOnCurve(TrackPoint &out, float t, TrackPoint p0, TrackPoint p1, TrackPoint p2, TrackPoint p3)
{
float t2 = t * t;
float t3 = t2 * t;
out.x = 0.5f * ( ( 2.0f * p1.x ) +
( -p0.x + p2.x ) * t +
( 2.0f * p0.x - 5.0f * p1.x + 4 * p2.x - p3.x ) * t2 +
( -p0.x + 3.0f * p1.x - 3.0f * p2.x + p3.x ) * t3 );
out.y = 0.5f * ( ( 2.0f * p1.y ) +
( -p0.y + p2.y ) * t +
( 2.0f * p0.y - 5.0f * p1.y + 4 * p2.y - p3.y ) * t2 +
( -p0.y + 3.0f * p1.y - 3.0f * p2.y + p3.y ) * t3 );
}

Jim Buck
01-11-2006, 09:30 AM
You could always port the Java implementation if you're unsure how to proceed from scratch.

Leper
01-11-2006, 12:43 PM
Here is the game in action:
http://www.mattmcfarland.com/IntrTrl2.wmv

I have since updated enemy attack patterns and made the bullets look better too. I'm also still tweaking the explosions. My older explosion pics were showing explosions 2x the size they now are in the video. I may change the size again, may not. Just still in the very alpha stages of game development.
Enjoy!

adamw
01-11-2006, 02:36 PM
techbear - Thanks! That helps.

Jim - Am I insane? That Java API is mongo huge! It's also very class-y - meaning you can't just pick out a part and do it. You'd have to bite off a chunk. I want the power, but I don't want to spend that kinda time (at this time).

Leper - Looks pretty cool. I love shmups. Yum!

adamw
01-11-2006, 09:26 PM
Alright! Things are coming along fine. Catmull-Rom works dandy. Now...

Anybody happen to have some Natural Cubic Spline code lying about? I've looked, but it ain't pretty out there in Google land.

ekampf
01-13-2006, 04:05 PM
You can find what you are looking for here:
http://www.geometrictools.com/Curves.html

Regards,
Eran Kampf
http://www.developerzen.com

adamw
01-13-2006, 06:52 PM
You can find what you are looking for here:
http://www.geometrictools.com/Curves.html

Incredible! This is a treasure trove of curving goodness! Thank you very much!