1
votes

Explanation of the problem

The problem I face is pictured above. Basically what I want is to create a fluent bend between two tracks. Position B(the start of the next track) is variable and determines what part of the circular track should be drawn. What I want to end up with is two tracks with a fluent bend between them by emitting part of the circle.

NOTE: the circle is a seperate sprite as are the two tracks. I simply want to omit a part of the circle sprite

I asked a similar question earlier, but I felt like that was a bit unclear without a picture. XNA: How can I draw only a "pizza slice" of my sprite?

Does anyone have a good idea how to achieve this effect?

1
What's wrong with answer to your previous question? Look reasonable...Alexei Levenkov

1 Answers

2
votes

Build a triangle strip that looks like the following.

o-o-o-       -o-o
|\|\|\ . . . \|\|
o-o-o-       -o-o

Given inner radius r1, outer radius r2, starting angle a, ending angle b, you can generate your vertices as follows.

r * [cos(t); sin(t)],    for r in {r1, r2}, t in [a, b]

Number the vertices as follows.

1-3-5-7-
|\|\|\|\ . . .
0-2-4-6-

Then given vertices 0, 1, ..., 2*N-1, the triangles in your triangle strip correspond to the following 3-tuple rows of vertex indexes.

  0     1     2
        1     2     3
              2     3     4
                    3     4     5
                          4     5     6
                                      .
                                            .
                                                  .
                                                2*N-3 2*N-2 2*N-1

Because of the obvious repetition and pattern here, XNA and graphics in general encode the vertex indexes of a triangle strip as the more-compact array [0, 1, . . ., 2*N-1] instead.

I'll let you figure out the XNA-specific portion of the problem from there. Microsoft's example is pretty straightforward:

var N = 4;
var points = 2 * N;
var vertices = new VertexPositionColor[points];

for (int x = 0; x < points / 2; x++)
{
    for (int y = 0; y < 2; y++)
    {
        vertices[(x * 2) + y] = new VertexPositionColor(
            new Vector3(x * 100, y * 100, 0), Color.White);
    }
}

var indices = new short[points];

for (int i = 0; i < points; i++)
{
    indices[i] = i;
}

GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
    PrimitiveType.TriangleStrip,
    vertices,
    0,          // vertex buffer offset to add to each element of the index buffer
    points,     // number of vertices to draw
    indices,
    0,          // first index element to read
    points - 2  // number of primitives to draw
);