⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ball.java

📁 MASON代表多主体邻里或网络仿真(Multi-Agent Simulator of Neighborhoods or Networks)。它是乔治梅森大学用Java开发的离散事件多主体仿真核心库
💻 JAVA
字号:
package sim.app.balls3d;import sim.engine.*;import sim.portrayal3d.*;import sim.util.*;import sim.field.network.*;import sim.field.continuous.*;public class Ball extends SimplePortrayal3D implements Steppable    {    // force on the Ball    public double forcex;    public double forcey;    public double forcez;        // Ball mass    public double mass;        // Current Ball velocity    public double velocityx;    public double velocityy;    public double velocityz;         // 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 getVelocityZ() { return velocityz; }    public void setVelocityZ(double val) { velocityz = val; }     public double getMass() { return mass; }    public void setMass(double val) { if (val > 0) { mass = val; diameter = Math.sqrt(val); } }            public String toString()         {         Double3D loc = Balls3D.balls.getObjectLocation(this);        double x = Math.floor(loc.x*10)/10;         double y = Math.floor(loc.y*10)/10;        double z = Math.floor(loc.z*10)/10;         return "(" + x + ", " + y + ", " + z + ")";         }        public Ball(double vx, double vy, double vz, double m)        {        velocityx=vx;        velocityy=vy;        velocityz=vz;         mass = m;        diameter = Math.sqrt(m);        }                    static Bag myBag = new Bag();    public void computeCollision(Balls3D tut)        {        collision = false;        Double3D me = Balls3D.balls.getObjectLocation(this);        Bag b = Balls3D.balls.getObjectsWithinDistance(me,Balls3D.collisionDistance,false, false, myBag);        for(int x=0;x<b.numObjs;x++)            if( this != b.objs[x] )                {                Double3D loc = Balls3D.balls.getObjectLocation(b.objs[x]);                if ((loc.x-me.x)*(loc.x-me.x) + (loc.y-me.y)*(loc.y-me.y) + (loc.z-me.z)*(loc.z-me.z)                    <= Balls3D.collisionDistance * Balls3D.collisionDistance)                    {                    collision = true;                    ((Ball)(b.objs[x])).collision = true;                    }                }        }                    public void addForce(Double3D otherBallLoc, Double3D myLoc, Band band)        {        // compute difference        final double dx = otherBallLoc.x - myLoc.x;        final double dy = otherBallLoc.y - myLoc.y;        final double dz = otherBallLoc.z - myLoc.z;         final double len = Math.sqrt(dx*dx + dy*dy + dz*dz);        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;            forcez += (dy * forcemagnitude) / len;             }        }                    public void computeForce(SimState state)        {        Balls3D tut = (Balls3D) state;        Network bands = tut.bands;        Continuous3D balls = tut.balls;        Double3D me = balls.getObjectLocation(this);                forcex = 0; forcey = 0; forcez = 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<in.numObjs;x++)                {                Edge e = (Edge)(in.objs[x]);                Band b = (Band) (e.info);                Ball other = (Ball)(e.from());  // from him to me                Double3D him = balls.getObjectLocation(other);                addForce(him,me,b);                }        if (out!=null)            for(int x=0;x<out.numObjs;x++)                {                Edge e = (Edge)(out.objs[x]);                Band b = (Band) (e.info);                Ball other = (Ball)(e.to());  // from me to him                Double3D him = balls.getObjectLocation(other);                addForce(him,me,b);                }        }        public void step(SimState state)        {        Balls3D tut = (Balls3D) state;                // acceleration = force / mass        final double ax = forcex / mass;        final double ay = forcey / mass;        final double az = forcez / mass;                 // velocity = velocity + acceleration        velocityx += ax;        velocityy += ay;        velocityz += az;                 // position = position + velocity        Double3D pos = Balls3D.balls.getObjectLocation(this);        Double3D newpos = new Double3D(pos.x+velocityx, pos.y + velocityy, pos.z + velocityz);        Balls3D.balls.setObjectLocation(this,newpos);                // compute collisions        computeCollision(tut);        }    }    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -