Search in sources :

Example 1 with RevoluteJointDef

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

the class Snake method attach.

/**
 * attach a body to center of one of the segments
 */
public void attach(Body2D b, int segment) {
    Dynamics2D world = world();
    world.invoke(() -> {
        RevoluteJoint w = (RevoluteJoint) b.W.addJoint(new RevoluteJointDef(bodies.get(segment), b));
        attachments.add(b);
        joints.add(w);
    });
}
Also used : RevoluteJoint(spacegraph.space2d.phys.dynamics.joints.RevoluteJoint) RevoluteJointDef(spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef)

Example 2 with RevoluteJointDef

use of spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef 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 3 with RevoluteJointDef

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

the class TensorGlow method main.

public static void main(String[] args) {
    PhyWall p = SpaceGraph.wall(1200, 1000);
    p.W.setGravity(new v2(0, -2.8f));
    staticBox(p.W, -5, -8, +5, 2f, false, true, true, true);
    for (int j = 0; j < 3; j++) {
        BodyDef bodyDef2 = new BodyDef();
        bodyDef2.type = BodyType.DYNAMIC;
        // otocenie
        bodyDef2.angle = -0.6f;
        // smer pohybu
        bodyDef2.linearVelocity = new v2(0.0f, 0.0f);
        // rotacia (rychlost rotacie)
        bodyDef2.angularVelocity = 0.0f;
        Body2D newBody = p.W.addBody(bodyDef2);
        PolygonShape shape2 = new PolygonShape();
        shape2.setAsBox(0.25f, 0.25f);
        Fixture f = newBody.addFixture(shape2, 1.0f);
        // trenie
        f.friction = 0.5f;
        // odrazivost
        f.restitution = 0.0f;
        f.material = new Uniform();
        f.material.m_rigidity = 1.0f;
    }
    // //ceiling rack
    // addBox(p.W, -1, +0.4f, 0, +0.65f, false, true, true, true);
    // new Pacman(p.W);
    {
        p.W.setContactListener(new Explosives.ExplosionContacts());
        TheoJansen t = new TheoJansen(p.W, 0.35f);
        PhyWall.PhyWindow pw = p.put(new Gridding(0.5f, new Port((float[] v) -> {
            // System.out.println(v);
            t.motorJoint.setMotorSpeed(v[0] * 2 - v[1] * 2);
            t.motorJoint.setMaxMotorTorque(v[2]);
            t.motorJoint.enableLimit(true);
            t.motorJoint.setLimits((float) (-v[3] * Math.PI), (float) (+v[4] * Math.PI));
            if (v[5] > 0.5f) {
                t.gun.fire();
            }
            t.turretJoint.setLimits((float) (+Math.PI / 2 + v[6] * Math.PI - 0.1f), (float) (+Math.PI / 2 + v[6] * Math.PI + 0.1f));
        })), 0.8f, 0.4f);
        p.W.addJoint(new RevoluteJoint(p.W, new RevoluteJointDef(pw.body, t.chassis)));
    }
    {
        p.W.setParticleRadius(0.05f);
        p.W.setParticleDamping(0.1f);
        CircleShape shape = new CircleShape();
        shape.center.set(0, 10);
        shape.radius = 2f;
        ParticleGroupDef pd = new ParticleGroupDef();
        pd.flags = ParticleType.b2_waterParticle;
        // b2_viscousParticle;
        // b2_elasticParticle;
        // b2_springParticle;
        // b2_powderParticle;
        pd.color = new ParticleColor(0.7f, 0.1f, 0.1f, 0.8f);
        pd.shape = shape;
        p.W.addParticles(pd);
    }
    HaiQae q = new HaiQae(8, 2);
    float[] in = new float[q.ae.inputs()];
    final Tensor randomVector = Tensor.randomVectorGauss(in.length, 0, 1, rng);
    final FloatRange lerpRate = new FloatRange(0.01f, 0, 1f);
    final TensorLERP lerpVector = new TensorLERP(randomVector, lerpRate);
    PhyWall.PhyWindow w = p.put(new Gridding(0.25f, new AutoUpdateMatrixView(lerpVector.data), new LabeledPane("lerp", new XYSlider().on((x, y) -> {
        lerpRate.set(x);
    })), new LabeledPane("out", new Port((x) -> {
    }) {

        @Override
        public void prePaint(int dtMS) {
            super.prePaint(dtMS);
            out(lerpVector.data);
        }
    })), 0.5f, 0.5f);
    p.put(new TogglePort(), 0.25f, 0.25f);
    PhyWall.PhyWindow qw = p.put(new Gridding(new Label("HaiQ"), new AutoSurface<>(q), new LabeledPane("input", new Port((float[] i) -> {
        System.arraycopy(i, 0, in, 0, i.length);
    })), new Gridding(VERTICAL, new AutoUpdateMatrixView(in), new AutoUpdateMatrixView(q.ae.x), new AutoUpdateMatrixView(q.ae.W), new AutoUpdateMatrixView(q.ae.y)), new Gridding(VERTICAL, new AutoUpdateMatrixView(q.q), new AutoUpdateMatrixView(q.et))), 1, 1);
    Loop.of(() -> {
        lerpVector.update();
        q.act((((float) Math.random()) - 0.5f) * 2, in);
    }).runFPS(25);
}
Also used : Fixture(spacegraph.space2d.phys.dynamics.Fixture) ParticleGroupDef(spacegraph.space2d.phys.particle.ParticleGroupDef) Explosives(spacegraph.space2d.phys.explosive.Explosives) Tensor(jcog.math.tensor.Tensor) HaiQae(jcog.learn.ql.HaiQae) Random(java.util.Random) RevoluteJoint(spacegraph.space2d.phys.dynamics.joints.RevoluteJoint) Port(spacegraph.space2d.widget.windo.Port) ParticleColor(spacegraph.space2d.phys.particle.ParticleColor) AutoSurface(spacegraph.space2d.widget.meta.AutoSurface) AutoUpdateMatrixView(spacegraph.space2d.widget.meter.AutoUpdateMatrixView) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) PhyWall(spacegraph.space2d.widget.windo.PhyWall) Gridding(spacegraph.space2d.container.Gridding) BodyType(spacegraph.space2d.phys.dynamics.BodyType) ParticleType(spacegraph.space2d.phys.particle.ParticleType) RevoluteJointDef(spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef) XoRoShiRo128PlusRandom(jcog.math.random.XoRoShiRo128PlusRandom) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) LabeledPane(spacegraph.space2d.widget.text.LabeledPane) Loop(jcog.exe.Loop) TogglePort(spacegraph.space2d.widget.windo.TogglePort) XYSlider(spacegraph.space2d.widget.slider.XYSlider) Dynamics2D.staticBox(spacegraph.space2d.phys.dynamics.Dynamics2D.staticBox) Uniform(spacegraph.space2d.phys.fracture.materials.Uniform) FloatRange(jcog.math.FloatRange) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) spacegraph.util.math.v2(spacegraph.util.math.v2) TensorLERP(jcog.math.tensor.TensorLERP) VERTICAL(spacegraph.space2d.container.Gridding.VERTICAL) SpaceGraph(spacegraph.SpaceGraph) Label(spacegraph.space2d.widget.text.Label) Body2D(spacegraph.space2d.phys.dynamics.Body2D) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) Port(spacegraph.space2d.widget.windo.Port) TogglePort(spacegraph.space2d.widget.windo.TogglePort) Label(spacegraph.space2d.widget.text.Label) Uniform(spacegraph.space2d.phys.fracture.materials.Uniform) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) TogglePort(spacegraph.space2d.widget.windo.TogglePort) FloatRange(jcog.math.FloatRange) AutoSurface(spacegraph.space2d.widget.meta.AutoSurface) LabeledPane(spacegraph.space2d.widget.text.LabeledPane) Fixture(spacegraph.space2d.phys.dynamics.Fixture) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) ParticleGroupDef(spacegraph.space2d.phys.particle.ParticleGroupDef) HaiQae(jcog.learn.ql.HaiQae) Tensor(jcog.math.tensor.Tensor) RevoluteJoint(spacegraph.space2d.phys.dynamics.joints.RevoluteJoint) TensorLERP(jcog.math.tensor.TensorLERP) RevoluteJoint(spacegraph.space2d.phys.dynamics.joints.RevoluteJoint) RevoluteJointDef(spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef) Gridding(spacegraph.space2d.container.Gridding) AutoUpdateMatrixView(spacegraph.space2d.widget.meter.AutoUpdateMatrixView) XYSlider(spacegraph.space2d.widget.slider.XYSlider) PhyWall(spacegraph.space2d.widget.windo.PhyWall) ParticleColor(spacegraph.space2d.phys.particle.ParticleColor) spacegraph.util.math.v2(spacegraph.util.math.v2) Body2D(spacegraph.space2d.phys.dynamics.Body2D)

Example 4 with RevoluteJointDef

use of spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef 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)

Example 5 with RevoluteJointDef

use of spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef 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)

Aggregations

RevoluteJointDef (spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef)6 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)4 spacegraph.util.math.v2 (spacegraph.util.math.v2)4 RevoluteJoint (spacegraph.space2d.phys.dynamics.joints.RevoluteJoint)3 CircleShape (spacegraph.space2d.phys.collision.shapes.CircleShape)2 DistanceJointDef (spacegraph.space2d.phys.dynamics.joints.DistanceJointDef)2 Random (java.util.Random)1 Loop (jcog.exe.Loop)1 HaiQae (jcog.learn.ql.HaiQae)1 FloatRange (jcog.math.FloatRange)1 XoRoShiRo128PlusRandom (jcog.math.random.XoRoShiRo128PlusRandom)1 Tensor (jcog.math.tensor.Tensor)1 TensorLERP (jcog.math.tensor.TensorLERP)1 SpaceGraph (spacegraph.SpaceGraph)1 Gridding (spacegraph.space2d.container.Gridding)1 VERTICAL (spacegraph.space2d.container.Gridding.VERTICAL)1 EdgeShape (spacegraph.space2d.phys.collision.shapes.EdgeShape)1 Vec2 (spacegraph.space2d.phys.common.Vec2)1 Body (spacegraph.space2d.phys.dynamics.Body)1 Body2D (spacegraph.space2d.phys.dynamics.Body2D)1