Simple sample request: Adding programmatic behaviors

Aug 29, 2010 at 2:32 AM


I have been searching around here about the "Could not determine the outline of UIElement Ball. Could not find a point within its boundaries" error when adding programmatic behaviors, but seems to be no reliable workaround.

So would you Andy please make a very simple demo project which does this programmatically?

1. Create a floor (square) programmatically.

2. Create a ball (ellipse) programmatically.

3. Let the ball fall and bounce.

I’m pretty sure it won’t take you more than 5 mins :)
will solve all problems people is having when adding programmatic behaviors. I have tried many things but still no luck.


Aug 29, 2010 at 12:35 PM

Sure there is a sample in the last entry of this forum post:

Some notes about the boundary determination error:

- it is usually because of the boundary detection algorithm trying to determine the outline of an object you are using. This algorithm works by tracing a path around the pixels in the object

- your visual tree _has_ to be drawn to the screen in order for the boundary detection to work! This is because it uses the VisualTreeHelper class.

- if you have ONE OR MORE instances of your physics object inside your game Canvas XAML at start time, the boundary will be determined and CACHED by the physics controller. So that subsequent programmatic ads will used the cached boundary for performance (the trace takes some time!)

- If you DO NOT have ONE OR MORE instances of the physics object inside your game Canvas XAML at start time, you will need to delay the AddPhysicsBody calls until after the Visual Tree is drawn. I often use a very short duration (500 ms) Storyboard to make sure everything is drawn to the screen so that the VisualTreeHelper can do a HitTest.

- make sure you don't have any stray pixels sitting around outside the image's shape, because this can throw off the boundary detection algorithm.

- you need to make sure everything is in a Canvas (no Grids) and make sure the Canvas is located at x,y = 0,0 in your visual tree (You can change this after the Initialized event raised by the PhysicsController. (Admittedly this is a bit touchy!) So no Game Canvas inside a Grid - but you can certainly use overlays on top of your game Canvas for score, etc.

Also, there are several posts on Adding Physics Behaviors Programmatically -

Aug 29, 2010 at 3:29 PM

Thanks. I tried many things, for example:

Used the PerfTest project on PhysicsHelper.WindowsPhone.sln and only replaced ucBlockHorizontalMedium for a simple Ellipse. Project ran, but boundaries error came, no other changes were made.

I also went to try this, but class PhysicsObjectBehavior seem not to exist. Maybe it is an older version?

Aug 29, 2010 at 5:46 PM

Sorry maybe you didn't see the sample I was referring to? If you go to this post

, at the very bottom there is a link to this sample:

That has a dynamically added ellipse.

Aug 29, 2010 at 5:56 PM

Yes, I guess you mean this: 

            ucNewCtl = new UserControl2();
            ucNewCtl.SetValue(Canvas.LeftProperty, Convert.ToDouble(_rand.Next(0, 500)));
            ucNewCtl.SetValue(Canvas.TopProperty, 0D);

But ucNewCtl is a control created on design time. I am looking for some thing like: 

          Ellipse ball = new Ellipse();
ball.Width = 50;
ball.Height = 50; LayoutRoot.Children.Add(ball); ball.SetValue(Canvas.LeftProperty, Convert.ToDouble(_rand.Next(0, 500))); ball.SetValue(Canvas.TopProperty, 0D); _physicsController.AddPhysicsBodyForCanvasWithBehaviors(???); What to use at ??? ?
Aug 30, 2010 at 10:38 PM
Edited Aug 30, 2010 at 10:38 PM

I'm not sure why you want to add a primitive ellipse, I think User Controls are much more powerful, but here is a sample:

Remember you need to delay adding the physics because of the VisualTreeHelper limitation (stuff needs to be drawn to the screen).

Aug 30, 2010 at 11:52 PM

Thanks! (oh I feel like a newbie right now... lol)

I wanted simple controls just to let the user draw simple objects at runtime.

Keep up the nice work!