/*
* File name: PhysScheduler.java (package eas.users.lukas.demos.lukesPhysics4)
* Author(s): Lukas König
* Java version: 6.0
* Generation date: 03.05.2012 (12:35:56)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.users.demos.physics2DC;
import java.awt.Color;
import java.util.List;
import eas.math.geometry.Polygon2D;
import eas.math.geometry.Vector2D;
import eas.plugins.masterScheduler.AbstractDefaultKeyEventMaster;
import eas.plugins.standard.visualization.AllroundVideoPlugin;
import eas.simulation.ConstantsSimulation;
import eas.simulation.Wink;
import eas.simulation.event.EASEvent;
import eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.math.Vector2f;
import eas.simulation.spatial.sim2D.physicalSimulation.standardAgents.PhysicsAgent2D;
import eas.simulation.spatial.sim2D.physicalSimulation.standardAgents.StaticBody;
import eas.simulation.spatial.sim2D.physicalSimulation.standardEnvironments.PhysicsEnvironment2D;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;
/**
* @author Lukas König
*/
public class PhysScheduler extends AbstractDefaultKeyEventMaster<PhysEnv> {
/**
*
*/
private static final long serialVersionUID = -8329217723166484110L;
/* (non-Javadoc)
* @see eas.simulation.masterScheduler.AbstractDefaultMaster#getParameters()
*/
@Override
public List<SingleParameter> getParameters() {
List<SingleParameter> list = super.getParameters();
list.add(new SingleParameter("numBoxes", Datatypes.integerRange(0, 30), 10, "Number of boxes per row AND column.", this.id().toUpperCase()));
list.add(new SingleParameter("numSpheres", Datatypes.integerRange(0, 100), 10, "Number of spheres.", this.id().toUpperCase()));
return list;
}
@Override
public PhysEnv[] generateRunnables(ParCollection params) {
PhysEnv env = new PhysEnv(
0,
params,
new Vector2f(0, 0),
150);
int max = params.getParValueInt("numBoxes");
int numSpheres = params.getParValueInt("numSpheres");
fillEnv(params, env, max, numSpheres);
return new PhysEnv[] {env};
}
private void fillEnv(ParCollection params, PhysEnv env, int max, int numSpheres) {
for (int i = 0; i < max; i++) {
for (int j = 0; j < max; j++) {
env.addAgent(new PhysicsAgent2D<PhysicsEnvironment2D<?>>(i * max + j + 1, env, 10, params) {
/**
*
*/
private static final long serialVersionUID = 3492184926381835319L;
Polygon2D pol;
@Override
public synchronized Polygon2D getAgentShape() {
if (pol == null) {
pol = new Polygon2D();
pol.add(new Vector2D(-1, -1));
pol.add(new Vector2D(1, -1));
pol.add(new Vector2D(1, 1));
pol.add(new Vector2D(-1, 1));
}
return pol;
}
}, new Vector2D(i * 2.1, j * 2.1), 0);
}
}
for (int i = 0; i < numSpheres; i++) {
env.addAgent(new PhysicsAgentCircle(max * max + i + 1, env, 300, params), new Vector2D(10 + 0.5 * i, -30 - 10 * i), 0, new Vector2D(0.1, 0.1));
}
env.addAgent(new StaticBody(max * max + numSpheres + 1, env, params), new Vector2D(20, 130), 180);
env.addAgent(new StaticBody(max * max + numSpheres + 2, env, params), new Vector2D(-50, 70), 90);
env.addAgent(new StaticBody(max * max + numSpheres + 3, env, params), new Vector2D(90, 70), 90);
PhysicsAgentCircle circ = new PhysicsAgentCircle(max * max + numSpheres + 4, env, 1000, params);
env.addAgent(circ, new Vector2D(-5, -1), 0, new Vector2D(0.1, 0.1));
circ.setColor(Color.blue);
env.setMoveableAgentID(max * max + numSpheres + 4);
}
@Override
public void runDuringSimulation(
PhysEnv umg, Wink w, ParCollection params) {
super.runDuringSimulation(umg, w, params);
if (this.clear) {
clear = false;
umg.removeAllAgents();
umg.clear();
int max = params.getParValueInt("numBoxes");
int numSpheres = params.getParValueInt("numSpheres");
this.fillEnv(params, umg, max, numSpheres);
}
if (w.getLastTick() > 0) {
return;
}
try {
AllroundVideoPlugin vid = (AllroundVideoPlugin) umg.getSimTime().getPluginObject(new AllroundVideoPlugin().id());
vid.setFitPerfectly(true);
} catch (Exception e) {
}
}
@Override
public String id() {
return ConstantsSimulation.DEFAULT_MASTER_SCHEDULER_ID + "-physDemo";
}
private boolean clear = false;
@Override
public void handleEvent(EASEvent e, PhysEnv env, Wink lastTick,
ParCollection params) {
super.handleEvent(e, env, lastTick, params);
clear = true;
}
}