SetXForm accuracy

Hello,

I am making an application where I need to record the physics simulations; the simulation consists of applying an impulse to a single body, and let the engine do its job. To do this, I save the position of the body from GetPosition() and the angle from GetAngle(), and when the movie is replayed, I pass this information to SetXForm(). My thinking was that if the original positions are the same of what they were originally, and if I apply the same impulse to the body as before, the simulation should be exactly, or very close to, the original simulation.

When I retrieve the position and angle of the body after one step, I see that current GetPosition() doesn't differ much from the previous value (in term of pixels, the different is less than half a pixel), but the angle is off by little more than 1 degree. This causes the replay to differ siginificantly from the original simulation.

The way that I've thought of for compensating this difference is to continuously adjust the angle of the body until I am close to some reasonable degree to the desired angle. Would this be the way to about this problem? Or is there an easier/different way?

Thanks.
To attempt to solve this problem, I made a binary search algorithm. If current angle of the body is less than the angle I want (the desiredAngle), I pick an angle in the middle of 0 and current angle of the body, and set the body to that angle. I do something similar if the current angle of the body is greater than the desiredAngle. Since I know I can't set angle of the body to exactly what I want, I try get within some accuracy, which is specified in m_environ.accuracy. For example, if m_envion.accuracy is 1000, the angle I will compare against will be three decimal digits (0.000 to 0.999). Theoretically, I should eventually get the angle I want.

I call the following function in my ENTER_FRAME handler after the call to Step(). I'm not sure whether this will work, but when I read value of currBoxAngle (done in the first line), the angle always is what I had set initially. However, when read the angle of the body after calling SetXForm(), the angle is correct. This results in my "base case" to never hit. It seems that Step() overrides whatever I angle I set using SetXForm(). It doesn't matter if move my function before or after the call to Step(). I'm not doing anything else in my ENTER_FRAME handler other than the call to the Step() routine. Any ideas why this is happening?

Code:
      // adjusts the angle of the m_box.body to the desired angle by doing a binary search
      public function adjustBoxAngle():void
      {
         var currBoxAngle:int = int(m_box.body.GetAngle() * int(m_environ.accuracy));
         var desiredAngle:int = int(Number(m_environ.BoxAngle) * int(m_environ.accuracy));
         var newAngle:Number;

         if (m_environ.replay == 0)
            return;
         
         trace("desired angle: " + desiredAngle);
         trace("current Box angle: " + currBoxAngle);

         if (desiredAngle == currBoxAngle)
            return;

         if (currBoxAngle > desiredAngle)
         {
            newAngle = Number((currBoxAngle + m_loAngle) / 2)/int(m_environ.accuracy);
            m_hiAngle = currBoxAngle;
            trace("new hi angle: " + m_hiAngle);
         }
         else// if (currBoxAngle < desiredAngle)
         {
            newAngle = Number((currBoxAngle + m_hiAngle) / 2)/int(m_environ.accuracy);
            m_loAngle = currBoxAngle;
            trace("new lo angle: " + m_loAngle);
         }

         trace("setting Box angle to " + newAngle);
         m_Box.body.SetXForm(new b2Vec2(m_environ.BoxX, m_environ.BoxY), newAngle);
         trace("Box angle was set to " + m_box.body.GetAngle());
         
         trace();
      }


[edit]
In the init routine where I was creating the body, I was calling SetXForm(). I removed that call and relied only on this function to set the body's XForm, and now my function works properly, i.e, the call to GetAngle() returns a number close to what I want it be. The init routine is only called once. I'm not sure why calling SetXForm() initially makes a difference.
The function sets the body's angle quickly and is very accurate. For example, if my desired angle is 0.9337219678830505 and degree of accuracy is 1,000,000,000, I able to set the body's angle to 0.933721967, and the actual angle from GetAngle() turns out to be 0.9337219655.
[/edit]
Replay has been discussed before on these forums. The conclusion is that you should just record the body positions. You can use compression if you like.
I thought about recording the body's position and angle everyframe and replay without the physics engine, but I thought, logically, if everything is in its initial state as before and force is applied, the simulation should be the same as before. I am having some luck with this "continuous adjustment," though.
It is possible to get reproducible results. I rely on this in the testbed. You have to create everything in the same order and just let the simulation run. Any intervention will break it.

It seems like you are trying to do a partial replay. You have to start the simulation at the beginning.
Particularly if you just need to replay something like a movie, you're really going to be best off just storing away the body positions and replaying those rather than relying on the physics engine during playback. There are just so many things to get right if you want to recreate the physics, whereas storing away an array of positions is so simple.

If you're worried about space, you'll probably want to at least keyframe every 30 or 60 frames to make sure that if little errors show up they are corrected pretty quickly.
ewjordan wrote:
Particularly if you just need to replay something like a movie, you're really going to be best off just storing away the body positions and replaying those rather than relying on the physics engine during playback. There are just so many things to get right if you want to recreate the physics, whereas storing away an array of positions is so simple.


I may have to end up doing that. However, my simulation also requires a body with a bunch of shapes to explode. I'd have to keep track of that as well. Of course, this all can be done, but I'd have to write a separate engine. I'd thought I might as well try too see if I can get the physics simulation to work right.
Yeah, it's going to be a decent amount of work either way; I understand that esp. if you have bodies being created and destroyed keeping track of that is a lot of bookkeeping. But I've had lots of headaches in the past trying to get deterministic replays going, even when I was writing an engine which was aimed at that from the start, so my general opinion now is that it's usually far more difficult than it seems at first.

Original thread: https://box2d.org/forum/viewtopic.php?t=3262