Search in sources :

Example 1 with PolygonFixture

use of spacegraph.space2d.phys.fracture.PolygonFixture in project narchy by automenta.

the class PhyWall method drawBody.

private void drawBody(Body2D body, GL2 gl) {
    if (body.data() instanceof PhyWindow.WallBody) {
        // its rendered already via its Surface
        return;
    }
    if (body instanceof Consumer) {
        // HACK make better custom enderer interface
        ((Consumer) body).accept(gl);
        return;
    }
    // boolean active = body.isActive();
    boolean awake = body.isAwake();
    gl.glColor4f(0.5f, 0.5f, 0.5f, awake ? 0.75f : 0.65f);
    // List<PolygonFixture> generalPolygons = new FasterList<>();
    for (Fixture f = body.fixtures; f != null; f = f.next) {
        PolygonFixture pg = f.polygon;
        if (pg != null) {
        // generalPolygons.add(pg);
        } else {
            Shape shape = f.shape();
            switch(shape.m_type) {
                case POLYGON:
                    Draw.poly(body, gl, (PolygonShape) shape);
                    break;
                case CIRCLE:
                    CircleShape circle = (CircleShape) shape;
                    float r = circle.radius;
                    v2 v = new v2();
                    body.getWorldPointToOut(circle.center, v);
                    // Point p = getPoint(v);
                    // int wr = (int) (r * zoom);
                    // g.fillOval(p.x - wr, p.y - wr, wr * 2, wr * 2);
                    Draw.circle(gl, v, true, r, 9);
                    break;
                case EDGE:
                    EdgeShape edge = (EdgeShape) shape;
                    Tuple2f p1 = edge.m_vertex1;
                    Tuple2f p2 = edge.m_vertex2;
                    gl.glLineWidth(4f);
                    Draw.line(gl, p1.x, p1.y, p2.x, p2.y);
                    break;
            }
        }
    }
// if (generalPolygons.size() != 0) {
// PolygonFixture[] polygonArray = generalPolygons.toArray(new PolygonFixture[generalPolygons.size()]);
// for (PolygonFixture poly : polygonArray) {
// int n = poly.size();
// int x[] = new int[n];
// int y[] = new int[n];
// for (int i = 0; i < n; ++i) {
// body.getWorldPointToOut(poly.get(i), v);
// Point p = getPoint(v);
// x[i] = p.x;
// y[i] = p.y;
// }
// g.fillPolygon(x, y, n);
// }
}
Also used : EdgeShape(spacegraph.space2d.phys.collision.shapes.EdgeShape) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) Shape(spacegraph.space2d.phys.collision.shapes.Shape) EdgeShape(spacegraph.space2d.phys.collision.shapes.EdgeShape) Tuple2f(spacegraph.util.math.Tuple2f) Consumer(java.util.function.Consumer) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) PolygonFixture(spacegraph.space2d.phys.fracture.PolygonFixture) PolygonFixture(spacegraph.space2d.phys.fracture.PolygonFixture) spacegraph.util.math.v2(spacegraph.util.math.v2)

Example 2 with PolygonFixture

use of spacegraph.space2d.phys.fracture.PolygonFixture in project narchy by automenta.

the class Box2DTests method drawBody.

private void drawBody(Body2D body) {
    if (body.getType() == BodyType.DYNAMIC) {
        g.setColor(Color.LIGHT_GRAY);
    } else {
        g.setColor(Color.GRAY);
    }
    Tuple2f v = new Vec2();
    MyList<PolygonFixture> generalPolygons = new MyList<>();
    for (Fixture f = body.fixtures; f != null; f = f.next) {
        PolygonFixture pg = f.polygon;
        if (pg != null) {
            if (!generalPolygons.contains(pg)) {
                generalPolygons.add(pg);
            }
        } else {
            Shape shape = f.shape();
            switch(shape.m_type) {
                case POLYGON:
                    PolygonShape poly = (PolygonShape) shape;
                    for (int i = 0; i < poly.vertices; ++i) {
                        body.getWorldPointToOut(poly.vertex[i], v);
                        Point p = getPoint(v);
                        x[i] = p.x;
                        y[i] = p.y;
                    }
                    g.fillPolygon(x, y, poly.vertices);
                    break;
                case CIRCLE:
                    CircleShape circle = (CircleShape) shape;
                    float r = circle.radius;
                    body.getWorldPointToOut(circle.center, v);
                    Point p = getPoint(v);
                    int wr = (int) (r * zoom);
                    g.fillOval(p.x - wr, p.y - wr, wr * 2, wr * 2);
                    break;
                case EDGE:
                    EdgeShape edge = (EdgeShape) shape;
                    Tuple2f v1 = edge.m_vertex1;
                    Tuple2f v2 = edge.m_vertex2;
                    Point p1 = getPoint(v1);
                    Point p2 = getPoint(v2);
                    g.drawLine(p1.x, p1.y, p2.x, p2.y);
                    break;
            }
        }
    }
    if (generalPolygons.size() != 0) {
        PolygonFixture[] polygonArray = generalPolygons.toArray(new PolygonFixture[generalPolygons.size()]);
        for (PolygonFixture poly : polygonArray) {
            int n = poly.size();
            int[] x = new int[n];
            int[] y = new int[n];
            for (int i = 0; i < n; ++i) {
                body.getWorldPointToOut(poly.get(i), v);
                Point p = getPoint(v);
                x[i] = p.x;
                y[i] = p.y;
            }
            g.fillPolygon(x, y, n);
        }
    }
}
Also used : EdgeShape(spacegraph.space2d.phys.collision.shapes.EdgeShape) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) Shape(spacegraph.space2d.phys.collision.shapes.Shape) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) EdgeShape(spacegraph.space2d.phys.collision.shapes.EdgeShape) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) DistanceJoint(spacegraph.space2d.phys.dynamics.joints.DistanceJoint) MouseJoint(spacegraph.space2d.phys.dynamics.joints.MouseJoint) Joint(spacegraph.space2d.phys.dynamics.joints.Joint) Tuple2f(spacegraph.util.math.Tuple2f) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) Vec2(spacegraph.space2d.phys.common.Vec2) MyList(spacegraph.space2d.phys.fracture.util.MyList) PolygonFixture(spacegraph.space2d.phys.fracture.PolygonFixture) PolygonFixture(spacegraph.space2d.phys.fracture.PolygonFixture)

Aggregations

CircleShape (spacegraph.space2d.phys.collision.shapes.CircleShape)2 EdgeShape (spacegraph.space2d.phys.collision.shapes.EdgeShape)2 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)2 Shape (spacegraph.space2d.phys.collision.shapes.Shape)2 PolygonFixture (spacegraph.space2d.phys.fracture.PolygonFixture)2 Tuple2f (spacegraph.util.math.Tuple2f)2 Consumer (java.util.function.Consumer)1 Vec2 (spacegraph.space2d.phys.common.Vec2)1 DistanceJoint (spacegraph.space2d.phys.dynamics.joints.DistanceJoint)1 Joint (spacegraph.space2d.phys.dynamics.joints.Joint)1 MouseJoint (spacegraph.space2d.phys.dynamics.joints.MouseJoint)1 MyList (spacegraph.space2d.phys.fracture.util.MyList)1 spacegraph.util.math.v2 (spacegraph.util.math.v2)1