I’m making a particle game with (So far, only water), and the Draw() method inside an element is quite slow with a few going at once. I assume it’s because it’s calculating so much every second, but I’m still quite unhappy with it, considering they’re just basic 2D shapes. How can I make it more efficient?
Here’s my code:
(Loop) (Ran every 50 miliseconds) (Note, I just included Forms for the timer, I’m using WPF.)
void TimeKeeper_Tick(object sender, EventArgs e)
{
foreach (Element ele in Elements)
{
ele.Draw();
}
}
And draw method. Pos is a “Vector2” (Class I made to save me time):
public void Draw()
{
Pos.Add(3, 10);
if (Pos.Y > (int)canvas.ActualHeight)
{
Pos.Set(Pos.X, (int)shape.RenderSize.Width);
}
if (Pos.X+shape.RenderSize.Width > (int)canvas.ActualWidth)
{
Pos.Set(0, Pos.Y+(int)shape.RenderSize.Height);
}
shape.SetValue(Canvas.LeftProperty, (double)Pos.X);
shape.SetValue(Canvas.TopProperty, (double)Pos.Y);
if (canvas.Children.IndexOf(shape) != null)
{
canvas.Children.Remove(shape);
}
canvas.Children.Add(shape);
}
A lot depends on what you consider ‘slow’
If you move the shapes every 50 ms you have to move them more each interval to make them go faster.
But adding a timer like you did is not the best way to create animations in WPF.
There are a couple of options:
specify a speed for each shape (something like: 10 units per second), then with each update of the screen move the shapes according to the time that passed since the last time it has been moved and its speed. So you need to keep track of the time elapsed too.
Create Storyboards, they have their own timers (timelines) so all you have to do is set the start and the end position and specify the duration and finally Play the animation.