Get PhysicsSprite from a PhysicsObjectBehavior

Mar 4, 2011 at 2:28 AM


Is it possible to get the physicssprite directly from the physicsObjectBehavior? Or am I forced to access it through the physicsControllerBehavior?

Also, if I have a physicsObjectBehavior, how do I get the corresponding sprite in the physicsControllerBehavior.PhysicsMain.PhysicsObject[key]?

I am only one day into physicshelper and very confused, help is much appreciated.

Mar 4, 2011 at 2:57 PM

When your app runs, the UI element (or elements in the case of a User Control being added with multiple behaviors) is placed into the Game Canvas for the visual. And a Geometry and Body are added to the Physics simulation. Both can be referenced from the PhysicsControllerBehavior and are wrapped in a PhysicsSprite class, something like so:

PhysicsControllerMain _physicsController = LayoutRoot.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;
_physicsController.PhysicsObjects["ball"] ...

I did it this way because things get complicated when adding a bunch of User Controls with Physics Objects, Joints, etc. into a single simulation, and I wanted to preserve some compatibility with older versions (SL2) of the Physics Helper Library which pre-dated Behaviors.

Is there a reason why you need to get at the PhysicsSprite through the Behavior and not the Controller?

Mar 4, 2011 at 6:02 PM
Edited Mar 4, 2011 at 7:08 PM

Thanks for the response... 

I guess I didn't explain myself in enough detail in my original post, so I will try to elaborate. 

The reason I would prefer to get the PhysicsSprite from the Behavior is because I wrapped the Behavior into a UserControl, and I wanted to change the position of the PhysicsSprite from within my UserControl... the only way to do this (if I understand correctly) is with the PhysicsSprite (as the PhysicsObjectBehavior doesn't have anyway to set the position), but I can't access the PhysicsController to get at the Sprite from my UserControl as its in a different scope. The application hierarchy looks something like this:











I got around this problem by implementing a PositionChanged event from my usercontrol, and setting an event handler for it on my MainPage where I can access my PhysicsController. Is there a better way to do this, do you think? The reason I need this is because I want to animate the position of the usercontrol using a Storyboard, but I want the PhysicsObject to still be consistent with the usercontrols position.

The problem is that the position of the PhysicsObject does not update when I change the Canvas.Left and Canvas.Top property of my usercontrol. My first question is, is there any easy way to solve this problem?

My second question is, given only a physicsObjectBehavior, is there any way to get the physicsSprite from the Controller?