Search in sources :

Example 6 with Body

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

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

the class Turret method step.

@Override
public void step(int i) {
    super.step(i);
    for (Body b : removedBullets) {
        bullets.remove(b);
        sim.remove(b);
        final BulletData bd = (BulletData) b.getUserData();
        bd.explode();
        explosions.add(bd);
    }
    removedBullets.clear();
    if (Math.random() < fireProbability) {
        fireBullet();
    }
}
Also used : Body(spacegraph.space2d.phys.dynamics.Body)

Example 8 with Body

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

the class Explosion method explodeBlastRadius.

public static void explodeBlastRadius(World world, Vec2 center, float blastRadius, float blastPower) {
    final float m_blastRadiusSq = blastRadius * blastRadius;
    // find all bodies with fixtures in blast radius AABB
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public boolean reportFixture(Fixture fixture) {
            Body body = fixture.getBody();
            Vec2 bodyCom = body.getWorldCenter();
            // ignore bodies outside the blast range
            if ((bodyCom.sub(center)).lengthSquared() < m_blastRadiusSq) {
                applyBlastImpulse(body, center, bodyCom, blastPower);
                return true;
            }
            return false;
        }
    };
    world.queryAABB(queryCallback, new AABB(center.sub(new Vec2(blastRadius, blastRadius)), center.add(new Vec2(blastRadius, blastRadius))));
}
Also used : Vec2(spacegraph.space2d.phys.common.Vec2) Fixture(spacegraph.space2d.phys.dynamics.Fixture) Body(spacegraph.space2d.phys.dynamics.Body) QueryCallback(spacegraph.space2d.phys.callbacks.QueryCallback) AABB(spacegraph.space2d.phys.collision.AABB)

Example 9 with Body

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

the class RoverWorld method addWall.

public Body addWall(float x, float y, float w, float h, float a) {
    Body b = addBlock(x, y, w, h, a, 0);
    b.setUserData(Material.wall);
    return b;
}
Also used : Body(spacegraph.space2d.phys.dynamics.Body)

Example 10 with Body

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

the class RoverWorld method addFood.

public void addFood(float w, float h, float minSize, float maxSize, float mass, Material m) {
    float x = (float) Math.random() * w - w / 2f;
    float y = (float) Math.random() * h - h / 2f;
    float bw = (float) (minSize + Math.random() * (maxSize - minSize));
    float bh = (float) (minSize + Math.random() * (maxSize - minSize));
    float a = 0;
    Body b = addBlock(x * 2.0f, y * 2.0f, bw, bh, a, mass);
    b.applyAngularImpulse((float) Math.random());
    b.setUserData(m);
}
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