📄 veh.java
字号:
import swarm.Globals;
import swarm.defobj.Zone;
import swarm.objectbase.SwarmObjectImpl;
import swarm.space.Grid2dImpl;
import java.util.*;
public class Veh extends SwarmObjectImpl
{
final static double T=0.1;//仿真步长
final static double minBrakeGap=2000;
final static double stopDistance=1.5;
static double length=7.0;
int id;
float acc;
float maxAcc;
float maxDcc;
double speed;
double expSpeed;
float distance;//车距离出发点的距离
double angle=30;
boolean delFlag;
Node position;
Road road;
public Veh(Zone aZone,Node pos,Road road,int id)
{
super(aZone);
this.position=pos;
this.road=road;
this.id=id;
acc=0;
maxAcc=8;
maxDcc=-8;
speed=distance=0;
expSpeed=20+10*Math.random();
delFlag=false;
}
boolean addToRoad(Road road)
{
Random ran=new Random();
double dis=50;
Veh preVeh=getPreVeh();
boolean addToRoad=false;
if(!road.startFlag)
{
addToRoad=false;
}
else
{
if(preVeh==null)
{
this.road=road;
this.position=road.from;
road.vehChain.add(this);
addToRoad=true;
}
else
{
if(preVeh.distance-dis-length>0)
{
this.road=road;
this.position=road.from;
road.vehChain.add(this);
addToRoad=true;
}
else
{
addToRoad=false;
}
}
}
if(speed==0.0)
{
speed=ran.nextInt(25);
}
angle=road.startAngle;
distance=0;
return addToRoad;
}
Veh getPreVeh()
{
LinkedList vehChain=road.vehChain;
if(vehChain.size()>0)
{
int index=vehChain.indexOf(this);
if(index<2)
{
return null;
}
else
{
return (Veh)vehChain.get(index-1);
}
}
else
return null;
}
double calcGap()
{
double gap;
Veh tempVeh,pre=getPreVeh();
this.calcPosition();
if(pre!=null)
{
tempVeh=pre;
tempVeh.calcPosition();
gap=tempVeh.distance-this.distance-this.length;
return gap;
}
else
{
return 200;
}
}
void calcPosition()
{
road.calculateLine(this.position,this.distance,this.angle);
}
public void follow()
{
double gap=calcGap();
float tempAcc;
double dis;
boolean aa=true;
if(checkRoad())
{
//return ;
}
//自由行驶
if(gap>=2*(3600*speed/minBrakeGap))
{
System.out.println("the "+id+" Number Car is free");
aa=false;
if(speed>expSpeed)
{
acc= (float)(maxDcc*(1-Math.pow(expSpeed/speed,2)));
}
else
{
acc=(float)(maxAcc*(1-Math.pow(speed/expSpeed,2)));
}
}
//一般跟驰
else if(gap>(3600*speed/minBrakeGap)&&aa)
{
System.out.println("The"+id+"car is Normal following");
Veh preVeh=getPreVeh();
if(preVeh!=null)
{
if(preVeh.acc>0)//前车加速行驶
{
for(int n=1;n<10;n++)
{
tempAcc=maxAcc*(11-n)/10;
dis=gap+preVeh.speed*T+Math.pow(preVeh.acc*T+preVeh.speed,2)/(2*preVeh.acc)-speed*T
-Math.pow(speed+tempAcc*T,2)/(2*tempAcc);
if(dis>stopDistance)
{
acc=tempAcc;
break;
}
}
}
}
else if(preVeh.acc==0)//前车匀速行驶
{
for(int n=1;n<10;n++)
{
tempAcc=maxAcc*(11-n)/10;
dis=gap+preVeh.speed*T-speed*T-Math.pow(speed+tempAcc*T,2)/(2*tempAcc);
if(dis>stopDistance)
{
acc=tempAcc;
break;
}
}
}
else if(preVeh.acc<0)//前车减速行驶
{
for(int n=1;n<10;n++)
{
tempAcc=maxDcc*n/10;
dis=gap+preVeh.speed*T+Math.pow(preVeh.acc*T+preVeh.speed,2)/(2*preVeh.acc)-speed*T
-Math.pow(speed+tempAcc*T,2)/(2*tempAcc);
if(dis>stopDistance)
{
acc=tempAcc;
break;
}
}
}
}
//紧密跟驰
else if(0<gap&&gap<(3600*speed/minBrakeGap))
{
acc=maxDcc;
System.out.println("The"+id+"car is too near");
}
//碰撞时
else if(gap<0)
{
acc=0;
speed=0;
}
distance+=speed*T+0.5*acc*T*T;
speed+=acc*T;
System.out.println("The"+id+"car's accelerate is"+acc+" And now distance "+distance);
}
boolean checkRoad()
{
if(distance>road.length)
{
delete();
return false;
}
else
{
return true;
}
}
void delete()
{
delFlag=true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -