Search in sources :

Example 1 with MassData

use of spacegraph.space2d.phys.collision.shapes.MassData in project narchy by automenta.

the class Body2D method resetMassData.

/**
 * This resets the mass properties to the sum of the mass properties of the fixtures. This
 * normally does not need to be called unless you called setMassData to override the mass and you
 * later want to reset the mass.
 */
public final void resetMassData() {
    // Compute mass data from shapes. Each shape has its own density.
    mass = 0.0f;
    m_massArea = 0.0f;
    m_invMass = 0.0f;
    m_I = 0.0f;
    m_invI = 0.0f;
    sweep.localCenter.set(0, 0);
    final MassData massData = pmd;
    for (Fixture f = fixtures; f != null; f = f.next) {
        if (f.density != 0.0f) {
            f.getMassData(massData);
            m_massArea += massData.mass;
        }
    }
    // Static and kinematic bodies have zero mass.
    if (type == BodyType.STATIC || type == BodyType.KINEMATIC) {
        // m_sweep.c0 = m_sweep.c = m_xf.position;
        sweep.c0.set(pos);
        sweep.c.set(pos);
        sweep.a0 = sweep.a;
        return;
    }
    assert (type == BodyType.DYNAMIC);
    // Accumulate mass over all fixtures.
    final Tuple2f localCenter = W.pool.popVec2();
    localCenter.set(0, 0);
    final Tuple2f temp = W.pool.popVec2();
    for (Fixture f = fixtures; f != null; f = f.next) {
        if (f.density == 0.0f) {
            continue;
        }
        f.getMassData(massData);
        mass += massData.mass;
        // center += massData.mass * massData.center;
        temp.set(massData.center).scaled(massData.mass);
        localCenter.added(temp);
        m_I += massData.I;
    }
    // Compute center of mass.
    if (mass > 0.0f) {
        m_invMass = 1.0f / mass;
        localCenter.scaled(m_invMass);
    } else {
        // Force all dynamic bodies to have a positive mass.
        mass = 1.0f;
        m_invMass = 1.0f;
    }
    if (m_I > 0.0f && (flags & e_fixedRotationFlag) == 0) {
        // Center the inertia about the center of mass.
        m_I -= mass * Tuple2f.dot(localCenter, localCenter);
        assert (m_I > 0.0f);
        m_invI = 1.0f / m_I;
    } else {
        m_I = 0.0f;
        m_invI = 0.0f;
    }
    Tuple2f oldCenter = W.pool.popVec2();
    // Move center of mass.
    oldCenter.set(sweep.c);
    sweep.localCenter.set(localCenter);
    // m_sweep.c0 = m_sweep.c = Mul(m_xf, m_sweep.localCenter);
    Transform.mulToOutUnsafe(this, sweep.localCenter, sweep.c0);
    sweep.c.set(sweep.c0);
    // Update center of mass velocity.
    // m_linearVelocity += Cross(m_angularVelocity, m_sweep.c - oldCenter);
    temp.set(sweep.c).subbed(oldCenter);
    final Tuple2f temp2 = oldCenter;
    Tuple2f.crossToOutUnsafe(velAngular, temp, temp2);
    vel.added(temp2);
    W.pool.pushVec2(3);
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) MassData(spacegraph.space2d.phys.collision.shapes.MassData) PolygonFixture(spacegraph.space2d.phys.fracture.PolygonFixture)

Aggregations

MassData (spacegraph.space2d.phys.collision.shapes.MassData)1 PolygonFixture (spacegraph.space2d.phys.fracture.PolygonFixture)1 Tuple2f (spacegraph.util.math.Tuple2f)1