我正在玩CocosSharp中的box2d。我确实有一个问题 - 对象移动得太慢了!尽管价值很高......
// Physics
public const float GRAVITY = -99999999999999999999999999999f;
public const int PTM = 32;
public const float ROCKET_MASS = 10000f;
public const float TIMESTEP = 1f/240f;
请注意,我已经改变了时间步。我试图使用质量,但它完全没有意义!
// Add rocket's physics
var def = new b2BodyDef ();
def.allowSleep = true;
def.position = new b2Vec2(0.0f, 0.0f);
def.type = b2BodyType.b2_dynamicBody;
body = world.CreateBody (def);
// body.Mass = Globals.ROCKET_MASS;
body.SetActive(true);
b2PolygonShape bodyBox = new b2PolygonShape();
bodyBox.SetAsBox(0.1f , 0.5f); // !
b2FixtureDef bodyFixture = new b2FixtureDef();
bodyFixture.shape = bodyBox;
bodyFixture.density = 0.5f;
bodyFixture.friction = 0.4f;
body.CreateFixture(bodyFixture);
以下是我初始化物理的方法:
void InitPhysics ()
{
// Init the world
var gravity = new b2Vec2 (0.0f, Globals.GRAVITY);
world = new b2World (gravity);
world.SetAllowSleeping (true);
world.SetContinuousPhysics (true);
// Now init the ground
new Ground(world);
}
这是我的渲染器代码:
private void StartScheduling()
{
Schedule(t =>
{
if(t <= Globals.TIMESTEP)
timeAccumulator += t;
else
timeAccumulator += Globals.TIMESTEP;
while (timeAccumulator >= Globals.TIMESTEP)
{
world.Step(Globals.TIMESTEP, 6, 2);
timeAccumulator -= Globals.TIMESTEP;
// If touched, then do apply force
if (touch)
{
rocket.body.ApplyLinearImpulse(new b2Vec2(0f, 10000f), b2Vec2.Zero);
}
world.DrawDebugData();
rocket.UpdateRocketPosition();
}
world.ClearForces();
});
}
我好累,请帮忙!火箭必须快速下降,但事实并非如此!
答案 0 :(得分:0)
感谢@ iforce2d。
解决方案是在将结果渲染到屏幕时使用PTM调整box2d单位。至于我,我不明白,box2d世界是一个非常小的东西,并不依赖于设备的屏幕尺寸,我应该将它(放大)用于渲染。现在它的效果非常好:
public void UpdateRocketPosition ()
{
// PositionX, Y are sprite's positions
PositionX = body.Position.x * Globals.PTM;
PositionY = body.Position.y * Globals.PTM;
}