The way to Add Physics Behaviors Programmatically

Dec 10, 2009 at 3:13 AM
Edited Dec 10, 2009 at 3:14 AM

I still don't know how to add physics behaviors programmatically,I'm so week in Silverlight and WPF...

The error is"Could not determine the outline of UIElement sprite_1. Could not find a point within its boundaries."

Error is in the sentence "_physicsController.AddPhysicsBody(truck.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain);"

("truck" is a user control which defined above and doesn't have the physics behaviors at first)

My code is like this:


            PhysicsControllerMain _physicsController = LayoutRoot.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;
            theTruck truck = new theTruck();
            LayoutRoot.Children.Add(truck);
            truck.SetValue(Canvas.LeftProperty, Convert.ToDouble(_rand.Next(50, 950)));
            truck.SetValue(Canvas.TopProperty, 0D);
            s.Begin();
            BehaviorCollection behaviorCollection = Interaction.GetBehaviors(truck);
            behaviorCollection.Add(new PhysicsObjectBehavior());
            _physicsController.AddPhysicsBody(truck.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain);

I know less about storyBoard……

I didn't use the method AddPhysicsBodyForCanvasWithBehaviors,I tried to learn the code which was in your article:

PhysicsControllerMain _physicsController = LayoutRoot.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;

 

// add physics behavior to an ellipse named "ball"

behaviorCollection = Interaction.GetBehaviors(ball);

behaviorCollection.Add(new PhysicsObjectBehavior());

_physicsController.AddPhysicsBody(ball.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain);

 

// add static body behavior to a rectangle named ground

PhysicsObjectBehavior behPhysObj = new PhysicsObjectBehavior();

behPhysObj.IsStatic = true;

behaviorCollection = Interaction.GetBehaviors(ground);

behaviorCollection.Add(behPhysObj);

_physicsController.AddPhysicsBody(ground.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain);

But I always get the error "Could not determine the outline of UIElement sprite_1. Could not find a point within its boundaries."  Can you help me or give me a demo?

Thank you, thanks a lot!


Jun 26, 2010 at 11:17 PM

Indeed, Farseer Helper is a great thing. However, it has not provided enough helper documents related to its API. This is, in my opinion, one of the may cause that Farseer Helper( including Farseer itself) has not prevailed to such an extend as far as the author initially imaged.

Coordinator
Jun 27, 2010 at 12:10 PM
Edited Jun 28, 2010 at 5:25 PM

Sorry you're having issues with Farseer and the Physics Helper. Please keep in mind that these are volunteer based projects by the community. If you are interested in volunteering time to document or otherwise contribute, please contact myself for Physics Helper, or for Farseer Physics, one of the Farseer Physics team members.

That said, I think there are some good sources of documentation for starters though:

I'm not sure if you are refering to the "Could not determine the outline" error, which is quite common, or if you are just venting? But this error is usually because of the boundary detection algorithm trying to determine the outline of an image you are using. This algorithm works by tracing a path around the pixels in the image, but doesn't work if you have any stray pixels sitting around outside the image's shape. You also need to make sure everything is in a Canvas located at x,y = 0,0 in your visual tree (You can change this after the Initialized event raised by the PhysicsController. (Admittedly this is a bit touchy!)

Also, there are several posts on Adding Physics Behaviors Programmatically -

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

http://physicshelper.codeplex.com/Thread/View.aspx?ThreadId=77450

http://physicshelper.codeplex.com/Thread/View.aspx?ThreadId=79815