Search code examples
c#xnapath-finding

Drawing Ball Flight using XNA


I am currently making a golf ball game using C# and XNA. So far I have been able to make the ball fly, bounce, and roll, while displaying the position and velocity. Now I want to be able to see the ball path (similar to images below), in order to check the whole ball movement.

http://www.golf-simulators.com/images/ms4.jpg

My code updates the ball position at a fixed time interval. I am thinking of:

  1. using these position,
  2. insert them to an array of VertexPositionColor[],
  3. and draw them using GraphicsDevice.DrawUserPrimitives() method

but I have no luck so far, the VertexPositionColor is static array, while the amount of position data is always increasing.

I am thinking of using List, but the GraphicsDevice.DrawUserPrimitives() method refuse to take that as an input

    protected override void Draw(GameTime gameTime)
    {

    trail_vertice = new VertexPositionColor;
    trail_vertice.Position = Position;
    trail_vertice.Color = Color.Black;
    trail_amount += 1;
    trailList.Add(trail_vertice);
        
    basicEffect.CurrentTechnique.Passes[0].Apply();
    graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, trailList, 0, trail_amount);

    }

I do not need advanced effects like using particle engine. Just a single line is enough. Any suggestion?


Solution

  • The C# List<> class has a handy .ToArray() function on it that you can call each time you want to draw the primitives.

    graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, trailList.ToArray(), 0, trail_amount);