Hi,
I am having a problem where my graphic will fall out of sync with the physics object.
I will try to post the relevant code so hopefully someone more experienced with Box2d can let me know what is going on 🙂
The physics loop runs in its own update at 60hz.
The Frame update handles mapping the player graphic to the physics location
I am using flixel for rendering.
I run the application and they are matched up,
--> move the spaceship around a bit and its fine,
--> start moving some more and they get unsynchronized.
--> move back the opposite direction and they get resynchronized.
Really have no idea why this is happening.
I am applying force in the direction they are going with the W,S keys
the distance the object is seperated from the actual physics object gets greater when moving farther from the origin (0,0)
Any help would be appreciated.
I am having a problem where my graphic will fall out of sync with the physics object.
I will try to post the relevant code so hopefully someone more experienced with Box2d can let me know what is going on 🙂
The physics loop runs in its own update at 60hz.
Code:
private function updatePhysics(e:Event):void
{
//step the physics world
pWorld.Step((1/60), 10, 10);
pWorld.DrawDebugData();
pWorld.ClearForces();
}
{
//step the physics world
pWorld.Step((1/60), 10, 10);
pWorld.DrawDebugData();
pWorld.ClearForces();
}
The Frame update handles mapping the player graphic to the physics location
Code:
//update the x and y with the physics properties
this.x = (_obj.GetWorldCenter().x * ratio) - width/2 ;
this.y = (_obj.GetWorldCenter().y * ratio) - height/2;
angle = (_obj.GetAngle() * (180 / Math.PI)) + 90;
this.x = (_obj.GetWorldCenter().x * ratio) - width/2 ;
this.y = (_obj.GetWorldCenter().y * ratio) - height/2;
angle = (_obj.GetAngle() * (180 / Math.PI)) + 90;
I am using flixel for rendering.
I run the application and they are matched up,
--> move the spaceship around a bit and its fine,
--> start moving some more and they get unsynchronized.
--> move back the opposite direction and they get resynchronized.
Really have no idea why this is happening.
I am applying force in the direction they are going with the W,S keys
Code:
if (FlxG.keys.pressed("W"))
{
//limit the speed of the ship
if (_obj.GetLinearVelocity().y < maxForwardSpeed && _obj.GetLinearVelocity().x < maxForwardSpeed) {
var x:Number = Math.cos(_obj.GetAngle()) * forwardSpeed;
var y:Number = Math.sin(_obj.GetAngle()) * forwardSpeed;
force = new b2Vec2(x,y);
var pos:b2Vec2 = _obj.GetWorldCenter();
_obj.ApplyForce(force, pos);
}
}
else if (FlxG.keys.pressed("S"))
{
//check for max linear velocity
if (_obj.GetLinearVelocity().y < maxReverseSpeed && _obj.GetLinearVelocity().x < maxReverseSpeed) {
var x:Number = Math.cos(_obj.GetAngle()) * reverseSpeed;
var y:Number = Math.sin(_obj.GetAngle()) * reverseSpeed;
//apply the force in the opposite direction
force = new b2Vec2(-x,-y);
var pos:b2Vec2 = _obj.GetWorldCenter();
_obj.ApplyForce(force, pos);
}
}
{
//limit the speed of the ship
if (_obj.GetLinearVelocity().y < maxForwardSpeed && _obj.GetLinearVelocity().x < maxForwardSpeed) {
var x:Number = Math.cos(_obj.GetAngle()) * forwardSpeed;
var y:Number = Math.sin(_obj.GetAngle()) * forwardSpeed;
force = new b2Vec2(x,y);
var pos:b2Vec2 = _obj.GetWorldCenter();
_obj.ApplyForce(force, pos);
}
}
else if (FlxG.keys.pressed("S"))
{
//check for max linear velocity
if (_obj.GetLinearVelocity().y < maxReverseSpeed && _obj.GetLinearVelocity().x < maxReverseSpeed) {
var x:Number = Math.cos(_obj.GetAngle()) * reverseSpeed;
var y:Number = Math.sin(_obj.GetAngle()) * reverseSpeed;
//apply the force in the opposite direction
force = new b2Vec2(-x,-y);
var pos:b2Vec2 = _obj.GetWorldCenter();
_obj.ApplyForce(force, pos);
}
}
the distance the object is seperated from the actual physics object gets greater when moving farther from the origin (0,0)
Any help would be appreciated.