use of spacegraph.space2d.phys.common.Vec2 in project narchy by automenta.
the class AWTPanelHelper method addHelpAndPanelListeners.
/**
* Adds common help text and listeners for awt-based testbeds.
*/
public static void addHelpAndPanelListeners(Component panel, final TestbedState model, final PhysicsController controller, final int screenDragButton) {
final Vec2 oldDragMouse = new Vec2();
final Vec2 mouse = new Vec2();
List<String> help = Lists.newArrayList();
help.add("Click and drag the left mouse button to move objects.");
help.add("Click and drag the right mouse button to move the view.");
help.add("Shift-Click to aim a bullet, or press space.");
help.add("Scroll to zoom in/out on the mouse position");
help.add("Press '[' or ']' to change tests, and 'r' to restart.");
model.setImplSpecificHelp(help);
panel.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent e) {
int notches = e.getWheelRotation();
PhysicsModel currTest = model.getCurrTest();
if (currTest == null) {
return;
}
ZoomType zoom = notches < 0 ? ZoomType.ZOOM_IN : ZoomType.ZOOM_OUT;
currTest.getCamera().zoomToPoint(mouse, zoom);
}
});
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
if (arg0.getButton() == screenDragButton) {
screenDragButtonDown = false;
} else if (model.getCodedKeys()[KeyEvent.VK_SHIFT] && !mouseJointButtonDown) {
controller.queueMouseUp(new Vec2(arg0.getX(), arg0.getY()), PhysicsModel.BOMB_SPAWN_BUTTON);
} else {
if (arg0.getButton() == PhysicsModel.MOUSE_JOINT_BUTTON) {
mouseJointButtonDown = false;
}
Vec2 vv = worldPos(model, arg0);
controller.queueMouseUp(vv, arg0.getButton());
}
}
@Override
public void mousePressed(MouseEvent arg0) {
if (arg0.getButton() == screenDragButton) {
screenDragButtonDown = true;
oldDragMouse.set(arg0.getX(), arg0.getY());
return;
} else if (model.getCodedKeys()[KeyEvent.VK_SHIFT]) {
controller.queueMouseDown(new Vec2(arg0.getX(), arg0.getY()), PhysicsModel.BOMB_SPAWN_BUTTON);
} else {
if (arg0.getButton() == PhysicsModel.MOUSE_JOINT_BUTTON) {
mouseJointButtonDown = true;
}
Vec2 vv = worldPos(model, arg0);
controller.queueMouseDown(vv, arg0.getButton());
}
}
});
panel.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent arg0) {
mouse.set(arg0.getX(), arg0.getY());
controller.queueMouseMove(new Vec2(mouse));
}
@Override
public void mouseDragged(MouseEvent arg0) {
mouse.set(arg0.getX(), arg0.getY());
if (screenDragButtonDown) {
PhysicsModel currTest = model.getCurrTest();
if (currTest == null) {
return;
}
Vec2 diff = oldDragMouse.sub(mouse);
currTest.getCamera().moveWorld(diff);
oldDragMouse.set(mouse);
} else if (mouseJointButtonDown) {
Vec2 vv = worldPos(model, arg0);
controller.queueMouseDrag(vv, PhysicsModel.MOUSE_JOINT_BUTTON);
} else if (model.getCodedKeys()[KeyEvent.VK_SHIFT]) {
controller.queueMouseDrag(new Vec2(arg0.getX(), arg0.getY()), PhysicsModel.BOMB_SPAWN_BUTTON);
} else {
controller.queueMouseDrag(new Vec2(mouse), arg0.getButton());
}
}
});
panel.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent arg0) {
controller.queueKeyReleased(arg0.getKeyChar(), arg0.getKeyCode());
}
@Override
public void keyPressed(KeyEvent arg0) {
char c = arg0.getKeyChar();
controller.queueKeyPressed(c, arg0.getKeyCode());
switch(c) {
case '[':
controller.lastTest();
break;
case ']':
controller.nextTest();
break;
case 'r':
controller.reset();
break;
case ' ':
controller.queueLaunchBomb();
break;
case 'p':
controller.queuePause();
break;
}
}
});
}
use of spacegraph.space2d.phys.common.Vec2 in project narchy by automenta.
the class AWTPanelHelper method worldPos.
public static Vec2 worldPos(TestbedState model, MouseEvent arg0) {
Vec2 vv;
model.getDebugDraw().getScreenToWorldToOut(arg0.getX(), arg0.getY(), vv = new Vec2());
return vv;
}
use of spacegraph.space2d.phys.common.Vec2 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();
}
use of spacegraph.space2d.phys.common.Vec2 in project narchy by automenta.
the class JetpackEmitterSettings method onCreateParticle.
@Override
protected void onCreateParticle() {
// set offset
super.offset = new Vec2(rng.nextFloat() * super.xLocationVariance * -0.2f, rng.nextFloat() * super.yLocationVariance * 0.02f);
// set particle lifetime
if (rng.nextFloat() > 0.9f) {
float low = 0.8f, high = 1.0f;
float liveLong = rng.nextFloat();
if (liveLong < low)
super.particleLifetime = low * 2.0f;
else if (liveLong > high)
super.particleLifetime = high * 2.0f;
else
super.particleLifetime = liveLong * 2.0f;
} else {
super.particleLifetime = rng.nextFloat() * 0.75f;
}
// set particle force
super.particleForce = new Vec2(rng.nextBoolean() ? rng.nextFloat() * -5.0f : rng.nextFloat() * -5.0f, rng.nextFloat() * -50.0f);
}
use of spacegraph.space2d.phys.common.Vec2 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))));
}
Aggregations