Dynamically creating physics objects in WPF

Sep 8, 2009 at 9:41 AM
Edited Sep 8, 2009 at 9:44 AM

Absolutely loving your work here, but I've ran into a problem...

I'm trying to make a behaviour that's effectively a particle emitter, but I can only use it to create rectangles. Unfortunately I kept failing and ended up trying to match the silverlight examples (Ragdoll example where you click the button to create more dolls).

In my project, I have a UserControl named Rock, containing a canvas with a path that looks like a rock (and the path has a physicsObjectBehavior attached.) The following code is in MainWindow.xaml.cs triggered by a timer.


var rock = new Rock();
LayoutRoot.Children.Add( rock );
rock.SetValue( Canvas.LeftProperty, Convert.ToDouble( random.Next(0, 600) ) );
rock.SetValue( Canvas.TopProperty, 0d );



I've found this works perfectly when you replace the Path with a Rectangle, however never works when using a Path. Specifically, the error occurs in BoundaryHelper.GetPointsForPath. 

Error: "Could not determine the outline of UIElement sprite_2. Could not find a point within its boundaries."

Is this problem occuring due to something I'm missing, or is it due to the current version not handling path's inside user controls?



Sep 8, 2009 at 11:31 AM

What if you place the Path into a Canvas (in Blend right click the Path and select Group Into/Canvas), and then apply the PhysicsObjectBehavior (and other physics behaviors) to the Canvas instead of the Path?

There are sometimes issues with nested objects but I think they are worked around by grouping things into a Canvas and then using the Canvas to apply Physics Behaviors.