TrafficSim/app/src/main/java/de/hems/trafficsim/Vehicle.java
2020-10-30 22:35:33 +01:00

61 lines
1.5 KiB
Java

package de.hems.trafficsim;
import java.util.Random;
public class Vehicle {
protected int id;
protected float position;
protected float curVelocity;
protected float maxVelocity;
protected float brakeProp;
protected float trackLength;
public Vehicle(int id, int position, float maxVelocity, float brakeProp, float trackLength) {
this.id = id;
this.position = position;
this.maxVelocity = maxVelocity;
this.brakeProp = brakeProp;
this.curVelocity = 0;
this.trackLength = trackLength;
}
public float getPosition() {
return position;
}
public float getCurVelocity() {
return curVelocity;
}
public float getMaxVelocity() {
return maxVelocity;
}
public void updateVelocity(float distanceForerunner, float timeStep) {
Random random = new Random();
float r = random.nextFloat();
if (curVelocity < maxVelocity) {
curVelocity = curVelocity + 1*timeStep;
}
if (r < brakeProp && curVelocity > 0) {
curVelocity = getCurVelocity() - 1;
if (curVelocity < 0)
curVelocity = 0;
}
float distance = this.curVelocity * timeStep;
if (distance > distanceForerunner) {
curVelocity = distanceForerunner/timeStep;
}
}
public void timeElapse(float timeStep) {
position = (position + curVelocity*timeStep) % this.trackLength;
}
}