Gravity on a WP7 user control?

Mar 30, 2010 at 6:47 AM

Thanks for all of your hard work. This library is great!

I can successfully add user controls at run-time in a WP7 (3.0.0.5) app. However, the controls I add at run time (via AddPhysicsBodyForCanvasWithBehaviors()) don't seem to respond to gravity. No exceptions; the control just hangs in mid-air. If I add the user control at design time, the control responds to gravity. Some code below, but I'm curious if anyone has successfully built a WP7 project that uses AddPhysicsForCanvasWithBehaviors() with user controls and gravity? I've been combing through the examples, but I can't spot the issue. It's likely a problem in my code, but I'm curious if WP7 apps require a different approach?

My user control:

<UserControl x:Class="PhoneRecognizer.circleControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
         xmlns:Spritehand_PhysicsBehaviors="clr-namespace:Spritehand.PhysicsBehaviors;assembly=Spritehand.PhysicsBehaviors"
    mc:Ignorable="d" Height="50" Width="50">
    <Canvas x:Name="LayoutRoot">
        <Ellipse x:Name="circleGraph" Height="50" Width="50" Stroke="Black">
            <Ellipse.Fill>
                <RadialGradientBrush RadiusX="0.192" RadiusY="0.167" Center="0.346,0.281" GradientOrigin="0.346,0.281">
                    <GradientStop Color="#FF84D49D" Offset="0"/>
                    <GradientStop Color="#FF397E4F" Offset="1"/>
                </RadialGradientBrush>
            </Ellipse.Fill>
            <i:Interaction.Behaviors>
                <Spritehand_PhysicsBehaviors:PhysicsObjectBehavior />
            </i:Interaction.Behaviors>
        </Ellipse>
    </Canvas>
</UserControl>

The Add code:

_circle = new circleControl();

_circle.SetValue(Canvas.LeftProperty, 100D);
_circle.SetValue(Canvas.TopProperty, 100D);

LayoutRoot.Children.Add(_circle);
_physicsController.AddPhysicsBodyForCanvasWithBehaviors(_circle.LayoutRoot);

 

 

Mar 30, 2010 at 7:46 AM

Maybe I was over-thinking it? This works:

_circle = new circleControl();

_circle.SetValue(Canvas.LeftProperty, 100D);
_circle.SetValue(Canvas.TopProperty, 100D);

LayoutRoot.Children.Add(_circle);
_physicsController.AddPhysicsBody(_circle);

 

 

Coordinator
Mar 30, 2010 at 12:19 PM

This blog post might help?

http://www.andybeaulieu.com/Home/tabid/67/EntryID/168/Default.aspx

Technically these behaviors should inject themselves automatically when added through code, but I don't want to change the behavior at this point because there is code that depends on it _not_ injecting automatically. I'll probably address this with some kind of a switch in a future release.

Mar 30, 2010 at 2:46 PM

Thanks. I'd read that post.

I guess my question is: when do you use AddPhysicsBodyForCanvasWithBehaviors() and when do you use AddPhysicsBody() (and is there a difference when WP7 is involved)? The samples seem to use AddPhysicsBodyForCanvasWithBehaviors(). Any guidance?



 

Coordinator
Mar 30, 2010 at 2:52 PM

AddPhysicsBody is meant to be called for stand-alone UI Elements that have Physics Behaviors attached.

AddPhysicsBodyForCanvasWithBehaviors (man that's along name) is meant to called for a UserControl that has nested Physics Behaviors contained within it.

There should be no diff when WP7 is involved, but I've only been working in the emulator for a little more than a week :) So there may be some issues I don't know about yet.