Adding PhysicsObjectBehavior to a user control works, but PhysicsExplodeBehavior doesn't

Apr 21, 2010 at 12:42 AM

You can see this with the following example.


Take the truck demo and on the main page add a PhysicsExplodeBehavior to the truck control. Set it bodyone to the truck and bodytwo to the ground. Start the program and when the truck hits the ground "Boom!".

Now remove the explode. Open the truck user control. Add the PhysicsExplodeBehavior there. Edit the bodyone and bodytwo by hand. Tick "MatchLikeBodyNames" (or not, doesn't help.) Start the program.

Expected: Boom!

Result: Nothing.

I would further like to be able to respawn the truck when it explodes (from code is fine) but I am very unsure how to set the PhysicsExplodeBehavior on it from code.

Anyone know how to do this?

Apr 22, 2010 at 7:14 PM
Edited Apr 24, 2010 at 3:58 PM

After looking around for a while in the posts I found that PysicsExplodeBehavior still has some rough edges. So instead I hooked into the collision detection myself. Then I refactored the PhysicsExplodeBehavior to expose a public static ExplodeSprite method which I can call from my custom collision detection.








 protected override void Invoke(object args)
            if (_isControllerInitialized)
                string body = this.AssociatedObject.Name;
                if (body == string.Empty)
                    throw new Exception("There is no Physics body for the given element. Be sure you added a PhysicsObjectBehavior to the element.");

                ExplodeSprite(body, Controller, NumParticles, DeactivateBody);

        public static void ExplodeSprite(string body, PhysicsControllerMain physicsController, int numParticles, bool deactivateBody)

            if (_particles.Count < numParticles - 1)
                for (int i = 0; i < numParticles; i++)
                    ucExplode particle = new ucExplode();

            PhysicsSprite sprite;
            if (!physicsController.PhysicsObjects.TryGetValue(body, out sprite))
                return; // make sure sprite is still active

            Body bodyObj = physicsController.PhysicsObjects[body].BodyObject;

            double left = bodyObj.Position.X;
            double top = bodyObj.Position.Y;

            double angleStep = 360 / numParticles;
            double currAngle = 0;

            for (int i = 0; i < numParticles; i++)
                if (PhysicsControllerMain.ParentCanvas.Children.IndexOf(_particles[i]) < 0)

                _particles[i].Visibility = Visibility.Visible;
                _particles[i].SetValue(Canvas.LeftProperty, left + (_particles[i].ActualWidth / 2));
                _particles[i].SetValue(Canvas.TopProperty, top + (_particles[i].ActualHeight / 2));
                _particles[i].rotateExplode.Angle = currAngle;
                currAngle += angleStep;

                // TODO for WPF

                if (deactivateBody)



[Edit: removed stray code]


Apr 22, 2010 at 8:50 PM

cool :)

Apr 24, 2010 at 1:12 AM
AndyBeaulieu wrote:

cool :)

 Glad you like it. Here is the "alpha" for my physics game. If I work on it anymore I would add a scoreboard and some levels.


Apr 24, 2010 at 1:40 PM

One thing with your game, if you put DebugMode on for the PhysicsControllerBehavior, take a look at your asteroids boundaries. I think they might be following the boundary of the entire control instead of the image. You can set the Boundary Element on the PhysicsObjectBehavior to fix this, or place the PhysicsObjectBehavior on the Image element instead of the Canvas.

Apr 24, 2010 at 3:55 PM

Interesting. I will check the out. It might also be the drop shadow I added to "Absorb" the stray pixels. I may have overdone it and filled the whole image control with low alpha pixels.