study_cocos2dx

2014-08-23 07:09 阅读 3.1k

关于Box2d-自定义重力

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

//如果刚体有自定义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条回答默认 最新

• 拉登Dony 2014-09-06 02:55