Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Curvy Lines, Beizers, Splines?

This thread is locked; no one can reply to it. rss feed Print
Curvy Lines, Beizers, Splines?
MSGleader
Member #6,637
December 2005

Im trying to make a random landscape generator for my game, sortof like the terrain from the game worms, and i want it all curvy and stuff. so i need to make a curvy line. ive looked up making lines with curves and stuff, but none of it makes sense. could someone please explain to me how to draw splines?

HoHo
Member #4,534
April 2004
avatar

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

miran
Member #2,407
June 2002

Bezier: You have 4 points. You draw a line that starts in point 1, goes close to points 2 and 3 and ends in point 4. There's a formula that you use to calculate intermediate points. Basically you have a parameter that goes from 0 to 1 and the formula calculates the position of an intermediate point for each value of the parameter. For 0 it returns point 1, for 1 it returns point 4 and for every value between 0 and 1 it returns some point between points 1 and 4. You make a loop in which the parameter goes from 0 to 1, calculate intermediate points and draw straight lines between calculated intermediate points. If the granularity is fine enough, the result looks like a smooth curve.

The problem is how to connect multiple segments so that they connect smoothly. There are many methods to do that, each with its good and bad points. One such method is demonstrated in one of the examples that comes with Allegro. Others can easily be found on Google and Wikipedia.

--
sig used to be here

Ceagon Xylas
Member #5,495
February 2005
avatar

While we're on this topic, anyone know of a library that draws anti-aliased primitives and/or polygons?

miran
Member #2,407
June 2002

How is that on topic? ???

I have written some antialiased routines like aa_putpixel (takes in floats), lines, circles, ellipses, etc. and I think I even posted them somewhere. You can probably find the code if you search the forum. But it's a bit on the slow side. If you want proper AA, just use OpenLayer and turn AA on...

--
sig used to be here

Ceagon Xylas
Member #5,495
February 2005
avatar

Quote:

How is that on topic? ???

Hahaha... Well, we were talking about drawing primitives... And it made me think of some ideas that I'd want AAed primitives for :P

Quote:

If you want proper AA, just use OpenLayer and turn AA on...

I really want to leave out OpenLayer for now. I've had too many problems with it in the past.

Johan Halmén
Member #1,550
September 2001

As miran said, you need four points to draw one Bizarre curve. To draw a 2d landscape you just need more of these four point segments. And you need to connect them.

Say that you have two Boozier curves and eight points, a1, a2, a3, a4, b1, b2, b3 and b4. Your curve starts at a1, heads towards a2, turns to wards a3 and hits a4, which is same point as b1. Then it continues towards b2 and b3 to end in b4. Note that the curve doesn't touch a2, a3, b2 and b3.

If you want the smooth connection at a4 (=b1), you have to place a3, a4 and b2 on same line. That will do the trick. The connection is even smoother (or kind of symmetric) if the distance a3-a4 is same as a4-b2.

To draw the landscape, put out the points a1, b1, c1, d1 etc. Then you can do the following to determin the control points a2, a3, b2, b3 etc:

The line from a3 to b2 should be parallel with the line from a1 to c1. The distance from a3 to a4 could be a third or a fourth of the distance between a1 and a4. And that's all, actually. Draw everything on a paper and it will be clear. a2 is an exception, since it is in the end of the curve. You could place it f.i. on a line between a1 and a3, at a fourth.

I should have typed Bezier, but couldn't help change the words to what Spell Check suggested :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

miran
Member #2,407
June 2002

I think it's actually Bézier, but the spellchecker doesn't know that either...

But I was thinking, for a 2D landscape maybe you don't need (all) the segments to connect smoothly. Maybe it would actually look better if there were a few sharp edges here and there...

--
sig used to be here

Ceagon Xylas
Member #5,495
February 2005
avatar

I believe the original worms was like that... 'Sharp' edges.

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

I really want to leave out OpenLayer for now. I've had too many problems with it in the past.

What kinds of problems? Every problem can be solved when it's known. OpenLayer has improved all the time, so maybe one day it suits for you as well...

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Neil Walker
Member #210
April 2000
avatar

If you read up on catmull -om splines, they kind of guarantee to go through every point you specify, whereas bezier doesn't.

I don't have any code, but if you want it I can give Graham Goring a should who uses it for his sprite pattern movements.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

miran
Member #2,407
June 2002

Quote:

catmull -om splines

What he meant to say was Catmull-Rom.

--
sig used to be here

Johan Halmén
Member #1,550
September 2001

My method guarantees to go through every point I specify. What I specify are the points a1, b1, c1, d1 etc. My path goes through all these, smoothly. But to do that I calculate the points a2, a3, b2, b3 etc. And a4 = b1, b4 = c1 etc. Then I do the Bézier.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

MSGleader
Member #6,637
December 2005

can someone post a mini example of code, even if the code doesnt work, i just need an idea on how to do just a bezier with points i set, not random ones yet, and the "exspline.c" is really hard to understand whats what.

thanx guys

edit: nevermind guys! i got it to work! :)

int points[8] = {
        25,480,
        125,300,
        225,200,
        325,480,
        };
    spline(buffer,points,makecol(255,255,255));

woot!

Go to: