Search in sources :

Example 16 with Rot

use of spacegraph.space2d.phys.common.Rot in project narchy by automenta.

the class Collision method findMaxSeparation.

/**
 * Find the max separation between poly1 and poly2 using edge normals from poly1.
 *
 * @param edgeIndex
 * @param poly1
 * @param xf1
 * @param poly2
 * @param xf2
 * @return
 */
public final void findMaxSeparation(EdgeResults results, final PolygonShape poly1, final Transform xf1, final PolygonShape poly2, final Transform xf2) {
    int count1 = poly1.vertices;
    int count2 = poly2.vertices;
    Tuple2f[] n1s = poly1.normals;
    Tuple2f[] v1s = poly1.vertex;
    Tuple2f[] v2s = poly2.vertex;
    Transform.mulTransToOutUnsafe(xf2, xf1, xf);
    final Rot xfq = xf;
    int bestIndex = 0;
    float maxSeparation = -Float.MAX_VALUE;
    for (int i = 0; i < count1; i++) {
        // Get poly1 normal in frame2.
        Rot.mulToOutUnsafe(xfq, n1s[i], n);
        Transform.mulToOutUnsafe(xf, v1s[i], v1);
        // Find deepest point for normal i.
        float si = Float.MAX_VALUE;
        for (int j = 0; j < count2; ++j) {
            Tuple2f v2sj = v2s[j];
            float sij = n.x * (v2sj.x - v1.x) + n.y * (v2sj.y - v1.y);
            if (sij < si) {
                si = sij;
            }
        }
        if (si > maxSeparation) {
            maxSeparation = si;
            bestIndex = i;
        }
    }
    results.edgeIndex = bestIndex;
    results.separation = maxSeparation;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) Rot(spacegraph.space2d.phys.common.Rot)

Example 17 with Rot

use of spacegraph.space2d.phys.common.Rot in project narchy by automenta.

the class EdgeShape method computeAABB.

@Override
public void computeAABB(AABB aabb, Transform xf, int childIndex) {
    final Tuple2f lowerBound = aabb.lowerBound;
    final Tuple2f upperBound = aabb.upperBound;
    final Rot xfq = xf;
    final float v1x = (xfq.c * m_vertex1.x - xfq.s * m_vertex1.y) + xf.pos.x;
    final float v1y = (xfq.s * m_vertex1.x + xfq.c * m_vertex1.y) + xf.pos.y;
    final float v2x = (xfq.c * m_vertex2.x - xfq.s * m_vertex2.y) + xf.pos.x;
    final float v2y = (xfq.s * m_vertex2.x + xfq.c * m_vertex2.y) + xf.pos.y;
    lowerBound.x = v1x < v2x ? v1x : v2x;
    lowerBound.y = v1y < v2y ? v1y : v2y;
    upperBound.x = v1x > v2x ? v1x : v2x;
    upperBound.y = v1y > v2y ? v1y : v2y;
    lowerBound.x -= radius;
    lowerBound.y -= radius;
    upperBound.x += radius;
    upperBound.y += radius;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) Rot(spacegraph.space2d.phys.common.Rot)

Example 18 with Rot

use of spacegraph.space2d.phys.common.Rot in project narchy by automenta.

the class Body2D method synchronizeTransform.

public final void synchronizeTransform() {
    // m_xf.q.set(m_sweep.a);
    // 
    // // m_xf.position = m_sweep.c - Mul(m_xf.R, m_sweep.localCenter);
    // Rot.mulToOutUnsafe(m_xf.q, m_sweep.localCenter, m_xf.p);
    // m_xf.p.mulLocal(-1).addLocal(m_sweep.c);
    // 
    Rot q = this;
    q.s = (float) Math.sin(sweep.a);
    q.c = (float) Math.cos(sweep.a);
    Tuple2f v = sweep.localCenter;
    pos.x = sweep.c.x - q.c * v.x + q.s * v.y;
    pos.y = sweep.c.y - q.s * v.x - q.c * v.y;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) Rot(spacegraph.space2d.phys.common.Rot)

Example 19 with Rot

use of spacegraph.space2d.phys.common.Rot in project narchy by automenta.

the class CircleShape method raycast.

// Collision Detection in Interactive 3D Environments by Gino van den Bergen
// From Section 3.1.2
// x = s + a * r
// norm(x) = radius
@Override
public final boolean raycast(RayCastOutput output, RayCastInput input, Transform transform, int childIndex) {
    final Tuple2f inputp1 = input.p1;
    final Tuple2f inputp2 = input.p2;
    final Rot tq = transform;
    final Tuple2f tp = transform.pos;
    // Rot.mulToOutUnsafe(transform.q, m_p, position);
    // position.addLocal(transform.p);
    final float positionx = tq.c * center.x - tq.s * center.y + tp.x;
    final float positiony = tq.s * center.x + tq.c * center.y + tp.y;
    final float sx = inputp1.x - positionx;
    final float sy = inputp1.y - positiony;
    // final float b = Vec2.dot(s, s) - m_radius * m_radius;
    final float b = sx * sx + sy * sy - radius * radius;
    // Solve quadratic equation.
    final float rx = inputp2.x - inputp1.x;
    final float ry = inputp2.y - inputp1.y;
    // final float c = Vec2.dot(s, r);
    // final float rr = Vec2.dot(r, r);
    final float c = sx * rx + sy * ry;
    final float rr = rx * rx + ry * ry;
    final float sigma = c * c - rr * b;
    // Check for negative discriminant and short segment.
    if (sigma < 0.0f || rr < Settings.EPSILON) {
        return false;
    }
    // Find the point of intersection of the line with the circle.
    float a = -(c + (float) Math.sqrt(sigma));
    // Is the intersection point on the segment?
    if (0.0f <= a && a <= input.maxFraction * rr) {
        a /= rr;
        output.fraction = a;
        output.normal.x = rx * a + sx;
        output.normal.y = ry * a + sy;
        output.normal.normalize();
        return true;
    }
    return false;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) Rot(spacegraph.space2d.phys.common.Rot)

Example 20 with Rot

use of spacegraph.space2d.phys.common.Rot in project narchy by automenta.

the class FrictionJoint method initVelocityConstraints.

/**
 * @see Joint#initVelocityConstraints(org.jbox2d.dynamics.TimeStep)
 */
@Override
public void initVelocityConstraints(final SolverData data) {
    m_indexA = A.island;
    m_indexB = B.island;
    m_localCenterA.set(A.sweep.localCenter);
    m_localCenterB.set(B.sweep.localCenter);
    m_invMassA = A.m_invMass;
    m_invMassB = B.m_invMass;
    m_invIA = A.m_invI;
    m_invIB = B.m_invI;
    float aA = data.positions[m_indexA].a;
    v2 vA = data.velocities[m_indexA];
    float wA = data.velocities[m_indexA].w;
    float aB = data.positions[m_indexB].a;
    v2 vB = data.velocities[m_indexB];
    float wB = data.velocities[m_indexB].w;
    final Tuple2f temp = pool.popVec2();
    final Rot qA = pool.popRot();
    final Rot qB = pool.popRot();
    qA.set(aA);
    qB.set(aB);
    // Compute the effective mass matrix.
    Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subbed(m_localCenterA), m_rA);
    Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subbed(m_localCenterB), m_rB);
    // J = [-I -r1_skew I r2_skew]
    // [ 0 -1 0 1]
    // r_skew = [-ry; rx]
    // Matlab
    // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
    // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
    // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;
    final Mat22 K = pool.popMat22();
    K.ex.x = mA + mB + iA * m_rA.y * m_rA.y + iB * m_rB.y * m_rB.y;
    K.ex.y = -iA * m_rA.x * m_rA.y - iB * m_rB.x * m_rB.y;
    K.ey.x = K.ex.y;
    K.ey.y = mA + mB + iA * m_rA.x * m_rA.x + iB * m_rB.x * m_rB.x;
    K.invertToOut(m_linearMass);
    m_angularMass = iA + iB;
    if (m_angularMass > 0.0f) {
        m_angularMass = 1.0f / m_angularMass;
    }
    if (data.step.warmStarting) {
        // Scale impulses to support a variable time step.
        m_linearImpulse.scaled(data.step.dtRatio);
        m_angularImpulse *= data.step.dtRatio;
        final Tuple2f P = pool.popVec2();
        P.set(m_linearImpulse);
        temp.set(P).scaled(mA);
        vA.subbed(temp);
        wA -= iA * (Tuple2f.cross(m_rA, P) + m_angularImpulse);
        temp.set(P).scaled(mB);
        vB.added(temp);
        wB += iB * (Tuple2f.cross(m_rB, P) + m_angularImpulse);
        pool.pushVec2(1);
    } else {
        m_linearImpulse.setZero();
        m_angularImpulse = 0.0f;
    }
    // data.velocities[m_indexA].v.set(vA);
    assert !(data.velocities[m_indexA].w != wA) || (data.velocities[m_indexA].w != wA);
    data.velocities[m_indexA].w = wA;
    // data.velocities[m_indexB].v.set(vB);
    data.velocities[m_indexB].w = wB;
    pool.pushRot(2);
    pool.pushVec2(1);
    pool.pushMat22(1);
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) Rot(spacegraph.space2d.phys.common.Rot) Mat22(spacegraph.space2d.phys.common.Mat22) spacegraph.util.math.v2(spacegraph.util.math.v2)

Aggregations

Rot (spacegraph.space2d.phys.common.Rot)25 Tuple2f (spacegraph.util.math.Tuple2f)23 spacegraph.util.math.v2 (spacegraph.util.math.v2)3 Mat22 (spacegraph.space2d.phys.common.Mat22)2 Transform (spacegraph.space2d.phys.common.Transform)2 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)1 PolygonFixture (spacegraph.space2d.phys.fracture.PolygonFixture)1