Programmatically add objects

Aug 3, 2011 at 11:06 AM

Hi Nich

Sorry if I'm overloading you with questions :)

I'm trying to add an ellipse with physics programmatically (C#). The very ellipse is easy enough, but how do I add a CircleBody to this afterwards? Or is it the other way around? Should the gameloop be stopped during this creation?

Here's my feeble attempt:

 public void CreateEllipse()
        {
            Ellipse ellipse= new Ellipse();
            ellipse.Height = 50;
            ellipse.Width = 50;
XamlPhysics.WPF.CircleBody ellipseBody = new XamlPhysics.WPF.CircleBody();
XamlPhysics.WPF.PhysicalBox.SetBody(ellipse, ellipseBody);

PhysicsRoot.Children.Add(ellipse);
}

Coordinator
Aug 3, 2011 at 10:45 PM

It looks like your doing it correctly except that you need to make a call to PhysicalBox.FindNewBodies. Otherwise it will never know it's there. Also until you have have fix I mentioned in another thread, you should probably set the position to something even if it's 0,0 with Canvas.SetLeft and Canvas.SetTop.

The gameloop should probably be stopped. The physics engine will probably not like bodies added in the middle of a step.