Pietari
Pietari

Reputation: 83

How to make geometry follow a curve?

Is it possible to make some sprites/objects to copy itself and bend in a curve? I made an image to show what i mean:

road

Here the curve, possibly a bezier path is green and the geometry is shown in black. I want the sprite or object (on the left) to copy itself and merge it's vertices with the last copy, while the last two vertices follow the curve. If it is, how to do it? Is there any documentation on something like this? Have you done something like this? How?

EDIT: I don't want the sprite or object to move through the path, but kind of duplicate itself and merge itself with it's copies.

Upvotes: 5

Views: 847

Answers (2)

Hal Mueller
Hal Mueller

Reputation: 7655

Take a look at SCNShape, which generates a SceneKit geometry from a Bézier curve.

Upvotes: 0

user1118321
user1118321

Reputation: 26395

Yes, what you want to do can work, and your drawing shows how it works fairly well. The pseudocode would look something like this:

curveLength = <length of entire curve>;
tileLength = <length of 1 tile>;
currentLength = 0;
while (currentLength < curveLength)
{
    currentTileLength = 0;
    startPt = prevPt = calculateBezierAt(0.0);
    for (t = delta; (t < 1) && (currentTileLength < tileLength); t += delta) // delta is up to you. I often use 1/100th
    {
        nextPt = calculateBezierAt(t);
        distance = distanceBetween(prevPt, nextPt);
        currentTileLength += distance;
        currentLength += distance;
        prevPt = nextPt;
    }
    endPt = prevPt;
    // Calculate quad here
}

To calculate each quad, you need to generate perpendiculars at the start and end points. You then have 4 points for your quad.

Note that I've simplified things by assuming there's only a single bezier. Normally, you'll have many of them connected together, so it will be a little trickier to iterate over them than I've said above, but it shouldn't be too hard.

Also note that if you have either very tight corners or if the curve loops back on itself you may get bad-looking results. Presumably you'll avoid that if your generating the curves yourself, though.

Upvotes: 2

Related Questions