All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sim.app.tutorial5.Ball Maven / Gradle / Ivy

Go to download

MASON is a fast discrete-event multiagent simulation library core in Java, designed to be the foundation for large custom-purpose Java simulations, and also to provide more than enough functionality for many lightweight simulation needs. MASON contains both a model library and an optional suite of visualization tools in 2D and 3D.

The newest version!
/*
  Copyright 2006 by Sean Luke and George Mason University
  Licensed under the Academic Free License version 3.0
  See the file "LICENSE" for more information
*/

package sim.app.tutorial5;
import sim.engine.*;
import sim.portrayal.*;
import sim.util.*;
import sim.field.network.*;
import sim.field.continuous.*;
import java.awt.*;
import java.awt.geom.*;


public class Ball extends SimplePortrayal2D implements Steppable
    {
    private static final long serialVersionUID = 1;

    // force on the Ball
    public double forcex;
    public double forcey;
    
    // Ball mass
    public double mass;
    
    // Current Ball velocity
    public double velocityx;
    public double velocityy;
    
    // did the Ball collide?
    public boolean collision;
    
    // for drawing: always sqrt of mass
    public double diameter;
        
    public double getVelocityX() { return velocityx; }
    public void setVelocityX(double val) { velocityx = val; }
    public double getVelocityY() { return velocityy; }
    public void setVelocityY(double val) { velocityy = val; }
    public double getMass() { return mass; }
    public void setMass(double val) { if (val > 0) { mass = val; diameter = Math.sqrt(val); } }
    
    public Ball(double vx, double vy, double m)
        {
        velocityx=vx;
        velocityy=vy;
        mass = m;
        diameter = Math.sqrt(m);
        }

    public void computeCollision(Tutorial5 tut)
        {
        /*
        // old previous code
        
        collision = false;
        Double2D me = tut.balls.getObjectLocation(this);
        Bag b = tut.balls.getNeighborsWithinDistance(me,Tutorial5.collisionDistance);
        for(int x=0;x 1;  // other than myself of course
        }
    
    public void addForce(Double2D otherBallLoc, Double2D myLoc, Band band)
        {
        // compute difference
        final double dx = otherBallLoc.x - myLoc.x;
        final double dy = otherBallLoc.y - myLoc.y;
        final double len = Math.sqrt(dx*dx + dy*dy);
        final double l = band.laxDistance;

        final double k = band.strength/512.0;  // cut-down
        final double forcemagnitude = (len - l) * k;
        
        // add rubber band force
        if (len - l > 0) 
            {
            forcex += (dx * forcemagnitude) / len;
            forcey += (dy * forcemagnitude) / len;
            }
        }
    
    public void computeForce(SimState state)
        {
        Tutorial5 tut = (Tutorial5) state;
        Network bands = tut.bands;
        Continuous2D balls = tut.balls;

        Double2D me = balls.getObjectLocation(this);
        
        forcex = 0; forcey = 0;
        // rubber bands exert a force both ways --
        // so our graph is undirected.  We need to get edges
        // both in and out, as they could be located either place
        Bag in = bands.getEdgesIn(this);
        Bag out = bands.getEdgesOut(this);
        if (in!=null)
            for(int x=0;x




© 2015 - 2025 Weber Informatics LLC | Privacy Policy