[FLASH] Trying to keep a body in the middle of the screen

Short summary: I wan't to reset the position of the body every frame back to some value with SetXForm. It doesn't work...
(sorry, I'm seeing just now that there is an own forum for flash threads)

Sounds so simple, and I have the code right here. It looks flawless, but the physics body is off by some amount that is growing with the falling speed.
Im pushing the body desired to be positioned in the middle of the screen here into an array:
Code:
m_bodies.push(circlebody);

Everything that follows now, is called every frame:
Each frame the "camera" is moved by the amount the circle moved: (9.1 and 6.5 is the middle of the screen and also the body's starting position)
Code:
m_world.Step(1/30,10);
camera.moveCamera(circlebody.GetPosition().x - 9.1, circlebody.GetPosition().y - 6.5);   

Actual moving code: (moves all bodies into the opposite direction of the circle's flying direction)
Code:
public function moveCamera(deltaXinMeters:Number, deltaYinMeters:Number)
      {
         for (var i:int = 0; i < m_bodies.length; ++i )
         {
            (m_bodies[i] as b2Body).SetXForm(new b2Vec2(
            (m_bodies[i] as b2Body).GetPosition().x - deltaXinMeters,
            (m_bodies[i] as b2Body).GetPosition().y - deltaYinMeters
            ), (m_bodies[i] as b2Body).GetAngle());   
         }
    }


Updating the graphical represantation:
Code:
for (var j:int = 0; j < m_bodies.length; ++j)
         {
            if (m_bodies[j].m_userData is MovieClip)
            {   
               m_bodies[j].m_userData.x = m_bodies[j].GetPosition().x * 30;
               m_bodies[j].m_userData.y = m_bodies[j].GetPosition().y * 30;
               m_bodies[j].m_userData.rotation = m_bodies[j].GetAngle() * (180 / Math.PI);
            }
         }


I tried to find the bug in this code for the whole day now, to no avail.
From my experience with Box2D so far (with C++), I have never had to use XForms to move a body, so I'm not sure why you preferred to use XForm instead of SetPosition/SetAngle (I'm not 100% sure what the XForm stuff does anyway).

Also, what's the * 30 for? I remember getting a similar-looking output when my camera was being updated at the wrong time (it was lagging a frame behind the action, so the faster I moved the farther the rendering was from the debug body).

-P
Hi! Ill answer your questions first:
b2Body doesn't have a method called "SetPosition". To what are you referring to? I have my game actors stored in the user data portion of the physics body, maybe this is where we divert.
I'm multiplying with 30, because in the flash port of box2d, apparently one meter is always 30 pixels. In c++ you instead scale the projection to fit into the small bounds of box2d meters. (at least that's how I understood it and did it)

Moving the camera code seems to have solved the problem! Thank you so much, I can't believe in all these hours, I never tried that. The bounding box is still a bit weird though, it expands upwards now, but that won't be a problem ingame.

Do you happen to know the reason for this? (referring to the need of moving the camera code to the end of the physics time step) I have read through the forums, and sometimes word falls that the world.Step() physics timestep needs to be "finished" before moving something with SetXForm. I have no idea what this means though, since there is no way to detect when and how the physics calculations is being finished.
It looks like it runs on its own thread parallel to my own code, from what I was able to deduce.
(how? -> I checked the position of my body at the start of my update() function, and rechecked it at the end. The position changed, although I never did so manually, and the call to world.Step() was before all of this)
Oh, I had never actually had to set the position/angle manually so that's news to me, thanks.

Well, that's one way, but that's certainly not how I did it. I modeled my scaling system exactly as Box2D described, except I never actually touch any units (ie, I spawn a box with 1.5 width and 2.0 height, and that's how it's rendered). All I did was move the camera closer to the scene. This is actually a much better method than scaling everything up, as you can run into precision errors really quickly.

The reason it happens is the camera is using the data from the body of the previous frame; hence, the faster you move, the farther behind your rendering is because the camera is still back one frame. Took me a while to figure it out, but now I only set the camera X/Y before the physics does it's timestepping.

EDIT: The physics are "done" once Step returns. AFAIK, Box2D is not multithreaded, unless ActionScript automatically does this sort of thing. Perhaps you can tell it to wait until Step returns?

-P
I only realized it now, but the weird bounding box I was talking about (which was supposed to be no problem) actually is a problem. It shows that my original problem has only been moved. Now my physics body isnt lagging behind, but my camera is. Therefore my circle/player body is farther from the middle of the screen, the faster he moves 🙁
Aww crap.
AABBs cover the entire space that the shape moves through in one step. So it gets longer the faster the shape is moving. They are meant to be loose. Perhaps you should use GetPosition to center your camera.

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