Package eas.users.demos.wsc2012.rescuer

Source Code of eas.users.demos.wsc2012.rescuer.RockAgent

/*
* File name:        RockAgent.java (package eas.users.wsc2012.rescuer)
* Author(s):        Lukas König
* Java version:     6.0
* Generation date:  30.03.2012 (12:56:03)
*
* (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.wsc2012.rescuer;

import java.awt.Color;
import java.util.Random;

import eas.math.geometry.Polygon2D;
import eas.math.geometry.Rectangle2D;
import eas.math.geometry.Vector2D;
import eas.simulation.Wink;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.startSetup.ParCollection;

/**
* @author Lukas König
*
*/
public class RockAgent extends AbstractAgent2D<RescuerContinousEnv> {

    /**
     *
     */
    private static final long serialVersionUID = 4721956400331538121L;
    private Random rand;
    private Color color = new Color(150, 105, 50);
    private Polygon2D pol;
   
    public RockAgent(int id, RescuerContinousEnv env, ParCollection params, Random random) {
        super(id, env, params);
        rand = random;
    }
   
    @Override
    public Color getAgentColor() {
        return color;
    }
   
    @Override
    public synchronized Polygon2D getAgentShape() {
        if (pol == null) {
            final double precision = 20;
            final double maxRadius = 2;
            final double minRadius = 0.5;
            pol = new Polygon2D();
            double rad;
           
            for (double d = 0; d <= Math.PI * 2; d += Math.PI * 2 / precision) {
                rad = Math.max(Math.abs(rand.nextDouble()), minRadius) * maxRadius;
                pol.add(new Vector2D(Math.sin(d) * rad, Math.cos(d) * rad));
            }
//            pol.add(new Vector2D(0, 0));
            pol.rotate(Vector2D.NULL_VECTOR, -Math.PI * 0.125);
        }
       
        return pol;
    }
   
    @Override
    public void step(Wink simTime) {
        Rectangle2D bound = this.getEnvironment().getAgentShapeInEnvironment(this.id()).getBoundingBox();
        double agentRad = Math.sqrt(bound.getWidth() * bound.getWidth() + bound.getHeight() * bound.getHeight());
       
        double disturbanceFactor = 0.5;
        double gradientFactor = 0.3;
        Vector2D pos = this.getEnvironment().getAgentPosition(this.id());
        Vector2D vec = new Vector2D(this.getEnvironment().getGradientAtCoordinate(pos));
        vec.mult(gradientFactor);
        vec.translate(new Vector2D(
                (rand.nextDouble() - 0.5) * disturbanceFactor,
                (rand.nextDouble() - 0.5) * disturbanceFactor));
       
        Vector2D newPos = new Vector2D(vec).translate(pos);
        Vector2D newPos1 = new Vector2D(vec).translate(pos).translate(new Vector2D(agentRad, agentRad));
        Vector2D newPos2 = new Vector2D(vec).translate(pos).sub(new Vector2D(agentRad, agentRad));
        if (this.getEnvironment().getBoundingBox(false).isPointInside(newPos1) && this.getEnvironment().getBoundingBox(false).isPointInside(newPos2)) {
            this.getEnvironment().setAgentPosition(this.id(), newPos);
            double angle = rand.nextDouble() * 10;
            this.getEnvironment().setAgentAngle(this.id(), this.getEnvironment().getAgentAngle(this.id()) + angle);
        }
    }
}
TOP

Related Classes of eas.users.demos.wsc2012.rescuer.RockAgent

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.