【问题标题】:Box2d center mass of welded bodies?Box2d焊接体的中心质量?
【发布时间】:2017-10-29 11:29:08
【问题描述】:

我有一个由焊接体组成的结构。

我应用脉冲/力来移动结构,但我注意到结构并没有完全笔直地移动。它开始向 Vector 的方向移动,然后缓慢转动,最终绕了一圈。

问题已解决Here

看来问题在于需要将力施加到质心。但是,我不确定如何从焊接在一起的结构中获得质心。

我有结构中每个物体的质心,但它们都在中心。有没有办法计算复杂焊接结构的质心?

【问题讨论】:

    标签: c++ box2d game-physics jbox2d


    【解决方案1】:

    这就像对每个对象的质心的世界坐标进行加权求和一样简单。

    b2Vec2 GetWorldCentreOfMass(std::vector<b2Body> *bodies) {
    
        // Compute total mass
        float32 totalMass = 0.f;
        for (b2Body &body : bodies) {
            totalMass += body.GetMass();
        }
    
        // Compute centre of mass
        b2Vec2 centreOfMass(0.f,0.f);
        for (b2Body &body : bodies) {
            b2Vec2 r = body.GetWorldCenter();
            float32 m = body.GetMass();
            centreOfMass += m*r / totalMass;
        }
    
        return centreOfMass;
    }
    

    见:https://en.wikipedia.org/wiki/Center_of_mass#A_system_of_particles

    然后使用 void ApplyLinearImpulse(const b2Vec2&amp; impulse, const b2Vec2&amp; point);:

    b2Vec2 centreOfMass = GetWorldCentreOfMass(bodies);
    for (b2Body &body : bodies) {
        body.ApplyLinearImpulse(impulse, centreOfMass);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      相关资源
      最近更新 更多