Search in sources :

Example 1 with BodyDef

use of spacegraph.space2d.phys.dynamics.BodyDef 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 2 with BodyDef

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

the class CarefulRover method newTorso.

@Override
protected Body newTorso() {
    PolygonShape shape = new PolygonShape();
    Vec2[] vertices = { new Vec2(3.0f, 0.0f), new Vec2(-1.0f, +2.0f), new Vec2(-1.5f, 0), new Vec2(-1.0f, -2.0f) };
    shape.set(vertices, vertices.length);
    // shape.m_centroid.set(bodyDef.position);
    BodyDef bd = new BodyDef();
    bd.linearDamping = (linearDamping);
    bd.angularDamping = (angularDamping);
    bd.type = BodyType.DYNAMIC;
    bd.position.set(0, 0);
    Body torso = getWorld().createBody(bd);
    Fixture f = torso.createFixture(shape, mass);
    f.setRestitution(restitution);
    f.setFriction(friction);
    initSensors(torso);
    initMotors(torso);
    return torso;
}
Also used : PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) Vec2(spacegraph.space2d.phys.common.Vec2) Fixture(spacegraph.space2d.phys.dynamics.Fixture) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) Body(spacegraph.space2d.phys.dynamics.Body)

Example 3 with BodyDef

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

the class ParticleEmitter method getParticleBodyDef.

/**
 * @return Body def for particle, based on current settings
 */
private BodyDef getParticleBodyDef() {
    BodyDef def = new BodyDef();
    def.active = true;
    def.allowSleep = false;
    def.awake = true;
    def.type = BodyType.DYNAMIC;
    def.linearVelocity.set(settings.particleForce.x, settings.particleForce.y);
    def.position.set(settings.offset.x + settings.attached.getLocation().x, settings.offset.y + settings.attached.getLocation().y);
    return def;
}
Also used : BodyDef(spacegraph.space2d.phys.dynamics.BodyDef)

Example 4 with BodyDef

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

the class ReactorWorld method fireDebris.

/**
 * fuel rod components mixed with plutonium ash mutated into a supernatural buckyball atomic compound
 */
protected void fireDebris(float w, float h) {
    BodyDef bd = new BodyDef();
    bd.type = BodyType.DYNAMIC;
    bd.position.set(0.0f, 4.0f);
    PolygonShape box = new PolygonShape();
    box.setAsBox(w, h);
    Body m_body = p.getWorld().createBody(bd);
    m_body.createFixture(box, 1.0f);
    box.setAsBox(0.25f, 0.25f);
    // m_x = RandomFloat(-1.0f, 1.0f);
    float m_x = -0.06530577f;
    bd.position.set(m_x, 10.0f);
    bd.bullet = true;
    Body m_bullet = p.getWorld().createBody(bd);
    m_bullet.createFixture(box, 100.0f);
    m_bullet.setLinearVelocity(new Vec2(0.0f, -50.0f));
    m_body.setTransform(new Vec2(0.0f, 4.0f), 0.0f);
    m_body.setLinearVelocity(new Vec2());
    m_body.setAngularVelocity(0.0f);
    m_x = MathUtils.randomFloat(-1.0f, 1.0f);
    m_bullet.setTransform(new Vec2(m_x, 10.0f), 0.0f);
    m_bullet.setLinearVelocity(new Vec2(0.0f, -50.0f));
    m_bullet.setAngularVelocity(0.0f);
}
Also used : PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) Vec2(spacegraph.space2d.phys.common.Vec2) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) Body(spacegraph.space2d.phys.dynamics.Body)

Example 5 with BodyDef

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

Aggregations

BodyDef (spacegraph.space2d.phys.dynamics.BodyDef)7 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)6 Body (spacegraph.space2d.phys.dynamics.Body)4 Fixture (spacegraph.space2d.phys.dynamics.Fixture)4 Vec2 (spacegraph.space2d.phys.common.Vec2)3 CircleShape (spacegraph.space2d.phys.collision.shapes.CircleShape)2 Body2D (spacegraph.space2d.phys.dynamics.Body2D)2 ParticleGroupDef (spacegraph.space2d.phys.particle.ParticleGroupDef)2 spacegraph.util.math.v2 (spacegraph.util.math.v2)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 VisionRay (nars.rover.obj.VisionRay)1 JoglAbstractDraw (nars.rover.physics.gl.JoglAbstractDraw)1 SpaceGraph (spacegraph.SpaceGraph)1 Gridding (spacegraph.space2d.container.Gridding)1