BodyObject.Position returns strange value

Nov 25, 2010 at 10:14 AM

I'm using the latest version of the excellent PhysicsHelper for development for WP7.  I seem to be getting an unusual result from the BodyObject.Position property.  I'm actually trying to use the cameraBehaviour to keep a specific element center in the screen (and then want to do some parallax scrolling), but the camer seems to put (roughly) the top left corner of the canvas to the center of the screen.

having gone through and debugged the code I have found what I believe to be the issue.  The cameraControllerMain gets the target position using something along the lines of "physicsControllerMain.PhysicsObjects["targetItem"].BodyObject.Position" when I check the value being returned I find that it returns a value that doesn't seem to represent the location of my targetItem (e.g. the item is positioned in blend as top:200; left:200, yet the values returned are X:7.43; Y: -0.536), continuing to debug I find that if I check the private properties of the PhysicsSprite ( _X and _Y ) these return values representative of what is in blend, and if I change the cameraControllerMain to use these values I get a result much closer to what I expect.

My reason for this post is that i have not seem any others posting a similar issue so I'm wondering if I have set something up incorrectly??  I tried copying the XAML below from another post (someone said it worked) but get the same problem as with my own code.

<Canvas x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}">
       <Rectangle Fill="#FFF4F4F5" Height="60" Canvas.Left="8" Stroke="Black" Canvas.Top="732" Width="464">
                     <pb:PhysicsObjectBehavior IsStatic="True"/>

        <Ellipse x:Name="ball" Height="52" Canvas.Left="305" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" StrokeThickness="4" StrokeMiterLimit="10" StrokeLineJoin="Miter" Width="48" RenderTransformOrigin="0.674,0.34" Stroke="{StaticResource PhoneBackgroundBrush}" Fill="#FFFD1B04" Canvas.Top="160">
                <pb:PhysicsObjectBehavior IsStatic="False" Mass="2" BoundaryElement="ball" />


I have not tried the code in a Silverlight web build, only a build for WP7.

Any feedback would be much appreciated,


Nov 25, 2010 at 1:31 PM

Hi Martyn,

Farseer 3 (used by Physic Helper 4) has a different corrdinate system than Farseer, thus the low values. It looks like I still need to update the CameraController Behavior to adjust for this.

But keep in mind that to get screen coordinates of an element, you can use the PhysicsSprite.Position property instead of the BodyObject.Position property.

There are also BoundaryHelperBox2D.WorldToScreen and ScreenToWorld methods which are helpful for this.

Nov 26, 2010 at 11:16 AM

Thanks for clearing that up for me Andy.