Search in sources :

Example 6 with CircleShape

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

the class Bodies method circle.

public static Shape circle(float radius) {
    CircleShape gBox = new CircleShape();
    gBox.setRadius(radius);
    return gBox;
}
Also used : CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape)

Example 7 with CircleShape

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

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

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

the class ParticlesTest method init.

@Override
public void init(Dynamics2D m_world) {
    // first one
    Body2D ground = m_world.bodies().iterator().next();
    {
        PolygonShape shape = new PolygonShape();
        v2[] vertices = new v2[] { new v2(-40, -10), new v2(40, -10), new v2(40, 0), new v2(-40, 0) };
        shape.set(vertices, 4);
        ground.addFixture(shape, 0.0f);
    }
    {
        PolygonShape shape = new PolygonShape();
        v2[] vertices = { new v2(-40, -1), new v2(-20, -1), new v2(-20, 20), new v2(-40, 30) };
        shape.set(vertices, 4);
        ground.addFixture(shape, 0.0f);
    }
    {
        PolygonShape shape = new PolygonShape();
        v2[] vertices = { new v2(20, -1), new v2(40, -1), new v2(40, 30), new v2(20, 20) };
        shape.set(vertices, 4);
        ground.addFixture(shape, 0.0f);
    }
    m_world.setParticleRadius(0.35f);
    m_world.setParticleDamping(0.2f);
    {
        CircleShape shape = new CircleShape();
        shape.center.set(0, 30);
        shape.radius = 20;
        ParticleGroupDef pd = new ParticleGroupDef();
        pd.flags = ParticleType.b2_waterParticle;
        pd.shape = shape;
        m_world.addParticles(pd);
    }
    {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        Body2D body = m_world.addBody(bd);
        CircleShape shape = new CircleShape();
        shape.center.set(0, 80);
        shape.radius = 5;
        body.addFixture(shape, 0.5f);
    }
}
Also used : ParticleGroupDef(spacegraph.space2d.phys.particle.ParticleGroupDef) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) spacegraph.util.math.v2(spacegraph.util.math.v2) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) Body2D(spacegraph.space2d.phys.dynamics.Body2D)

Example 10 with CircleShape

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

the class TestQueryCallback method launchBomb.

private void launchBomb(Vec2 position, Vec2 velocity) {
    if (bomb != null) {
        world.destroyBody(bomb);
        bomb = null;
    }
    // todo optimize this
    BodyDef bd = new BodyDef();
    bd.type = BodyType.DYNAMIC;
    bd.position.set(position);
    bd.bullet = true;
    bomb = world.createBody(bd);
    bomb.setLinearVelocity(velocity);
    CircleShape circle = new CircleShape();
    circle.m_radius = 0.3f;
    FixtureDef fd = new FixtureDef();
    fd.shape = circle;
    fd.density = 20f;
    fd.restitution = 0;
    Vec2 minV = new Vec2(position);
    Vec2 maxV = new Vec2(position);
    minV.subLocal(new Vec2(.3f, .3f));
    maxV.addLocal(new Vec2(.3f, .3f));
    aabb.lowerBound.set(minV);
    aabb.upperBound.set(maxV);
    bomb.createFixture(fd);
}
Also used : CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) Vec2(spacegraph.space2d.phys.common.Vec2)

Aggregations

CircleShape (spacegraph.space2d.phys.collision.shapes.CircleShape)11 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)7 spacegraph.util.math.v2 (spacegraph.util.math.v2)7 EdgeShape (spacegraph.space2d.phys.collision.shapes.EdgeShape)3 Shape (spacegraph.space2d.phys.collision.shapes.Shape)3 Tuple2f (spacegraph.util.math.Tuple2f)3 FasterList (jcog.list.FasterList)2 Vec2 (spacegraph.space2d.phys.common.Vec2)2 Body2D (spacegraph.space2d.phys.dynamics.Body2D)2 BodyDef (spacegraph.space2d.phys.dynamics.BodyDef)2 RevoluteJoint (spacegraph.space2d.phys.dynamics.joints.RevoluteJoint)2 RevoluteJointDef (spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef)2 PolygonFixture (spacegraph.space2d.phys.fracture.PolygonFixture)2 MyList (spacegraph.space2d.phys.fracture.util.MyList)2 ParticleGroupDef (spacegraph.space2d.phys.particle.ParticleGroupDef)2 Random (java.util.Random)1 Consumer (java.util.function.Consumer)1 Loop (jcog.exe.Loop)1 HaiQae (jcog.learn.ql.HaiQae)1 FloatRange (jcog.math.FloatRange)1