Search in sources :

Example 1 with EdgeShape

use of spacegraph.space2d.phys.collision.shapes.EdgeShape 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 EdgeShape

use of spacegraph.space2d.phys.collision.shapes.EdgeShape in project narchy by automenta.

the class TheoJansenTest method init.

// @Override
// public Long getTag(Body2D argBody) {
// if (argBody2D == m_chassis) {
// return CHASSIS_TAG;
// } else if (argBody2D == m_wheel) {
// return WHEEL_TAG;
// }
// return null;
// }
// 
// @Override
// public Long getTag(Joint argJoint) {
// if (argJoint == m_motorJoint) {
// return MOTOR_TAG;
// }
// return null;
// }
// @Override
// public void processBody(Body2D argBody, Long argTag) {
// if (argTag == CHASSIS_TAG) {
// m_chassis = argBody;
// } else if (argTag == WHEEL_TAG) {
// m_wheel = argBody;
// }
// }
// 
// @Override
// public void processJoint(Joint argJoint, Long argTag) {
// if (argTag == MOTOR_TAG) {
// m_motorJoint = (RevoluteJoint) argJoint;
// m_motorOn = m_motorJoint.isMotorEnabled();
// }
// }
@Override
public void init(Dynamics2D w) {
    this.w = w;
    m_offset.set(0.0f, 8.0f);
    m_motorSpeed = 2.0f;
    m_motorOn = true;
    v2 pivot = new v2(0.0f, 0.8f);
    // Ground
    {
        BodyDef bd = new BodyDef();
        Body2D ground = w.addBody(bd);
        EdgeShape shape = new EdgeShape();
        shape.set(new v2(-50.0f, 0.0f), new v2(50.0f, 0.0f));
        ground.addFixture(shape, 0.0f);
        shape.set(new v2(-50.0f, 0.0f), new v2(-50.0f, 10.0f));
        ground.addFixture(shape, 0.0f);
        shape.set(new v2(50.0f, 0.0f), new v2(50.0f, 10.0f));
        ground.addFixture(shape, 0.0f);
    }
    // Balls
    for (int i = 0; i < 40; ++i) {
        CircleShape shape = new CircleShape();
        shape.radius = 0.25f;
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(-40.0f + 2.0f * i, 0.5f);
        Body2D body = w.addBody(bd);
        body.addFixture(shape, 1.0f);
    }
    // Chassis
    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(2.5f, 1.0f);
        FixtureDef sd = new FixtureDef();
        sd.density = 1.0f;
        sd.shape = shape;
        sd.filter.groupIndex = -1;
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(pivot).added(m_offset);
        m_chassis = w.addBody(bd);
        m_chassis.addFixture(sd);
    }
    {
        CircleShape shape = new CircleShape();
        shape.radius = 1.6f;
        FixtureDef sd = new FixtureDef();
        sd.density = 1.0f;
        sd.shape = shape;
        sd.filter.groupIndex = -1;
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(pivot).added(m_offset);
        m_wheel = w.addBody(bd);
        m_wheel.addFixture(sd);
    }
    {
        RevoluteJointDef jd = new RevoluteJointDef();
        jd.initialize(m_wheel, m_chassis, pivot.add(m_offset));
        jd.collideConnected = false;
        jd.motorSpeed = m_motorSpeed;
        jd.maxMotorTorque = 400.0f;
        jd.enableMotor = m_motorOn;
        m_motorJoint = (RevoluteJoint) w.addJoint(jd);
    }
    v2 wheelAnchor;
    wheelAnchor = pivot.add(new v2(0.0f, -0.8f));
    createLeg(-1.0f, wheelAnchor);
    createLeg(1.0f, wheelAnchor);
    m_wheel.setTransform(m_wheel.getPosition(), 120.0f * MathUtils.PI / 180.0f);
    createLeg(-1.0f, wheelAnchor);
    createLeg(1.0f, wheelAnchor);
    m_wheel.setTransform(m_wheel.getPosition(), -120.0f * MathUtils.PI / 180.0f);
    createLeg(-1.0f, wheelAnchor);
    createLeg(1.0f, wheelAnchor);
}
Also used : EdgeShape(spacegraph.space2d.phys.collision.shapes.EdgeShape) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) RevoluteJoint(spacegraph.space2d.phys.dynamics.joints.RevoluteJoint) spacegraph.util.math.v2(spacegraph.util.math.v2) RevoluteJoint(spacegraph.space2d.phys.dynamics.joints.RevoluteJoint) RevoluteJointDef(spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef)

Example 3 with EdgeShape

use of spacegraph.space2d.phys.collision.shapes.EdgeShape 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)

Example 4 with EdgeShape

use of spacegraph.space2d.phys.collision.shapes.EdgeShape in project narchy by automenta.

the class Bodies method edge.

public static Shape edge(Vec2 vertex1, Vec2 vertex2) {
    EdgeShape e = new EdgeShape();
    e.set(vertex1, vertex2);
    return e;
}
Also used : EdgeShape(spacegraph.space2d.phys.collision.shapes.EdgeShape)

Aggregations

EdgeShape (spacegraph.space2d.phys.collision.shapes.EdgeShape)4 CircleShape (spacegraph.space2d.phys.collision.shapes.CircleShape)3 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)3 Shape (spacegraph.space2d.phys.collision.shapes.Shape)2 PolygonFixture (spacegraph.space2d.phys.fracture.PolygonFixture)2 Tuple2f (spacegraph.util.math.Tuple2f)2 spacegraph.util.math.v2 (spacegraph.util.math.v2)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 RevoluteJoint (spacegraph.space2d.phys.dynamics.joints.RevoluteJoint)1 RevoluteJointDef (spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef)1 MyList (spacegraph.space2d.phys.fracture.util.MyList)1