Search in sources :

Example 71 with Vec2

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

the class WorldRayCastWrapper method drawParticleSystem.

private void drawParticleSystem(ParticleSystem system) {
    boolean wireframe = (m_debugDraw.getFlags() & DebugDraw.e_wireframeDrawingBit) != 0;
    int particleCount = system.getParticleCount();
    if (particleCount != 0) {
        float particleRadius = system.getParticleRadius();
        Vec2[] positionBuffer = system.getParticlePositionBuffer();
        ParticleColor[] colorBuffer = null;
        if (system.m_colorBuffer.data != null) {
            colorBuffer = system.getParticleColorBuffer();
        }
        if (wireframe) {
            m_debugDraw.drawParticlesWireframe(positionBuffer, particleRadius, colorBuffer, particleCount);
        } else {
            m_debugDraw.drawParticles(positionBuffer, particleRadius, colorBuffer, particleCount);
        }
    }
}
Also used : Vec2(org.jbox2d.common.Vec2) ParticleColor(org.jbox2d.particle.ParticleColor) Joint(org.jbox2d.dynamics.joints.Joint) PulleyJoint(org.jbox2d.dynamics.joints.PulleyJoint)

Example 72 with Vec2

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

the class PolygonShape method testPoint.

@Override
public final boolean testPoint(final Transform xf, final Vec2 p) {
    float tempx, tempy;
    final Rot xfq = xf.q;
    tempx = p.x - xf.p.x;
    tempy = p.y - xf.p.y;
    final float pLocalx = xfq.c * tempx + xfq.s * tempy;
    final float pLocaly = -xfq.s * tempx + xfq.c * tempy;
    if (m_debug) {
        System.out.println("--testPoint debug--");
        System.out.println("Vertices: ");
        for (int i = 0; i < m_count; ++i) {
            System.out.println(m_vertices[i]);
        }
        System.out.println("pLocal: " + pLocalx + ", " + pLocaly);
    }
    for (int i = 0; i < m_count; ++i) {
        Vec2 vertex = m_vertices[i];
        Vec2 normal = m_normals[i];
        tempx = pLocalx - vertex.x;
        tempy = pLocaly - vertex.y;
        final float dot = normal.x * tempx + normal.y * tempy;
        if (dot > 0.0f) {
            return false;
        }
    }
    return true;
}
Also used : Rot(org.jbox2d.common.Rot) Vec2(org.jbox2d.common.Vec2)

Example 73 with Vec2

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

the class PolygonShape method set.

/**
   * Create a convex hull from the given array of points. The count must be in the range [3,
   * Settings.maxPolygonVertices]. This method takes an arraypool for pooling.
   * 
   * @warning the points may be re-ordered, even if they form a convex polygon.
   * @warning collinear points are removed.
   */
public final void set(final Vec2[] verts, final int num, final Vec2Array vecPool, final IntArray intPool) {
    assert (3 <= num && num <= Settings.maxPolygonVertices);
    if (num < 3) {
        setAsBox(1.0f, 1.0f);
        return;
    }
    int n = MathUtils.min(num, Settings.maxPolygonVertices);
    // Perform welding and copy vertices into local buffer.
    Vec2[] ps = (vecPool != null) ? vecPool.get(Settings.maxPolygonVertices) : new Vec2[Settings.maxPolygonVertices];
    int tempCount = 0;
    for (int i = 0; i < n; ++i) {
        Vec2 v = verts[i];
        boolean unique = true;
        for (int j = 0; j < tempCount; ++j) {
            if (MathUtils.distanceSquared(v, ps[j]) < 0.5f * Settings.linearSlop) {
                unique = false;
                break;
            }
        }
        if (unique) {
            ps[tempCount++] = v;
        }
    }
    n = tempCount;
    if (n < 3) {
        // Polygon is degenerate.
        assert (false);
        setAsBox(1.0f, 1.0f);
        return;
    }
    // Create the convex hull using the Gift wrapping algorithm
    // http://en.wikipedia.org/wiki/Gift_wrapping_algorithm
    // Find the right most point on the hull
    int i0 = 0;
    float x0 = ps[0].x;
    for (int i = 1; i < n; ++i) {
        float x = ps[i].x;
        if (x > x0 || (x == x0 && ps[i].y < ps[i0].y)) {
            i0 = i;
            x0 = x;
        }
    }
    int[] hull = (intPool != null) ? intPool.get(Settings.maxPolygonVertices) : new int[Settings.maxPolygonVertices];
    int m = 0;
    int ih = i0;
    while (true) {
        hull[m] = ih;
        int ie = 0;
        for (int j = 1; j < n; ++j) {
            if (ie == ih) {
                ie = j;
                continue;
            }
            Vec2 r = pool1.set(ps[ie]).subLocal(ps[hull[m]]);
            Vec2 v = pool2.set(ps[j]).subLocal(ps[hull[m]]);
            float c = Vec2.cross(r, v);
            if (c < 0.0f) {
                ie = j;
            }
            // Collinearity check
            if (c == 0.0f && v.lengthSquared() > r.lengthSquared()) {
                ie = j;
            }
        }
        ++m;
        ih = ie;
        if (ie == i0) {
            break;
        }
    }
    this.m_count = m;
    // Copy vertices.
    for (int i = 0; i < m_count; ++i) {
        if (m_vertices[i] == null) {
            m_vertices[i] = new Vec2();
        }
        m_vertices[i].set(ps[hull[i]]);
    }
    final Vec2 edge = pool1;
    // Compute normals. Ensure the edges have non-zero length.
    for (int i = 0; i < m_count; ++i) {
        final int i1 = i;
        final int i2 = i + 1 < m_count ? i + 1 : 0;
        edge.set(m_vertices[i2]).subLocal(m_vertices[i1]);
        assert (edge.lengthSquared() > Settings.EPSILON * Settings.EPSILON);
        Vec2.crossToOutUnsafe(edge, 1f, m_normals[i]);
        m_normals[i].normalize();
    }
    // Compute the polygon centroid.
    computeCentroidToOut(m_vertices, m_count, m_centroid);
}
Also used : Vec2(org.jbox2d.common.Vec2)

Example 74 with Vec2

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

the class PolygonShape method computeAABB.

@Override
public final void computeAABB(final AABB aabb, final Transform xf, int childIndex) {
    final Vec2 lower = aabb.lowerBound;
    final Vec2 upper = aabb.upperBound;
    final Vec2 v1 = m_vertices[0];
    final float xfqc = xf.q.c;
    final float xfqs = xf.q.s;
    final float xfpx = xf.p.x;
    final float xfpy = xf.p.y;
    lower.x = (xfqc * v1.x - xfqs * v1.y) + xfpx;
    lower.y = (xfqs * v1.x + xfqc * v1.y) + xfpy;
    upper.x = lower.x;
    upper.y = lower.y;
    for (int i = 1; i < m_count; ++i) {
        Vec2 v2 = m_vertices[i];
        // Vec2 v = Mul(xf, m_vertices[i]);
        float vx = (xfqc * v2.x - xfqs * v2.y) + xfpx;
        float vy = (xfqs * v2.x + xfqc * v2.y) + xfpy;
        lower.x = lower.x < vx ? lower.x : vx;
        lower.y = lower.y < vy ? lower.y : vy;
        upper.x = upper.x > vx ? upper.x : vx;
        upper.y = upper.y > vy ? upper.y : vy;
    }
    lower.x -= m_radius;
    lower.y -= m_radius;
    upper.x += m_radius;
    upper.y += m_radius;
}
Also used : Vec2(org.jbox2d.common.Vec2)

Example 75 with Vec2

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

the class Body 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);
    //
    m_xf.q.s = MathUtils.sin(m_sweep.a);
    m_xf.q.c = MathUtils.cos(m_sweep.a);
    Rot q = m_xf.q;
    Vec2 v = m_sweep.localCenter;
    m_xf.p.x = m_sweep.c.x - q.c * v.x + q.s * v.y;
    m_xf.p.y = m_sweep.c.y - q.s * v.x - q.c * v.y;
}
Also used : Rot(org.jbox2d.common.Rot) 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