Menu

#6 Mismatched Transform after Sleep

open
nobody
None
5
2012-09-08
2012-09-08
David Mace
No

Flash CS5.5 / Flex SDK 3.6

If you use body.GetPosition() and body.GetAngle() to sync the current physical position onto a Sprite/MovieClip, then as they fall asleep, the 'final' positions occasionally aren't up to date. Soon, several things mismatch their location on the debug render, and 'odd' behaviors begins to manifest with invisibly misplaced collisions (sometimes as much as 500cm or 45 degrees of rotation).

Initially I just disabled sleep with body.SetSleepingAllowed(false); but since the target is ultimately a mobile platform, that's not ideal.

Better work around: Added a call to 'SynchronizeTransform()' to b2Body.SetAwake(false). This will make sure the 'most recent' position+rotation reported when it fell asleep is correct, or at least more up-to-date than it was, without it.

Possibly making things 'even bigger', according to the simulation, would also solve this problem, which would put the cause back on me, 'not using it right'. At 20 pixels per meter, this is about the 'right' scale for the things, on the tiny display that I'm working with.

/**
* Set the sleep state of the body. A sleeping body has vety low CPU cost.
* @param flag - set to true to put body to sleep, false to wake it
*/
public function SetAwake(flag:Boolean):void {
if (flag)
{
m_flags |= e_awakeFlag;
m_sleepTime = 0.0;
}
else
{
m_flags &= ~e_awakeFlag;
m_sleepTime = 0.0;
m_linearVelocity.SetZero();
m_angularVelocity = 0.0;
m_force.SetZero();
m_torque = 0.0;
SynchronizeTransform(); /** <== Added here. **/
}
}

Discussion


Log in to post a comment.

MongoDB Logo MongoDB