Restart game level

Feb 1, 2011 at 6:52 AM

Hi all,

I have physics behaviors applied to objects in XAML for my wp7 game.  I would like to give the user the ability to pause and restart the level.  I figured out how to pause the level, which is a lot easier than I first thought it would be…_physicsController.PauseSimulation = true; …Does anyone know the best way to restart a level to its initial state?  Thanks!

Thanks Andy for an awesome library!


Feb 2, 2011 at 8:00 AM
Edited Feb 2, 2011 at 8:06 AM

Hi. I'm using this:

NavigationService.Navigate(new Uri(String.Format("%blablabla%.xaml?retry" + Guid.NewGuid()), UriKind.Relative));

Where %blablabla% is the path of page you want to restart.

But it works bad. Because remains of an old PhysicsControllerMain prevents normal work of new PhysicsControllerMain.

After that any physics won't work.

I don't know how to fix it. But in wp7 apps without PhysicsHelper, this kind of restart works fine.


Feb 2, 2011 at 3:54 PM

Thanks for the reply trentmon. I have a couple ideas I want to try tonight.  I will update the thread with my results.


Feb 4, 2011 at 12:50 PM

For my levels, I save the position of objects at the level start, and then restore their positions to reset velocity and angular force.

Here is what I do:


       public static List<Position> Positions = new List<Position>();



       public static void SavePositions(PhysicsControllerMain physicsController)


           foreach (KeyValuePair<string, PhysicsSprite> sprite in physicsController.PhysicsObjects)


               Position savePos = new Position();

               savePos.X = sprite.Value.BodyObject.Position.X;

               savePos.Y = sprite.Value.BodyObject.Position.Y;

               savePos.Rotation = sprite.Value.BodyObject.Rotation;

               savePos.BodyName = sprite.Key;

               savePos.Mass = sprite.Value.BodyObject.Mass;





       public static void RestorePositions(PhysicsControllerMain physicsController)


           foreach (KeyValuePair<string, PhysicsSprite> sprite in physicsController.PhysicsObjects)


               foreach (Position savePos in Positions)


                   sprite.Value.BodyObject.LinearVelocity = xna.Vector2.Zero;

                  sprite.Value.BodyObject.AngularVelocity = 0;


                   if (savePos.BodyName == sprite.Key)


                       sprite.Value.BodyObject.Position = new xna.Vector2(savePos.X, savePos.Y);

                       sprite.Value.BodyObject.Rotation = savePos.Rotation;








   public class Position


       public string BodyName { get; set; }

       public float X { get; set; }

       public float Y { get; set; }

       public float Rotation { get; set; }

       public float Mass { get; set; }

       public Point PointOffset { get; set; }



Feb 5, 2011 at 3:15 AM

Thanks, Andy. It works. But it doesn's restore removed objects. I'll try to modify your example.

Feb 6, 2011 at 1:42 AM

Very cool Andy, Thanks!

Feb 7, 2011 at 8:36 AM
Edited Feb 7, 2011 at 8:40 AM

I've redesigned RestorePositions method. But it still does not restore removed or exploded objects. Somebody help me please.

public void RestorePositions(PhysicsControllerMain physicsController)
        foreach (Position savePos in Positions)
                var sprite = physicsController.PhysicsObjects[savePos.BodyName];
                sprite.BodyObject.LinearVelocity = Vector2.Zero;
                sprite.BodyObject.AngularVelocity = 0;
                sprite.BodyObject.Position = new Vector2(savePos.X, savePos.Y);
                sprite.BodyObject.Rotation = savePos.Rotation;
                //handle removed objects here
                //it doesn't work=(
Feb 8, 2011 at 4:16 AM

trentmon, have you tried setting the visibility of the object to collapsed rather than removing it?  

Feb 9, 2011 at 2:12 AM

I have found another way. I'm not sure it's the best solution, but here it is.

I was using Explode Behavior with property "DeactivateBody" set to true. That's why my objects had removed.

Now I set the property to false and handle collisions in code. And when collision happens I just move an object to coordinates like X = -5000; Y = -5000; instead of removing it.