Search in sources :

Example 1 with Shape

use of org.jbox2d.collision.shapes.Shape 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 2 with Shape

use of org.jbox2d.collision.shapes.Shape in project Bytecoder by mirkosertic.

the class JBox2DSimulation method render.

private static void render() {
    renderingContext2D.clear();
    renderingContext2D.save();
    renderingContext2D.translate(0, 600);
    renderingContext2D.scale(1, -1);
    renderingContext2D.scale(100, 100);
    renderingContext2D.lineWidth(0.01f);
    for (Body body = scene.getWorld().getBodyList(); body != null; body = body.getNext()) {
        Vec2 center = body.getPosition();
        renderingContext2D.save();
        renderingContext2D.translate(center.x, center.y);
        renderingContext2D.rotate(body.getAngle());
        for (Fixture fixture = body.getFixtureList(); fixture != null; fixture = fixture.getNext()) {
            Shape shape = fixture.getShape();
            if (shape.getType() == ShapeType.CIRCLE) {
                CircleShape circle = (CircleShape) shape;
                renderingContext2D.beginPath();
                renderingContext2D.arc(circle.m_p.x, circle.m_p.y, circle.getRadius(), 0, Math.PI * 2, true);
                renderingContext2D.closePath();
                renderingContext2D.stroke();
            } else if (shape.getType() == ShapeType.POLYGON) {
                PolygonShape poly = (PolygonShape) shape;
                Vec2[] vertices = poly.getVertices();
                renderingContext2D.beginPath();
                renderingContext2D.moveTo(vertices[0].x, vertices[0].y);
                for (int i = 1; i < poly.getVertexCount(); ++i) {
                    renderingContext2D.lineTo(vertices[i].x, vertices[i].y);
                }
                renderingContext2D.closePath();
                renderingContext2D.stroke();
            }
        }
        renderingContext2D.restore();
    }
    renderingContext2D.restore();
}
Also used : PolygonShape(org.jbox2d.collision.shapes.PolygonShape) CircleShape(org.jbox2d.collision.shapes.CircleShape) PolygonShape(org.jbox2d.collision.shapes.PolygonShape) Shape(org.jbox2d.collision.shapes.Shape) CircleShape(org.jbox2d.collision.shapes.CircleShape) Vec2(org.jbox2d.common.Vec2) Fixture(org.jbox2d.dynamics.Fixture) Body(org.jbox2d.dynamics.Body)

Example 3 with Shape

use of org.jbox2d.collision.shapes.Shape in project HackerHop by nicovank.

the class PlayerTest method playerShapeTest.

@Test
void playerShapeTest() {
    World world = new World(new Vec2(0, -50));
    Vec2 position = new Vec2(0, 10);
    Player p = new Player(world, position);
    Shape s = p.getBody().getFixtureList().m_shape;
    PolygonShape r = new PolygonShape();
    r.setAsBox(3, 3);
    assertEquals(s.getType(), r.getType());
}
Also used : PolygonShape(org.jbox2d.collision.shapes.PolygonShape) PolygonShape(org.jbox2d.collision.shapes.PolygonShape) Shape(org.jbox2d.collision.shapes.Shape) Vec2(org.jbox2d.common.Vec2) World(org.jbox2d.dynamics.World) Test(org.junit.jupiter.api.Test)

Example 4 with Shape

use of org.jbox2d.collision.shapes.Shape in project libgdx by libgdx.

the class Contact method update.

public void update(ContactListener listener) {
    oldManifold.set(m_manifold);
    // Re-enable this contact.
    m_flags |= ENABLED_FLAG;
    boolean touching = false;
    boolean wasTouching = (m_flags & TOUCHING_FLAG) == TOUCHING_FLAG;
    boolean sensorA = m_fixtureA.isSensor();
    boolean sensorB = m_fixtureB.isSensor();
    boolean sensor = sensorA || sensorB;
    Body bodyA = m_fixtureA.getBody();
    Body bodyB = m_fixtureB.getBody();
    Transform xfA = bodyA.getTransform();
    Transform xfB = bodyB.getTransform();
    if (sensor) {
        Shape shapeA = m_fixtureA.getShape();
        Shape shapeB = m_fixtureB.getShape();
        touching = pool.getCollision().testOverlap(shapeA, m_indexA, shapeB, m_indexB, xfA, xfB);
        // Sensors don't generate manifolds.
        m_manifold.pointCount = 0;
    } else {
        evaluate(m_manifold, xfA, xfB);
        touching = m_manifold.pointCount > 0;
        // stored impulses to warm start the solver.
        for (int i = 0; i < m_manifold.pointCount; ++i) {
            ManifoldPoint mp2 = m_manifold.points[i];
            mp2.normalImpulse = 0.0f;
            mp2.tangentImpulse = 0.0f;
            ContactID id2 = mp2.id;
            for (int j = 0; j < oldManifold.pointCount; ++j) {
                ManifoldPoint mp1 = oldManifold.points[j];
                if (mp1.id.isEqual(id2)) {
                    mp2.normalImpulse = mp1.normalImpulse;
                    mp2.tangentImpulse = mp1.tangentImpulse;
                    break;
                }
            }
        }
        if (touching != wasTouching) {
            bodyA.setAwake(true);
            bodyB.setAwake(true);
        }
    }
    if (touching) {
        m_flags |= TOUCHING_FLAG;
    } else {
        m_flags &= ~TOUCHING_FLAG;
    }
    if (listener == null) {
        return;
    }
    if (wasTouching == false && touching == true) {
        listener.beginContact(this);
    }
    if (wasTouching == true && touching == false) {
        listener.endContact(this);
    }
    if (sensor == false && touching) {
        listener.preSolve(this, oldManifold);
    }
}
Also used : Shape(org.jbox2d.collision.shapes.Shape) ManifoldPoint(org.jbox2d.collision.ManifoldPoint) ContactID(org.jbox2d.collision.ContactID) Transform(org.jbox2d.common.Transform) Body(org.jbox2d.dynamics.Body) ManifoldPoint(org.jbox2d.collision.ManifoldPoint)

Example 5 with Shape

use of org.jbox2d.collision.shapes.Shape in project libgdx by libgdx.

the class Contact method getWorldManifold.

/**
   * Get the world manifold.
   */
public void getWorldManifold(WorldManifold worldManifold) {
    final Body bodyA = m_fixtureA.getBody();
    final Body bodyB = m_fixtureB.getBody();
    final Shape shapeA = m_fixtureA.getShape();
    final Shape shapeB = m_fixtureB.getShape();
    worldManifold.initialize(m_manifold, bodyA.getTransform(), shapeA.m_radius, bodyB.getTransform(), shapeB.m_radius);
}
Also used : Shape(org.jbox2d.collision.shapes.Shape) Body(org.jbox2d.dynamics.Body)

Aggregations

Shape (org.jbox2d.collision.shapes.Shape)6 Body (org.jbox2d.dynamics.Body)4 Vec2 (org.jbox2d.common.Vec2)3 ManifoldPoint (org.jbox2d.collision.ManifoldPoint)2 PolygonShape (org.jbox2d.collision.shapes.PolygonShape)2 Transform (org.jbox2d.common.Transform)2 Fixture (org.jbox2d.dynamics.Fixture)2 AABB (org.jbox2d.collision.AABB)1 ContactID (org.jbox2d.collision.ContactID)1 Manifold (org.jbox2d.collision.Manifold)1 WorldManifold (org.jbox2d.collision.WorldManifold)1 CircleShape (org.jbox2d.collision.shapes.CircleShape)1 World (org.jbox2d.dynamics.World)1 VelocityConstraintPoint (org.jbox2d.dynamics.contacts.ContactVelocityConstraint.VelocityConstraintPoint)1 Test (org.junit.jupiter.api.Test)1