Search in sources :

Example 11 with Body

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

the class Rover 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.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);
    // for (int i = -pixels / 2; i <= pixels / 2; i++) {
    for (int i = 0; i < retinaPixels; i++) {
        final int ii = i;
        final float angle = /*MathUtils.PI / 2f*/
        aStep * i;
        final boolean eats = ((angle < mouthArc / 2f) || (angle > (Math.PI * 2f) - mouthArc / 2f));
        // System.out.println(i + " " + angle + " " + eats);
        VisionRay v = new VisionRay(this, torso, /*eats ?*/
        mouthPoint, /*: new Vec2(0,0)*/
        angle, aStep, retinaRaysPerPixel, L, distanceResolution) {

            @Override
            public void onTouch(Body touched, float di) {
                if (touched == null)
                    return;
                if (touched.getUserData() instanceof Sim.Edible) {
                    if (eats) {
                        if (di <= biteDistanceThreshold)
                            eat(touched);
                    /*} else if (di <= tasteDistanceThreshold) {
                                //taste(touched, di );
                            }*/
                    }
                }
            }
        };
        v.sparkColor = new Color3f(0.5f, 0.4f, 0.4f);
        v.normalColor = new Color3f(0.4f, 0.4f, 0.4f);
        ((JoglAbstractDraw) draw).addLayer(v);
        senses.add(v);
    }
    return torso;
}
Also used : PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) VisionRay(nars.rover.obj.VisionRay) Color3f(spacegraph.space2d.phys.common.Color3f) Vec2(spacegraph.space2d.phys.common.Vec2) Fixture(spacegraph.space2d.phys.dynamics.Fixture) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) Body(spacegraph.space2d.phys.dynamics.Body) JoglAbstractDraw(nars.rover.physics.gl.JoglAbstractDraw)

Example 12 with Body

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

the class Spider method addArm.

public void addArm(Body base, float x, float y, float angle, int armSegments, float armLength, float armWidth) {
    Body[] arm = new Body[armSegments];
    Body prev = base;
    double dr = getArmLength(armLength, 0) / 2.0;
    for (int i = 0; i < armSegments; i++) {
        float al = getArmLength(armLength, i);
        float aw = getArmWidth(armWidth, i);
        float ax = (float) (x + Math.cos(angle) * dr);
        float ay = (float) (y + Math.sin(angle) * dr);
        final Body b = arm[i] = sim.create(new Vec2(ax, ay), sim.rectangle(new Vec2(al, aw), new Vec2(), angle), // , angle, 1.0f, m);
        BodyType.DYNAMIC);
        float rx = (float) (x + Math.cos(angle) * (dr - al / 2.0f));
        float ry = (float) (y + Math.sin(angle) * (dr - al / 2.0f));
        RevoluteJointDef rv = new RevoluteJointDef();
        rv.initialize(arm[i], prev, new Vec2(rx, ry));
        rv.enableLimit = true;
        rv.enableMotor = true;
        rv.upperAngle = 1;
        rv.lowerAngle = -1;
        // rv.referenceAngle = angle;
        sim.getWorld().createJoint(rv);
        // new RevoluteJointByIndexVote(brain, j, -servoRange, servoRange, servoSteps);
        // 
        // new QuantizedScalarInput(brain, 4) {
        // @Override
        // public float getValue() {
        // return j.getJointAngle() / (float) (Math.PI * 2.0f);
        // }
        // };
        // new QuantizedScalarInput(brain, velocityLevels) {
        // @Override
        // public float getValue() {
        // final float zl = b.getLinearVelocity().len2();
        // if (zl == 0) return 0;
        // float xx = b.getLinearVelocity().x;
        // xx *= xx;
        // return xx / zl;
        // }
        // };
        // new QuantizedScalarInput(brain, velocityLevels) {
        // @Override
        // public float getValue() {
        // final float zl = b.getLinearVelocity().len2();
        // if (zl == 0) return 0;
        // float yy = b.getLinearVelocity().y;
        // yy *= yy;
        // return yy / zl;
        // }
        // };
        // 
        // new QuantizedScalarInput(brain, orientationSteps) {
        // @Override
        // public float getValue() {
        // return b.getAngle() / (float) (2.0 * Math.PI);
        // }
        // };
        // new QuantizedScalarInput(brain, velocityLevels) {
        // @Override
        // public float getValue() {
        // return b.getAngularVelocity() / (float) (2.0 * Math.PI);
        // }
        // };
        // DEPRECATED
        // brain.addInput(new RevoluteJointAngle(j));
        // brain.addInput(new VelocityAxis(b, true));
        // brain.addInput(new VelocityAxis(b, false));
        // Orientation.newVector(brain, b, orientationSteps);
        // brain.addInput(new VelocityAngular(b));
        // brain.addOutput(new ColorBodyTowards(b, color, 0.95f));
        // brain.addOutput(new ColorBodyTowards(b, new Color(color.r * 0.5f, color.g * 0.5f, color.b * 0.5f, color.a * 0.25f), 0.95f));
        // int n = numRetinasPerSegment;
        // for (float z = 0; z < n; z++) {
        // 
        // float a = z * (float) (Math.PI * 2.0 / ((float) n));
        // retinas.add(new Retina(brain, b, new Vector2(0, 0), a, (float) initialVisionDistance, retinaLevels));
        // }
        // TODO Retina.newVector(...)
        // y -= al*0.9f;
        dr += al * 0.9f;
        prev = arm[i];
    }
}
Also used : Vec2(spacegraph.space2d.phys.common.Vec2) Body(spacegraph.space2d.phys.dynamics.Body) RevoluteJointDef(spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef)

Example 13 with Body

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

the class Turret method fireBullet.

public void fireBullet() /*float ttl*/
{
    // final float now = sim.getTime();
    // Iterator<Body> ib = bullets.iterator();
    // while (ib.hasNext()) {
    // Body b = ib.next();
    // ((BulletData)b.getUserData()).diesAt
    // 
    // }
    final float speed = 100f;
    if (bullets.size() >= maxBullets) {
        sim.remove(bullets.removeFirst());
    }
    Vec2 start = torso.getWorldPoint(new Vec2(6.5f, 0));
    Body b = sim.create(start, Bodies.rectangle(0.4f, 0.6f), BodyType.DYNAMIC);
    b.m_mass = 0.05f;
    float angle = torso.getAngle();
    Vec2 rayDir = new Vec2((float) Math.cos(angle), (float) Math.sin(angle));
    rayDir.mulLocal(speed);
    // float diesAt = now + ttl;
    b.setUserData(new BulletData(b, 0));
    bullets.add(b);
    b.applyForce(rayDir, new Vec2(0, 0));
// float angle = (i / (float)numRays) * 360 * DEGTORAD;
// b2Vec2 rayDir( sinf(angle), cosf(angle) );
// 
// b2BodyDef bd;
// bd.type = b2_dynamicBody;
// bd.fixedRotation = true; // rotation not necessary
// bd.bullet = true; // prevent tunneling at high speed
// bd.linearDamping = 10; // drag due to moving through air
// bd.gravityScale = 0; // ignore gravity
// bd.position = center; // start at blast center
// bd.linearVelocity = blastPower * rayDir;
// b2Body* body = m_world->CreateBody( &bd );
// 
// b2CircleShape circleShape;
// circleShape.m_radius = 0.05; // very small
// 
// b2FixtureDef fd;
// fd.shape = &circleShape;
// fd.density = 60 / (float)numRays; // very high - shared across all particles
// fd.friction = 0; // friction not necessary
// fd.restitution = 0.99f; // high restitution to reflect off obstacles
// fd.filter.groupIndex = -1; // particles should not collide with each other
// body->CreateFixture( &fd );
}
Also used : Vec2(spacegraph.space2d.phys.common.Vec2) Body(spacegraph.space2d.phys.dynamics.Body)

Example 14 with Body

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

the class Turret method getMaterial.

@Override
public RoboticMaterial getMaterial() {
    return new RoboticMaterial(this) {

        @Override
        public void before(Body b, JoglAbstractDraw d, float time) {
            super.before(b, d, time);
            if (!explosions.isEmpty()) {
                Iterator<BulletData> ii = explosions.iterator();
                while (ii.hasNext()) {
                    BulletData bd = ii.next();
                    if (bd.explosionTTL-- <= 0)
                        ii.remove();
                    d.drawSolidCircle(bd.getCenter(), bd.explosionTTL / 8 + rng.nextFloat() * 4, new Vec2(), new Color3f(1 - rng.nextFloat() / 3f, 0.8f - rng.nextFloat() / 3f, 0f));
                }
            }
        }
    };
}
Also used : Vec2(spacegraph.space2d.phys.common.Vec2) Color3f(spacegraph.space2d.phys.common.Color3f) Body(spacegraph.space2d.phys.dynamics.Body) JoglAbstractDraw(nars.rover.physics.gl.JoglAbstractDraw)

Example 15 with Body

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

the class RayCastClosestCallback method reportFixture.

public float reportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction) {
    Body body = fixture.getBody();
    // Object userData = body.getUserData();
    this.body = body;
    // if (userData != null) {
    // int index = (Integer) userData;
    // if (index == 0) {
    // // filter
    // return -1f;
    // }
    // }
    m_hit = true;
    m_point = point;
    m_normal = normal;
    return fraction;
}
Also used : Body(spacegraph.space2d.phys.dynamics.Body)

Aggregations

Body (spacegraph.space2d.phys.dynamics.Body)17 Vec2 (spacegraph.space2d.phys.common.Vec2)9 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)4 BodyDef (spacegraph.space2d.phys.dynamics.BodyDef)4 Fixture (spacegraph.space2d.phys.dynamics.Fixture)4 Color3f (spacegraph.space2d.phys.common.Color3f)3 JoglAbstractDraw (nars.rover.physics.gl.JoglAbstractDraw)2 Joint (spacegraph.space2d.phys.dynamics.joints.Joint)2 PulleyJoint (spacegraph.space2d.phys.dynamics.joints.PulleyJoint)2 AffineTransform (java.awt.geom.AffineTransform)1 VisionRay (nars.rover.obj.VisionRay)1 PhysicsCamera (nars.rover.physics.PhysicsCamera)1 SwingDraw (nars.rover.physics.j2d.SwingDraw)1 QueryCallback (spacegraph.space2d.phys.callbacks.QueryCallback)1 AABB (spacegraph.space2d.phys.collision.AABB)1 RevoluteJointDef (spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef)1