Package eas.users.demos.physics2DD

Source Code of eas.users.demos.physics2DD.PhysSchedulerSimple

/*
* 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.physics2DD;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

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.CrazyBody;
import eas.simulation.spatial.sim2D.physicalSimulation.standardAgents.PhysicsAgent2D;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;

/**
* @author Lukas König
*/
public class PhysSchedulerSimple extends AbstractDefaultKeyEventMaster<PhysEnvSimple> {

    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 PhysEnvSimple[] generateRunnables(ParCollection params) {
      PhysEnvSimple env = new PhysEnvSimple(
                0,
                params,
                new Vector2f(0, 0),
                5);
    fillEnv(params, env);
   
    return new PhysEnvSimple[] {env};
  }

  private void fillEnv(ParCollection params, PhysEnvSimple env) {
        env.addAgent(new CrazyBody(Integer.MAX_VALUE, env, params), new Vector2D(20, 130), 180);
    }

    @Override
  public void runDuringSimulation(
          PhysEnvSimple umg, Wink w, ParCollection params) {
      super.runDuringSimulation(umg, w, params);

      if (this.clear) {
          clear = false;
          umg.removeAllAgents();
          umg.clear();
          fillEnv(params, umg);
      }

      if (this.clearAgent) {
          this.clearAgent = false;
          AllroundVideoPlugin vid = (AllroundVideoPlugin) umg.getSimTime().getPluginObject(new AllroundVideoPlugin().id());
          try {
                umg.removeAgent(vid.getMarkedAgentId());
            } catch (Exception e) {
            }
      }
     
      if (!pause && w.getLastTick() % 5 == 0) {
          PhysicsAgent2D<PhysEnvSimple> agent;
          if (new Random().nextBoolean()) {
              agent = new PhysAgentRectangleSimple((int) w.getLastTick(), umg, 10, params);
          } else {
                agent = new PhysAgentCircleSimple((int) w.getLastTick(), umg, 10, params);
          }
         
          umg.addAgent(agent, new Vector2D(15, 113), 90);
          agent.addForce(new Vector2f(-100000, 0));
      }
     
      if (insertAgent && w.getLastTick() % 1 == 0) {
            PhysicsAgent2D<PhysEnvSimple> agent;
            if (new Random().nextBoolean()) {
                agent = new PhysAgentRectangleSimple((int) w.getLastTick(), umg, 10, params);
            } else {
                agent = new PhysAgentCircleSimple((int) w.getLastTick(), umg, 10, params);
            }
           
            umg.addAgent(agent, new Vector2D(-70, 70), 0);
            agent.addForce(new Vector2f(0, 1000));
      }

        if (insertAgent2 && w.getLastTick() % 1 == 0) {
            PhysicsAgent2D<PhysEnvSimple> agent;
            if (new Random().nextBoolean()) {
                agent = new PhysAgentRectangleSimple((int) w.getLastTick(),
                        umg, 10, params);
            } else {
                agent = new PhysAgentCircleSimple((int) w.getLastTick(),
                        umg, 10, params);
            }

            umg.addAgent(agent, new Vector2D(-100, -30), 0);
            agent.addForce(new Vector2f(0, 1000));
        }

        if (insertAgent3 && w.getLastTick() % 1 == 0) {
            PhysicsAgent2D<PhysEnvSimple> agent;
            if (new Random().nextBoolean()) {
                agent = new PhysAgentRectangleSimple((int) w.getLastTick(),
                        umg, 10, params);
            } else {
                agent = new PhysAgentCircleSimple((int) w.getLastTick(),
                        umg, 10, params);
            }

            try {
                AllroundVideoPlugin vid = (AllroundVideoPlugin) umg.getSimTime().getPluginObject(new AllroundVideoPlugin().id());
                umg.addAgent(agent, umg.getAgentPosition(vid.getMarkedAgentId()), 0);
                agent.addForce(new Vector2f(0, 1000));
            } catch (Exception e) {
            }
           
        }

      for (int id : umg.getAgentIDs()) {
          if (id != Integer.MAX_VALUE && umg.getAgentShapeInEnvironment(Integer.MAX_VALUE) != null) {
//                if (!Arrows.generateSimplePol().isPointWithin(umg.getAgentPosition(id))) {
              if (!umg.getAgentShapeInEnvironment(Integer.MAX_VALUE).getBoundingBox().isPointInside(umg.getAgentPosition(id))) {
                  umg.removeAgent(id);
              }
          }
      }
     
      if (freeze) {
          freeze = false;
          for (PhysicsAgent2D<?> a : new LinkedList<>(umg.getAgents())) {
              Vector2D pos = umg.getAgentPosition(a.id());
              double alpha = umg.getAgentAngle(a.id());
              int id = a.id();
              float mass = a.getMass();
             
              if (PhysAgentCircleSimple.class.isAssignableFrom(a.getClass())) {
                  umg.removeAgent(id);
                  PhysAgentCircleSimple a2 = new PhysAgentCircleSimple(id, umg, mass, params);
                  a2.setStaticAgent(true);
                  umg.addAgent(a2, pos, alpha);
              }
             
                if (PhysAgentRectangleSimple.class.isAssignableFrom(a.getClass())) {
                    umg.removeAgent(id);
                    PhysAgentRectangleSimple a2 = new PhysAgentRectangleSimple(id, umg, mass, params);
                    a2.setStaticAgent(true);
                    umg.addAgent(a2, pos, alpha);
                }
          }
      }
  }
 
  @Override
  public String id() {
    return ConstantsSimulation.DEFAULT_MASTER_SCHEDULER_ID + "-physDemo2";
  }

  private boolean clear = false;
  private boolean clearAgent = false;
  private boolean pause = true;
    private boolean insertAgent = false;
    private boolean insertAgent2 = false;
    private boolean insertAgent3 = false;
    private boolean freeze = false;
 
  @Override
  public void handleEvent(EASEvent e, PhysEnvSimple env, Wink lastTick,
          ParCollection params) {
      super.handleEvent(e, env, lastTick, params);
     
      if (e.getEventDescription().contains("#")) {
          clearAgent = true;
        } else if (e.getEventDescription().contains("*")) {
            pause = !pause;
        } else if (e.getEventDescription().contains("+")) {
            insertAgent = !insertAgent;
        } else if (e.getEventDescription().contains(",")) {
            insertAgent2 = !insertAgent2;
        } else if (e.getEventDescription().contains("/")) {
            insertAgent3 = !insertAgent3;
        } else if (e.getEventDescription().contains(" ")) {
          clear = true;
      } else if (e.getEventDescription().contains("ß")) {
          freeze  = true;
      }
  }
}
TOP

Related Classes of eas.users.demos.physics2DD.PhysSchedulerSimple

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.