Search in sources :

Example 1 with Color3f

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

the class VisionRay method step.

public void step(boolean feel, boolean drawing) {
    toDraw.clear();
    float conceptPriority;
    float conceptDurability;
    float conceptQuality;
    angleConcept = abstractPolygonBot.nar.memory.concept(angleTerm);
    if (angleConcept != null) {
        conceptPriority = angleConcept.getPriority();
        conceptDurability = angleConcept.getDurability();
        conceptQuality = angleConcept.getQuality();
    // sight.setProbability(Math.max(minVisionInputProbability, Math.min(1.0f, maxVisionInputProbability * conceptPriority)));
    // sight.setProbability(minVisionInputProbability);
    } else {
        conceptPriority = 0;
        conceptDurability = 0;
        conceptQuality = 0;
    }
    abstractPolygonBot.point1 = body.getWorldPoint(point);
    Body hit = null;
    final float distance = getDistance();
    // far enough away
    float minDist = distance * 1.1f;
    float totalDist = 0;
    float dArc = arc / resolution;
    // (float)Math.random() * (-arc/4f);
    float angOffset = 0;
    for (int r = 0; r < resolution; r++) {
        float da = (-arc / 2f) + dArc * r + angOffset;
        final float V = da + angle + body.getAngle();
        abstractPolygonBot.d.set(distance * (float) Math.cos(V), distance * (float) Math.sin(V));
        abstractPolygonBot.point2.set(abstractPolygonBot.point1);
        abstractPolygonBot.point2.addLocal(abstractPolygonBot.d);
        ccallback.init();
        try {
            abstractPolygonBot.getWorld().raycast(ccallback, abstractPolygonBot.point1, abstractPolygonBot.point2);
        } catch (Exception e) {
            System.err.println("Phys2D raycast: " + e + " " + abstractPolygonBot.point1 + " " + abstractPolygonBot.point2);
            e.printStackTrace();
        }
        Vec2 endPoint = null;
        if (ccallback.m_hit) {
            float d = ccallback.m_point.sub(abstractPolygonBot.point1).length() / distance;
            if (drawing) {
                rayColor.set(laserHitColor);
                rayColor.x = Math.min(1.0f, laserUnhitColor.x + 0.75f * (1.0f - d));
                // Vec2 pp = ccallback.m_point.clone();
                // toDraw.add(new Runnable() {
                // @Override public void run() {
                // 
                // getDraw().drawPoint(pp, 5.0f, sparkColor);
                // 
                // }
                // });
                endPoint = ccallback.m_point;
            }
            // pooledHead.set(ccallback.m_normal);
            // pooledHead.mulLocal(.5f).addLocal(ccallback.m_point);
            // draw.drawSegment(ccallback.m_point, pooledHead, normalColor, 0.25f);
            totalDist += d;
            if (d < minDist) {
                hit = ccallback.body;
                minDist = d;
            }
        } else {
            rayColor.set(normalColor);
            totalDist += 1;
            endPoint = abstractPolygonBot.point2;
        }
        if ((drawing) && (endPoint != null)) {
            // final float alpha = rayColor.x *= 0.2f + 0.8f * (senseActivity + conceptPriority)/2f;
            // rayColor.z *= alpha - 0.35f * senseActivity;
            // rayColor.y *= alpha - 0.35f * conceptPriority;
            rayColor.x = conceptPriority;
            rayColor.y = conceptDurability;
            rayColor.z = conceptQuality;
            float alpha = Math.min((0.4f * conceptPriority * conceptDurability * conceptQuality) + 0.1f, 1f);
            rayColor.x = Math.min(rayColor.x * 0.9f + 0.1f, 1f);
            rayColor.y = Math.min(rayColor.y * 0.9f + 0.1f, 1f);
            rayColor.z = Math.min(rayColor.z * 0.9f + 0.1f, 1f);
            rayColor.x = Math.max(rayColor.x, 0f);
            rayColor.y = Math.max(rayColor.y, 0f);
            rayColor.z = Math.max(rayColor.z, 0f);
            final Vec2 finalEndPoint = endPoint.clone();
            Color3f rc = new Color3f(rayColor.x, rayColor.y, rayColor.z);
            final float thick = 2f;
            toDraw.add(new Runnable() {

                @Override
                public void run() {
                    ((JoglAbstractDraw) abstractPolygonBot.getDraw()).drawSegment(abstractPolygonBot.point1, finalEndPoint, rc.x, rc.y, rc.z, alpha, 1f * thick);
                }
            });
        }
    }
    if (hit != null) {
        float meanDist = totalDist / resolution;
        float percentDiff = (float) Math.sqrt(Math.abs(meanDist - minDist));
        float conf = 0.70f + 0.2f * (1.0f - percentDiff);
        if (conf > 0.9f) {
            conf = 0.9f;
        }
        // perceiveDist(hit, conf, meanDist);
        perceiveDist(hit, conf, meanDist);
    } else {
        perceiveDist(hit, 0.5f, 1.0f);
    }
    updatePerception();
}
Also used : Vec2(spacegraph.space2d.phys.common.Vec2) Color3f(spacegraph.space2d.phys.common.Color3f) Body(spacegraph.space2d.phys.dynamics.Body)

Example 2 with Color3f

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

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

Aggregations

Color3f (spacegraph.space2d.phys.common.Color3f)3 Vec2 (spacegraph.space2d.phys.common.Vec2)3 Body (spacegraph.space2d.phys.dynamics.Body)3 JoglAbstractDraw (nars.rover.physics.gl.JoglAbstractDraw)2 VisionRay (nars.rover.obj.VisionRay)1 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)1 BodyDef (spacegraph.space2d.phys.dynamics.BodyDef)1 Fixture (spacegraph.space2d.phys.dynamics.Fixture)1