Canvas.Left not correct?

Sep 25, 2010 at 8:53 PM

I'm trying to bind a Line to a ball(Ellipse) with a PhysicsObjectBehavior like this:

 <Line X1="{Binding (Canvas.Left), ElementName=ball}" Y1="{Binding (Canvas.Top), ElementName=ball}" />

The Line dissapear when the physics starts.

 

In the debugger the following line allways returns -32. (The ball has width=64)

Canvas.GetLeft(ball);

Why cant I use Canvas.GetLeft() to get the position, is there another way? (that works with binding)

Coordinator
Sep 25, 2010 at 9:15 PM

Sorry there is no way to use binding to get the position. I'm not sure you would want to use binding for the best performance.

If you do really need it though, you might want to take a look at Pete Blois's behaviors for Box2d physics. I'm pretty sure he has all of the binding working for position.

Here is the link - http://www.blois.us/blog/

To do this with Farseer and the Physics Helper, you have a couple of options:

- use and extend the behaviors for ApplyForce, ApplyRotation, etc.

- use the PhysicsObjects collection to get the Farseer Body position
       int x = _physicsController.PhysicsObjects["ball"].BodyObject.Position.X;

- use the PhysicsSprite.uiElement property to get at the actual XAML element in the game Canvas 

Hope this helps

-Andy

 

Sep 29, 2010 at 9:07 PM

Thanks for the quick answer.

I went for the BodyObject.Position solution:

        void physicsControllerMain_TimerLoop(object source)
        {
            var b = physicsControllerMain.PhysicsObjects["ball"];
            var s = physicsControllerMain.PhysicsObjects["ship"];
           
            MyLine.X1 = b.BodyObject.Position.X;
            MyLine.Y1 = b.BodyObject.Position.Y;

            MyLine.X2 = s.BodyObject.Position.X;
            MyLine.Y2 = s.BodyObject.Position.Y;

        }