Search in sources :

Example 21 with Vec2

use of spacegraph.space2d.phys.common.Vec2 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 22 with Vec2

use of spacegraph.space2d.phys.common.Vec2 in project narchy by automenta.

the class CarefulRover method initSensors.

protected void initSensors(Body torso) {
    Vec2 center = new Vec2(0, 0);
    float da = (float) (Math.PI * 2 / numPixels);
    float a = 0;
    for (int i = 0; i < numPixels; i++) {
        VisionRay v = new VisionRay(this, torso, /*eats ?*/
        center, /*: new Vec2(0,0)*/
        a, da, 3, 10, 8) {

            @Override
            protected float getDistance() {
                return distance * visionDistanceFactor;
            }
        };
        pixels.put(v.angleTerm, v);
        draw.addLayer(v);
        senses.add(v);
        a += da;
    }
}
Also used : VisionRay(nars.rover.obj.VisionRay) Vec2(spacegraph.space2d.phys.common.Vec2)

Example 23 with Vec2

use of spacegraph.space2d.phys.common.Vec2 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 24 with Vec2

use of spacegraph.space2d.phys.common.Vec2 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 25 with Vec2

use of spacegraph.space2d.phys.common.Vec2 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

Vec2 (spacegraph.space2d.phys.common.Vec2)41 Body (spacegraph.space2d.phys.dynamics.Body)9 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)6 Tuple2f (spacegraph.util.math.Tuple2f)6 MouseJoint (spacegraph.space2d.phys.dynamics.joints.MouseJoint)4 JoglAbstractDraw (nars.rover.physics.gl.JoglAbstractDraw)3 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)3 Color3f (spacegraph.space2d.phys.common.Color3f)3 BodyDef (spacegraph.space2d.phys.dynamics.BodyDef)3 Fixture (spacegraph.space2d.phys.dynamics.Fixture)3 VisionRay (nars.rover.obj.VisionRay)2 Physics2dBody (ptrman.difficultyEnvironment.physics.Physics2dBody)2 CircleShape (spacegraph.space2d.phys.collision.shapes.CircleShape)2 DistanceJoint (spacegraph.space2d.phys.dynamics.joints.DistanceJoint)2 GLCapabilities (com.jogamp.opengl.GLCapabilities)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Material (nars.rover.Material)1 PhysicsModel (nars.rover.PhysicsModel)1 Sim (nars.rover.Sim)1