Question

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?

Était-ce utile?

La solution

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

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

Autres conseils

Instead of transforming the list to an array every frame, which seems to me very inefficient, you can calculate the estimated number of vertices you will need to display the trail, create an array of that size and fill it with more values each frame. (One of the argument you pass to DrawUserPrimitives is the number of primitives to draw from the array)

If you can't calculate this, you can also try setting a max length the trail can reach, so that when it reaches that length, it starts overriding the old values. This shouldn't matter much if the camera follows the ball around. If none of these solutions fit your needs, go ahead and use the ToArray function.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top