Search in sources :

Example 1 with CircleShape

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

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

the class JBox2DTest method testNewWorld.

@Test
public void testNewWorld() {
    World world = new World(new Vec2(0, -9.8f));
    BodyDef axisDef = new BodyDef();
    axisDef.type = BodyType.STATIC;
    axisDef.position = new Vec2(3, 3);
    Body axis = world.createBody(axisDef);
    CircleShape axisShape = new CircleShape();
    axisShape.setRadius(0.02f);
    axisShape.m_p.set(0, 0);
// FixtureDef axisFixture = new FixtureDef();
// axisFixture.shape = axisShape;
// axis.createFixture(axisFixture);
}
Also used : CircleShape(org.jbox2d.collision.shapes.CircleShape) Vec2(org.jbox2d.common.Vec2) World(org.jbox2d.dynamics.World) BodyDef(org.jbox2d.dynamics.BodyDef) Body(org.jbox2d.dynamics.Body) Test(org.junit.Test)

Example 3 with CircleShape

use of org.jbox2d.collision.shapes.CircleShape in project SnapStudio by reportmill.

the class PhysicsRunner method createShape.

/**
 * Creates a Box2D shape for given snap shape.
 */
public org.jbox2d.collision.shapes.Shape[] createShape(Shape aShape) {
    // Handle Rect (simple case)
    if (aShape instanceof Rect) {
        Rect rect = (Rect) aShape;
        PolygonShape pshape = new PolygonShape();
        float pw = viewToBox(rect.width / 2);
        float ph = viewToBox(rect.height / 2);
        pshape.setAsBox(pw, ph);
        return new org.jbox2d.collision.shapes.Shape[] { pshape };
    }
    // Handle Ellipse
    if (aShape instanceof Ellipse && aShape.getWidth() == aShape.getHeight()) {
        Ellipse elp = (Ellipse) aShape;
        CircleShape cshape = new CircleShape();
        cshape.setRadius(viewToBox(elp.getWidth() / 2));
        return new org.jbox2d.collision.shapes.Shape[] { cshape };
    }
    // Handle Arc
    if (aShape instanceof Arc && aShape.getWidth() == aShape.getHeight()) {
        Arc arc = (Arc) aShape;
        if (arc.getSweepAngle() == 360) {
            CircleShape cshape = new CircleShape();
            cshape.setRadius(viewToBox(arc.getWidth() / 2));
            return new org.jbox2d.collision.shapes.Shape[] { cshape };
        }
    }
    // Handle Polygon if Simple, Convex and less than 8 points
    if (aShape instanceof Polygon) {
        Polygon poly = (Polygon) aShape;
        org.jbox2d.collision.shapes.Shape pshape = createShape(poly);
        if (pshape != null)
            return new org.jbox2d.collision.shapes.Shape[] { pshape };
    }
    // Get shape centered around shape midpoint
    Rect bnds = aShape.getBounds();
    Shape shape = aShape.copyFor(new Transform(-bnds.width / 2, -bnds.height / 2));
    // Get convex Polygons for shape
    Polygon[] convexPolys = Polygon.getConvexPolys(shape, 8);
    List<org.jbox2d.collision.shapes.Shape> pshapes = new ArrayList();
    // Iterate over polygons
    for (Polygon cpoly : convexPolys) {
        // Try simple case
        org.jbox2d.collision.shapes.Shape pshp = createShape(cpoly);
        if (pshp != null)
            pshapes.add(pshp);
        else
            System.err.println("PhysicsRunner:.createShape: failure");
    }
    // Return Box2D shapes array
    return pshapes.toArray(new org.jbox2d.collision.shapes.Shape[0]);
}
Also used : PolygonShape(org.jbox2d.collision.shapes.PolygonShape) CircleShape(org.jbox2d.collision.shapes.CircleShape) PolygonShape(org.jbox2d.collision.shapes.PolygonShape) CircleShape(org.jbox2d.collision.shapes.CircleShape)

Example 4 with CircleShape

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

the class JBox2DTest method testSimpleBall.

@Test
public void testSimpleBall() {
    World world = new World(new Vec2(0, -9.8f));
    float ballRadius = 0.15f;
    BodyDef ballDef = new BodyDef();
    ballDef.type = BodyType.DYNAMIC;
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.friction = 0.3f;
    fixtureDef.restitution = 0.3f;
    fixtureDef.density = 0.2f;
    CircleShape shape = new CircleShape();
    shape.m_radius = ballRadius;
    fixtureDef.shape = shape;
    int i = 0;
    int j = 0;
    float x = (j + 0.5f) * (ballRadius * 2 + 0.01f);
    float y = (i + 0.5f) * (ballRadius * 2 + 0.01f);
    ballDef.position.x = 3 + x;
    ballDef.position.y = 3 + y;
    Body theBall = world.createBody(ballDef);
    theBall.createFixture(fixtureDef);
    for (int k = 0; k < 100; k++) {
        world.step(0.01f, 20, 40);
    }
    Vec2 thePosition = theBall.getPosition();
    int theX = (int) (thePosition.x * 1000);
    int theY = (int) (thePosition.y * 1000);
    System.out.println("Finally ended at ");
    System.out.println(theX);
    System.out.println(theY);
}
Also used : CircleShape(org.jbox2d.collision.shapes.CircleShape) Vec2(org.jbox2d.common.Vec2) World(org.jbox2d.dynamics.World) BodyDef(org.jbox2d.dynamics.BodyDef) Body(org.jbox2d.dynamics.Body) FixtureDef(org.jbox2d.dynamics.FixtureDef) ManifoldPoint(org.jbox2d.collision.ManifoldPoint) Test(org.junit.Test)

Example 5 with CircleShape

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

the class JBox2DTest method testCircleShape.

@Test
public void testCircleShape() {
    CircleShape axisShape = new CircleShape();
    axisShape.setRadius(0.02f);
    axisShape.m_p.set(0, 0);
}
Also used : CircleShape(org.jbox2d.collision.shapes.CircleShape) Test(org.junit.Test)

Aggregations

CircleShape (org.jbox2d.collision.shapes.CircleShape)6 Vec2 (org.jbox2d.common.Vec2)4 Body (org.jbox2d.dynamics.Body)3 BodyDef (org.jbox2d.dynamics.BodyDef)3 Test (org.junit.Test)3 PolygonShape (org.jbox2d.collision.shapes.PolygonShape)2 FixtureDef (org.jbox2d.dynamics.FixtureDef)2 World (org.jbox2d.dynamics.World)2 ManifoldPoint (org.jbox2d.collision.ManifoldPoint)1 Shape (org.jbox2d.collision.shapes.Shape)1 Fixture (org.jbox2d.dynamics.Fixture)1