Complex controls involving ellipses

Jul 29, 2010 at 1:13 PM

This post is a consequence of two previous posts:

http://physicshelper.codeplex.com/Thread/View.aspx?ThreadId=221828
http://physicshelper.codeplex.com/Thread/View.aspx?ThreadId=221686

The application I made only adds ellipses and rectangles (at the same time) to the main stage when a button is clicked.
Here is the code for the ball.xaml:

	<Canvas x:Name="LayoutRoot">
		<Ellipse x:Name="lb" Fill="#FF33D01E" Stroke="Black" Height="30" Width="30">
			<i:Interaction.Behaviors>
				<pb:PhysicsObjectBehavior/>
			</i:Interaction.Behaviors>
		</Ellipse>
		<TextBlock Height="30"  TextWrapping="Wrap" Text="0" Width="30" TextAlignment="Center" FontSize="18.667" FontWeight="Bold" Foreground="#FFFBEEEE" Canvas.Top="2"/>
	</Canvas>
And here you are the square.xaml:
	<Canvas x:Name="LayoutRoot" Width="25" Height="25">
		<Rectangle Fill="#EAF71919" Height="25" Stroke="Black" Width="25">
			<i:Interaction.Behaviors>
				<pb:PhysicsObjectBehavior/>
			</i:Interaction.Behaviors>
		</Rectangle>
		<TextBlock Height="25"  TextWrapping="Wrap" Text="0" Width="25" TextAlignment="Center" FontSize="18.667" FontWeight="Bold" Foreground="#FFFBEEEE" />
	</Canvas>
 
 As you can see the idea here is to add a ball and a square with a number 0 inside of them. The problem here is that with this xaml structure the only thing that will be under the physics influence will be the rectangle and the ellipse but not the Texblocks.
My first idea was to had the PhysicsObjectBehaviour to the main Canvas "LayoutRoot" but it does not work! 
Indeed, it throws an exception if any previous ball or square was added into the main stage when the application loads! I put a square and then a ball previously via xaml and the famous error message appeared:
Could not determine the outline of UIElement sprite_2. Could not find a point within its boundaries

I forgot to post the button event handler code:

 Private Sub Button_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btAdd.click
        'reference to main controller
        Dim p = CType(screen.GetValue(PhysicsControllerMain.PhysicsControllerProperty), PhysicsControllerMain)
        'adding rectangle
        Dim c As New square
        'screen.Children.Add(b)
        screen.Children.Insert(0, c)
        c.SetValue(Canvas.LeftProperty, Convert.ToDouble(New Random().Next(0, 250)))
        c.SetValue(Canvas.TopProperty, CDbl(0))
        Dim o1 = p.AddPhysicsBodyForCanvasWithBehaviors(c.LayoutRoot)
        'adding ball
        Dim b As New ball
        'screen.Children.Add(b)
        screen.Children.Insert(0, b)
        b.SetValue(Canvas.LeftProperty, Convert.ToDouble(New Random().Next(0, 500)))
        b.SetValue(Canvas.TopProperty, CDbl(0))
        Dim o = p.AddPhysicsBodyForCanvasWithBehaviors(b.LayoutRoot)
    End Sub
Next thing I tried is to add the PhysicsObjectBehavior to the UserControl itself but this has 2 different sideffects:
  1. The programmatically added objects won't have physics.
  2. The xaml added objects will have physics but the ellipse (again these holy ellipses) won't behave correctly. Instead of a ellipse they move like a square. Maybe they are getting the boundaries of the usercontrol and it is a square?

then I tried to use the BoundaryElement property (targeting the ellipse and the rectangle respectively) of the PhysicsObjectBehavior but again a weird behaviour occurs: Now the elements appear in a border of the screen and fall down beyond the ground object dissapearing from the screen!!! but again,
when I try to add them programmatically they appear but without physics.

The only option for me is to create a new panel inside the usercontrol containing both the ellipse/rectangle and the text and then assign the physicsObjectBehavior to the new panel... but... in the case of the ellipses we have againg the 2 problem I noted before: they behave like squares!!
All seem to work now but they behave like square when they roll down the ground!!

And then again I try the BoundaryElement property with the following results (I applied it to the rectangle and the ellipse in square.xaml and ball.xaml respectively):

  1. The square works great with its associated textblock moving along with it.
  2. The ellipse throws the boundary error!

The questions here are:

  1. How can I make these ellipses behave correctly?
  2. Why the boundaryElement property is not working correctly with ellipses? Am I missing something?

Thanks again!