Search in sources :

Example 1 with DistanceJointDef

use of spacegraph.space2d.phys.dynamics.joints.DistanceJointDef in project narchy by automenta.

the class TheoJansenTest method createLeg.

void createLeg(float s, v2 wheelAnchor) {
    v2 p1 = new v2(5.4f * s, -6.1f);
    v2 p2 = new v2(7.2f * s, -1.2f);
    v2 p3 = new v2(4.3f * s, -1.9f);
    v2 p4 = new v2(3.1f * s, 0.8f);
    v2 p5 = new v2(6.0f * s, 1.5f);
    v2 p6 = new v2(2.5f * s, 3.7f);
    FixtureDef fd1 = new FixtureDef();
    FixtureDef fd2 = new FixtureDef();
    fd1.filter.groupIndex = -1;
    fd2.filter.groupIndex = -1;
    fd1.density = 1.0f;
    fd2.density = 1.0f;
    PolygonShape poly1 = new PolygonShape();
    PolygonShape poly2 = new PolygonShape();
    if (s > 0.0f) {
        v2[] vertices = new v2[3];
        vertices[0] = p1;
        vertices[1] = p2;
        vertices[2] = p3;
        poly1.set(vertices, 3);
        vertices[0] = new v2();
        vertices[1] = p5.sub(p4);
        vertices[2] = p6.sub(p4);
        poly2.set(vertices, 3);
    } else {
        v2[] vertices = new v2[3];
        vertices[0] = p1;
        vertices[1] = p3;
        vertices[2] = p2;
        poly1.set(vertices, 3);
        vertices[0] = new v2();
        vertices[1] = p6.sub(p4);
        vertices[2] = p5.sub(p4);
        poly2.set(vertices, 3);
    }
    fd1.shape = poly1;
    fd2.shape = poly2;
    BodyDef bd1 = new BodyDef(), bd2 = new BodyDef();
    bd1.type = BodyType.DYNAMIC;
    bd2.type = BodyType.DYNAMIC;
    bd1.position = m_offset;
    bd2.position = p4.add(m_offset);
    bd1.angularDamping = 10.0f;
    bd2.angularDamping = 10.0f;
    Body2D body1 = w.addBody(bd1);
    Body2D body2 = w.addBody(bd2);
    body1.addFixture(fd1);
    body2.addFixture(fd2);
    DistanceJointDef djd = new DistanceJointDef();
    // Using a soft distance constraint can reduce some jitter.
    // It also makes the structure seem a bit more fluid by
    // acting like a suspension system.
    djd.dampingRatio = 0.5f;
    djd.frequencyHz = 10.0f;
    djd.initialize(body1, body2, p2.add(m_offset), p5.add(m_offset));
    w.addJoint(djd);
    djd.initialize(body1, body2, p3.add(m_offset), p4.add(m_offset));
    w.addJoint(djd);
    djd.initialize(body1, m_wheel, p3.add(m_offset), wheelAnchor.add(m_offset));
    w.addJoint(djd);
    djd.initialize(body2, m_wheel, p6.add(m_offset), wheelAnchor.add(m_offset));
    w.addJoint(djd);
    RevoluteJointDef rjd = new RevoluteJointDef();
    rjd.initialize(body2, m_chassis, p4.add(m_offset));
    w.addJoint(rjd);
}
Also used : PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) DistanceJointDef(spacegraph.space2d.phys.dynamics.joints.DistanceJointDef) spacegraph.util.math.v2(spacegraph.util.math.v2) RevoluteJointDef(spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef)

Example 2 with DistanceJointDef

use of spacegraph.space2d.phys.dynamics.joints.DistanceJointDef in project narchy by automenta.

the class TheoJansen method createLeg.

void createLeg(float s, v2 wheelAnchor) {
    v2 p1 = new v2(5.4f * s, -6.1f * abs(s));
    v2 p2 = new v2(7.2f * s, -1.2f * abs(s));
    v2 p3 = new v2(4.3f * s, -1.9f * abs(s));
    v2 p4 = new v2(3.1f * s, 0.8f * abs(s));
    v2 p5 = new v2(6.0f * s, 1.5f * abs(s));
    v2 p6 = new v2(2.5f * s, 3.7f * abs(s));
    FixtureDef fd1 = new FixtureDef();
    FixtureDef fd2 = new FixtureDef();
    fd1.filter.groupIndex = -1;
    fd2.filter.groupIndex = -1;
    fd1.density = 1.0f;
    fd2.density = 1.0f;
    PolygonShape poly1 = new PolygonShape();
    PolygonShape poly2 = new PolygonShape();
    if (s > 0.0f) {
        v2[] vertices = new v2[3];
        vertices[0] = p1;
        vertices[1] = p2;
        vertices[2] = p3;
        poly1.set(vertices, 3);
        vertices[0] = new v2();
        vertices[1] = p5.sub(p4);
        vertices[2] = p6.sub(p4);
        poly2.set(vertices, 3);
    } else {
        v2[] vertices = new v2[3];
        vertices[0] = p1;
        vertices[1] = p3;
        vertices[2] = p2;
        poly1.set(vertices, 3);
        vertices[0] = new v2();
        vertices[1] = p6.sub(p4);
        vertices[2] = p5.sub(p4);
        poly2.set(vertices, 3);
    }
    fd1.shape = poly1;
    fd2.shape = poly2;
    BodyDef bd1 = new BodyDef(), bd2 = new BodyDef();
    bd1.type = BodyType.DYNAMIC;
    bd2.type = BodyType.DYNAMIC;
    bd1.position = center;
    bd2.position = p4.add(center);
    bd1.angularDamping = 10.0f;
    bd2.angularDamping = 10.0f;
    Body2D body1 = world.addBody(bd1);
    Body2D body2 = world.addBody(bd2);
    body1.addFixture(fd1);
    body2.addFixture(fd2);
    DistanceJointDef djd = new DistanceJointDef();
    // Using a soft distance constraint can reduce some jitter.
    // It also makes the structure seem a bit more fluid by
    // acting like a suspension system.
    djd.dampingRatio = 0.5f;
    djd.frequencyHz = 10.0f;
    djd.initialize(body1, body2, p2.add(center), p5.add(center));
    world.addJoint(djd);
    djd.initialize(body1, body2, p3.add(center), p4.add(center));
    world.addJoint(djd);
    djd.initialize(body1, wheel, p3.add(center), wheelAnchor.add(center));
    world.addJoint(djd);
    djd.initialize(body2, wheel, p6.add(center), wheelAnchor.add(center));
    world.addJoint(djd);
    RevoluteJointDef rjd = new RevoluteJointDef();
    rjd.initialize(body2, chassis, p4.add(center));
    world.addJoint(rjd);
}
Also used : PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) DistanceJointDef(spacegraph.space2d.phys.dynamics.joints.DistanceJointDef) spacegraph.util.math.v2(spacegraph.util.math.v2) RevoluteJointDef(spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef)

Aggregations

PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)2 DistanceJointDef (spacegraph.space2d.phys.dynamics.joints.DistanceJointDef)2 RevoluteJointDef (spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef)2 spacegraph.util.math.v2 (spacegraph.util.math.v2)2