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:
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)
Actual moving code: (moves all bodies into the opposite direction of the circle's flying direction)
Updating the graphical represantation:
I tried to find the bug in this code for the whole day now, to no avail.
(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);
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());
}
}
{
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);
}
}
{
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.