Adding text to a physics object

Sep 11, 2011 at 1:03 AM
Edited Sep 11, 2011 at 1:04 AM


Trying to add text to a rectangle or ellipse so as to have a floating named object.

I can do this in XAML. But trying to build and add the same hierarchy of object explodes the engine.

This works fine:

<Grid Canvas.Left="53" Canvas.Top="427" >
    <Ellipse Fill="#FFFFEE33" Height="80" Stroke="Transparent" Width="80"></Ellipse>
    <TextBlock Text="BINGO"  HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>
        <pb:PhysicsObjectBehavior RestitutionCoefficient="0.1"/>
        <pb:PhysicsMagnetBehavior Magnetism="0.0001" FallOff="0.1" MaxDistance="500"/>
But attempting to build this in code and add it to the physics controller, POP! a loud explosion and debris everywhere.
PhysicsControllerMain physics = LayoutRoot.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;

Grid grd = new Grid();
grd.Name = "GRD" + DateTime.Now.Ticks.ToString();
grd.SetValue(Canvas.ZIndexProperty, 1);
grd.SetValue(Canvas.LeftProperty, (double)(300 - DateTime.Now.Second));
grd.SetValue(Canvas.TopProperty, (double)(DateTime.Now.Second + 200));

Ellipse el = new Ellipse();
el.Fill = new SolidColorBrush(Colors.LightGray);
el.Width = 60;
el.Height = 60;

TextBlock tb = new TextBlock();
tb.Text = "MSFT";
tb.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
tb.VerticalAlignment = System.Windows.VerticalAlignment.Center;


PhysicsObjectBehavior physicsBehavior = new PhysicsObjectBehavior();
physicsBehavior.RestitutionCoefficient = .5;

PhysicsMagnetBehavior magneticBehavior = new PhysicsMagnetBehavior();
magneticBehavior.Magnetism = 0.0001;
magneticBehavior.FallOff = 0.1;
magneticBehavior.MaxDistance = 500;


physics.AddPhysicsBody(grd.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain);
Sep 11, 2011 at 1:44 AM

OK, by substituting in a TextBox in place of the TextBlock I can get the grid/ellipse/text to show up on the canvas.

However, the boundaries which applied to the ellipse when I added it solo are not containing the grid. This new floating object sails right through all boundaries with no collision.

Any idea how to get the boundary to apply to the grid now?

Substitute code for the TextBlock:

TextBox tb = new TextBox();
tb.Name = "TB" + DateTime.Now.Ticks.ToString();
tb.IsReadOnly = true;
tb.Background = new SolidColorBrush(Colors.Transparent);
tb.BorderThickness = new Thickness(0);
tb.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
tb.VerticalAlignment = System.Windows.VerticalAlignment.Center;

Sep 11, 2011 at 2:55 AM

I also tried just joining the ellipse and the textbox (which works in XAML, in code:

PhysicsJointBehavior jointBehavior = new PhysicsJointBehavior();
jointBehavior.BodyOne = el.Name;
jointBehavior.BodyTwo = tb.Name;
jointBehavior.IsWeldJoint = true;

No go.
This worked though:
<Ellipse x:Name="ellipse" Fill="#FFF4F4F5" Height="102" Canvas.Left="524" Stroke="Black" Canvas.Top="321" Width="116">
<TextBox x:Name="textBox" Canvas.Left="555" TextWrapping="Wrap" Text="TextBox" Canvas.Top="432" IsReadOnly="True">
<Path Data="M518,450 L602,450" Fill="#FFF4F4F5" Height="1" Canvas.Left="541.333" Stretch="Fill" Stroke="Black" Canvas.Top="427" UseLayoutRounding="False" Width="82.333">
        <pb:PhysicsJointBehavior BodyOne="ellipse" BodyTwo="textBox" AngleSpringConstant="0" AngleSpringDampningConstant="0" IsWeldJoint="True"/>