Search in sources :

Example 51 with Vec2

use of org.jbox2d.common.Vec2 in project libgdx by libgdx.

the class ParticleSystem method createParticleGroup.

public ParticleGroup createParticleGroup(ParticleGroupDef groupDef) {
    float stride = getParticleStride();
    final Transform identity = tempTransform;
    identity.setIdentity();
    Transform transform = tempTransform2;
    transform.setIdentity();
    int firstIndex = m_count;
    if (groupDef.shape != null) {
        final ParticleDef particleDef = tempParticleDef;
        particleDef.flags = groupDef.flags;
        particleDef.color = groupDef.color;
        particleDef.userData = groupDef.userData;
        Shape shape = groupDef.shape;
        transform.set(groupDef.position, groupDef.angle);
        AABB aabb = temp;
        int childCount = shape.getChildCount();
        for (int childIndex = 0; childIndex < childCount; childIndex++) {
            if (childIndex == 0) {
                shape.computeAABB(aabb, identity, childIndex);
            } else {
                AABB childAABB = temp2;
                shape.computeAABB(childAABB, identity, childIndex);
                aabb.combine(childAABB);
            }
        }
        final float upperBoundY = aabb.upperBound.y;
        final float upperBoundX = aabb.upperBound.x;
        for (float y = MathUtils.floor(aabb.lowerBound.y / stride) * stride; y < upperBoundY; y += stride) {
            for (float x = MathUtils.floor(aabb.lowerBound.x / stride) * stride; x < upperBoundX; x += stride) {
                Vec2 p = tempVec;
                p.x = x;
                p.y = y;
                if (shape.testPoint(identity, p)) {
                    Transform.mulToOut(transform, p, p);
                    particleDef.position.x = p.x;
                    particleDef.position.y = p.y;
                    p.subLocal(groupDef.position);
                    Vec2.crossToOutUnsafe(groupDef.angularVelocity, p, particleDef.velocity);
                    particleDef.velocity.addLocal(groupDef.linearVelocity);
                    createParticle(particleDef);
                }
            }
        }
    }
    int lastIndex = m_count;
    ParticleGroup group = new ParticleGroup();
    group.m_system = this;
    group.m_firstIndex = firstIndex;
    group.m_lastIndex = lastIndex;
    group.m_groupFlags = groupDef.groupFlags;
    group.m_strength = groupDef.strength;
    group.m_userData = groupDef.userData;
    group.m_transform.set(transform);
    group.m_destroyAutomatically = groupDef.destroyAutomatically;
    group.m_prev = null;
    group.m_next = m_groupList;
    if (m_groupList != null) {
        m_groupList.m_prev = group;
    }
    m_groupList = group;
    ++m_groupCount;
    for (int i = firstIndex; i < lastIndex; i++) {
        m_groupBuffer[i] = group;
    }
    updateContacts(true);
    if ((groupDef.flags & k_pairFlags) != 0) {
        for (int k = 0; k < m_contactCount; k++) {
            ParticleContact contact = m_contactBuffer[k];
            int a = contact.indexA;
            int b = contact.indexB;
            if (a > b) {
                int temp = a;
                a = b;
                b = temp;
            }
            if (firstIndex <= a && b < lastIndex) {
                if (m_pairCount >= m_pairCapacity) {
                    int oldCapacity = m_pairCapacity;
                    int newCapacity = m_pairCount != 0 ? 2 * m_pairCount : Settings.minParticleBufferCapacity;
                    m_pairBuffer = BufferUtils.reallocateBuffer(Pair.class, m_pairBuffer, oldCapacity, newCapacity);
                    m_pairCapacity = newCapacity;
                }
                Pair pair = m_pairBuffer[m_pairCount];
                pair.indexA = a;
                pair.indexB = b;
                pair.flags = contact.flags;
                pair.strength = groupDef.strength;
                pair.distance = MathUtils.distance(m_positionBuffer.data[a], m_positionBuffer.data[b]);
                m_pairCount++;
            }
        }
    }
    if ((groupDef.flags & k_triadFlags) != 0) {
        VoronoiDiagram diagram = new VoronoiDiagram(lastIndex - firstIndex);
        for (int i = firstIndex; i < lastIndex; i++) {
            diagram.addGenerator(m_positionBuffer.data[i], i);
        }
        diagram.generate(stride / 2);
        createParticleGroupCallback.system = this;
        createParticleGroupCallback.def = groupDef;
        createParticleGroupCallback.firstIndex = firstIndex;
        diagram.getNodes(createParticleGroupCallback);
    }
    if ((groupDef.groupFlags & ParticleGroupType.b2_solidParticleGroup) != 0) {
        computeDepthForGroup(group);
    }
    return group;
}
Also used : Shape(org.jbox2d.collision.shapes.Shape) Vec2(org.jbox2d.common.Vec2) Transform(org.jbox2d.common.Transform) AABB(org.jbox2d.collision.AABB)

Example 52 with Vec2

use of org.jbox2d.common.Vec2 in project libgdx by libgdx.

the class ParticleSystem method solvePressure.

void solvePressure(TimeStep step) {
    // that means dimensionless density
    for (int i = 0; i < m_count; i++) {
        m_accumulationBuffer[i] = 0;
    }
    for (int k = 0; k < m_bodyContactCount; k++) {
        ParticleBodyContact contact = m_bodyContactBuffer[k];
        int a = contact.index;
        float w = contact.weight;
        m_accumulationBuffer[a] += w;
    }
    for (int k = 0; k < m_contactCount; k++) {
        ParticleContact contact = m_contactBuffer[k];
        int a = contact.indexA;
        int b = contact.indexB;
        float w = contact.weight;
        m_accumulationBuffer[a] += w;
        m_accumulationBuffer[b] += w;
    }
    // ignores powder particles
    if ((m_allParticleFlags & k_noPressureFlags) != 0) {
        for (int i = 0; i < m_count; i++) {
            if ((m_flagsBuffer.data[i] & k_noPressureFlags) != 0) {
                m_accumulationBuffer[i] = 0;
            }
        }
    }
    // calculates pressure as a linear function of density
    float pressurePerWeight = m_pressureStrength * getCriticalPressure(step);
    for (int i = 0; i < m_count; i++) {
        float w = m_accumulationBuffer[i];
        float h = pressurePerWeight * MathUtils.max(0.0f, MathUtils.min(w, Settings.maxParticleWeight) - Settings.minParticleWeight);
        m_accumulationBuffer[i] = h;
    }
    // applies pressure between each particles in contact
    float velocityPerPressure = step.dt / (m_density * m_particleDiameter);
    for (int k = 0; k < m_bodyContactCount; k++) {
        ParticleBodyContact contact = m_bodyContactBuffer[k];
        int a = contact.index;
        Body b = contact.body;
        float w = contact.weight;
        float m = contact.mass;
        Vec2 n = contact.normal;
        Vec2 p = m_positionBuffer.data[a];
        float h = m_accumulationBuffer[a] + pressurePerWeight * w;
        final Vec2 f = tempVec;
        final float coef = velocityPerPressure * w * m * h;
        f.x = coef * n.x;
        f.y = coef * n.y;
        final Vec2 velData = m_velocityBuffer.data[a];
        final float particleInvMass = getParticleInvMass();
        velData.x -= particleInvMass * f.x;
        velData.y -= particleInvMass * f.y;
        b.applyLinearImpulse(f, p, true);
    }
    for (int k = 0; k < m_contactCount; k++) {
        ParticleContact contact = m_contactBuffer[k];
        int a = contact.indexA;
        int b = contact.indexB;
        float w = contact.weight;
        Vec2 n = contact.normal;
        float h = m_accumulationBuffer[a] + m_accumulationBuffer[b];
        final float fx = velocityPerPressure * w * h * n.x;
        final float fy = velocityPerPressure * w * h * n.y;
        final Vec2 velDataA = m_velocityBuffer.data[a];
        final Vec2 velDataB = m_velocityBuffer.data[b];
        velDataA.x -= fx;
        velDataA.y -= fy;
        velDataB.x += fx;
        velDataB.y += fy;
    }
}
Also used : Vec2(org.jbox2d.common.Vec2) Body(org.jbox2d.dynamics.Body)

Example 53 with Vec2

use of org.jbox2d.common.Vec2 in project libgdx by libgdx.

the class ParticleSystem method solveWall.

public void solveWall(TimeStep step) {
    for (int i = 0; i < m_count; i++) {
        if ((m_flagsBuffer.data[i] & ParticleType.b2_wallParticle) != 0) {
            final Vec2 r = m_velocityBuffer.data[i];
            r.x = 0.0f;
            r.y = 0.0f;
        }
    }
}
Also used : Vec2(org.jbox2d.common.Vec2)

Example 54 with Vec2

use of org.jbox2d.common.Vec2 in project libgdx by libgdx.

the class DefaultWorldPool method getVec2Array.

public final Vec2[] getVec2Array(int argLength) {
    if (!avecs.containsKey(argLength)) {
        Vec2[] ray = new Vec2[argLength];
        for (int i = 0; i < argLength; i++) {
            ray[i] = new Vec2();
        }
        avecs.put(argLength, ray);
    }
    assert (avecs.get(argLength).length == argLength) : "Array not built with correct length";
    return avecs.get(argLength);
}
Also used : Vec2(org.jbox2d.common.Vec2)

Example 55 with Vec2

use of org.jbox2d.common.Vec2 in project libgdx by libgdx.

the class DistanceJoint method solveVelocityConstraints.

@Override
public void solveVelocityConstraints(final SolverData data) {
    Vec2 vA = data.velocities[m_indexA].v;
    float wA = data.velocities[m_indexA].w;
    Vec2 vB = data.velocities[m_indexB].v;
    float wB = data.velocities[m_indexB].w;
    final Vec2 vpA = pool.popVec2();
    final Vec2 vpB = pool.popVec2();
    // Cdot = dot(u, v + cross(w, r))
    Vec2.crossToOutUnsafe(wA, m_rA, vpA);
    vpA.addLocal(vA);
    Vec2.crossToOutUnsafe(wB, m_rB, vpB);
    vpB.addLocal(vB);
    float Cdot = Vec2.dot(m_u, vpB.subLocal(vpA));
    float impulse = -m_mass * (Cdot + m_bias + m_gamma * m_impulse);
    m_impulse += impulse;
    float Px = impulse * m_u.x;
    float Py = impulse * m_u.y;
    vA.x -= m_invMassA * Px;
    vA.y -= m_invMassA * Py;
    wA -= m_invIA * (m_rA.x * Py - m_rA.y * Px);
    vB.x += m_invMassB * Px;
    vB.y += m_invMassB * Py;
    wB += m_invIB * (m_rB.x * Py - m_rB.y * Px);
    //    data.velocities[m_indexA].v.set(vA);
    data.velocities[m_indexA].w = wA;
    //    data.velocities[m_indexB].v.set(vB);
    data.velocities[m_indexB].w = wB;
    pool.pushVec2(2);
}
Also used : Vec2(org.jbox2d.common.Vec2)

Aggregations

Vec2 (org.jbox2d.common.Vec2)185 Rot (org.jbox2d.common.Rot)36 Body (org.jbox2d.dynamics.Body)11 World (org.jbox2d.dynamics.World)9 AABB (org.jbox2d.collision.AABB)8 Mat22 (org.jbox2d.common.Mat22)7 Joint (org.jbox2d.dynamics.joints.Joint)7 PulleyJoint (org.jbox2d.dynamics.joints.PulleyJoint)7 ManifoldPoint (org.jbox2d.collision.ManifoldPoint)6 Test (org.junit.jupiter.api.Test)6 PolygonShape (org.jbox2d.collision.shapes.PolygonShape)5 Transform (org.jbox2d.common.Transform)5 Vec3 (org.jbox2d.common.Vec3)5 BodyDef (org.jbox2d.dynamics.BodyDef)5 VelocityConstraintPoint (org.jbox2d.dynamics.contacts.ContactVelocityConstraint.VelocityConstraintPoint)5 CircleShape (org.jbox2d.collision.shapes.CircleShape)4 Test (org.junit.Test)4 AffineTransform (java.awt.geom.AffineTransform)3 Shape (org.jbox2d.collision.shapes.Shape)3 Mat33 (org.jbox2d.common.Mat33)3