study_cocos2dx 于 2014.08.23 15:09 提问

cocos2dx box2d 自定义/重置重力

Box2D中的Dynamics包下有一个b2Island类，类中有一个Solve方法，这方法通过gravity形参对刚体进行重力模拟，代码如下：

b.m_linearVelocity.x += step.dt * (gravity.x + b.m_invMass * b.m_force.x);

b.m_linearVelocity.y += step.dt * (gravity.y + b.m_invMass * b.m_force.y);

b.m_angularVelocity += step.dt * b.m_invI * b.m_torque;

``````        gravity = new b2Vec2(0, 0);

b.m_linearVelocity.x += step.dt * (gravity.x + b.m_invMass * b.m_force.x);

b.m_linearVelocity.y += step.dt * (gravity.y + b.m_invMass * b.m_force.y);

b.m_angularVelocity += step.dt * b.m_invI * b.m_torque;
``````

``````        //2012-7-27

//added by ladeng6666

//如果刚体有自定义m_customGravity属性，则用其进行重力模拟计算

if (b.m_customGravity != null) {

LDgravity = b.m_customGravity;

}else {

LDgravity = gravity;

}

//用更新后的LDgravity属性进行重力模拟计算

b.m_linearVelocity.x += step.dt * (LDgravity.x + b.m_invMass * b.m_force.x);

b.m_linearVelocity.y += step.dt * (LDgravity.y + b.m_invMass * b.m_force.y);

b.m_angularVelocity += step.dt * b.m_invI * b.m_torque;
``````

1个回答

ladeng6666   2014.09.06 10:55