use of nars.rover.obj.VisionRay in project narchy by automenta.
the class CarefulRover method initMotors.
protected void initMotors(Body torso) {
nar.on(new NullOperator("vision") {
@Override
public List<Task> apply(Operation o) {
final String command = command(o, o.getMemory());
String[] sections = command.split(",");
if (sections.length == 1) {
// switch (sections[0]) {
// case "far":
// dist = 20f;
// break;
// case "near":
// dist = 10f;
// break;
// default:
// return null;
// }
String angle = sections[0];
float dist;
dist = 5 + 20f * o.getTask().getTruth().getExpectation();
VisionRay vr = pixels.get(angle);
if (vr != null)
vr.setDistance(dist);
}
return null;
}
});
System.out.println(pixels);
for (String a : pixels.keySet()) {
randomActions.add("vision(" + a + ")! :!: %1%");
randomActions.add("vision(" + a + ")! :|: %0%");
}
randomActions.add("motor(left)! :|:");
// randomActions.add("motor(left,left)!");
randomActions.add("motor(right)! :|:");
// randomActions.add("motor(right,right)!");
// randomActions.add("motor(forward,forward)!"); //too much actions are not good,
// however i would agree if <motor(forward,forward) --> motor(forward)>.
randomActions.add("motor(forward)! :|:");
// randomActions.add("motor(forward,forward)!");
// randomActions.add("motor(forward)!");
randomActions.add("motor(reverse)! :|:");
randomActions.add("motor(stop)! :|:");
// randomActions.add("motor(random)!");
nar.on(new NullOperator("motor") {
@Override
public List<Task> apply(Operation operation) {
String command = command(operation, operation.getMemory());
if (command.equals("$1")) {
// variable causes random movement
double v = Math.random();
if (v < 0.25f) {
command = "left";
} else if (v < 0.5f) {
command = "right";
} else if (v < 0.75f) {
command = "forward";
} else {
command = "reverse";
}
}
float strength = operation.getTaskExpectation();
float rotSpeed = 15 * strength;
float linSpeed = 1 * strength;
switch(command) {
case "right":
rotateRelative(-rotSpeed);
break;
case "right,right":
rotateRelative(-rotSpeed * 2);
break;
case "left":
rotateRelative(+rotSpeed);
break;
case "left,left":
rotateRelative(+rotSpeed * 2);
break;
case "forward,forward":
thrustRelative(+2 * linSpeed);
break;
case "forward":
thrustRelative(+linSpeed);
break;
case "reverse":
thrustRelative(-linSpeed);
break;
case "stop":
stop();
break;
case "random":
randomAction();
break;
}
return null;
}
});
}
use of nars.rover.obj.VisionRay 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;
}
}
use of nars.rover.obj.VisionRay 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;
}
Aggregations