2020-09-16 14:20:51 +02:00
|
|
|
package de.hems.trafficsim;
|
|
|
|
|
2020-10-12 18:26:52 +02:00
|
|
|
import java.util.Random;
|
|
|
|
|
2020-11-15 16:30:41 +01:00
|
|
|
/**
|
|
|
|
* Model class which represents a vehicle in the simulation.
|
|
|
|
*/
|
2020-09-16 14:20:51 +02:00
|
|
|
public class Vehicle {
|
2020-11-15 16:30:41 +01:00
|
|
|
/**
|
|
|
|
* a number to index the vehicle
|
|
|
|
*/
|
2020-11-15 16:45:27 +01:00
|
|
|
final protected int id;
|
2020-11-15 16:30:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* current position of the vehicle on the track
|
|
|
|
*/
|
2020-09-16 14:20:51 +02:00
|
|
|
protected float position;
|
2020-11-15 16:30:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* current velocity of the vehicle
|
|
|
|
*/
|
2020-09-16 14:20:51 +02:00
|
|
|
protected float curVelocity;
|
2020-11-15 16:30:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* maximum velocity of the vehicle
|
|
|
|
*/
|
2020-09-16 14:20:51 +02:00
|
|
|
protected float maxVelocity;
|
2020-11-15 16:30:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* probability of the vehicle to brake without reason
|
|
|
|
*/
|
2020-11-11 16:10:35 +01:00
|
|
|
protected float brakeProb;
|
2020-09-16 14:20:51 +02:00
|
|
|
|
2020-11-15 16:30:41 +01:00
|
|
|
/**
|
|
|
|
* length of the track the vehicle is placed on
|
|
|
|
*/
|
2020-11-15 16:45:27 +01:00
|
|
|
final protected float trackLength;
|
2020-09-16 14:20:51 +02:00
|
|
|
|
2020-11-15 16:30:41 +01:00
|
|
|
/**
|
|
|
|
* Constructs a new vehicle.
|
|
|
|
*
|
|
|
|
* @param id a number to index the vehicle
|
|
|
|
* @param position current position of the vehicle on the track
|
|
|
|
* @param maxVelocity maximum velocity of the vehicle
|
|
|
|
* @param brakeProb probability of the vehicle to brake without reason
|
|
|
|
* @param trackLength length of the track the vehicle is placed on
|
|
|
|
*/
|
2020-11-11 16:10:35 +01:00
|
|
|
public Vehicle(int id, int position, float maxVelocity, float brakeProb, float trackLength) {
|
2020-10-12 18:26:52 +02:00
|
|
|
this.id = id;
|
|
|
|
this.position = position;
|
|
|
|
this.maxVelocity = maxVelocity;
|
2020-11-11 16:10:35 +01:00
|
|
|
this.brakeProb = brakeProb;
|
2020-10-12 18:26:52 +02:00
|
|
|
this.curVelocity = 0;
|
2020-10-29 11:53:35 +01:00
|
|
|
this.trackLength = trackLength;
|
2020-10-12 18:26:52 +02:00
|
|
|
|
|
|
|
}
|
2020-09-16 14:20:51 +02:00
|
|
|
|
2020-11-15 16:30:41 +01:00
|
|
|
/**
|
|
|
|
* Getter for position
|
|
|
|
*
|
|
|
|
* @return position
|
|
|
|
*/
|
2020-09-16 14:20:51 +02:00
|
|
|
public float getPosition() {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
2020-11-15 16:30:41 +01:00
|
|
|
/**
|
|
|
|
* Getter for curVelocity
|
|
|
|
*
|
|
|
|
* @return curVelocity
|
|
|
|
*/
|
2020-09-16 14:20:51 +02:00
|
|
|
public float getCurVelocity() {
|
|
|
|
return curVelocity;
|
|
|
|
}
|
|
|
|
|
2020-11-15 16:30:41 +01:00
|
|
|
/**
|
|
|
|
* Getter for maxVelocity
|
|
|
|
*
|
|
|
|
* @return maxVelocity
|
|
|
|
*/
|
2020-10-26 21:17:40 +01:00
|
|
|
public float getMaxVelocity() {
|
|
|
|
return maxVelocity;
|
|
|
|
}
|
|
|
|
|
2020-11-15 16:30:41 +01:00
|
|
|
/**
|
|
|
|
* Setter for maxVelocity
|
|
|
|
*
|
|
|
|
* @return maxVelocity
|
|
|
|
*/
|
|
|
|
public void setMaxVelocity(float maxVelocity) {
|
|
|
|
this.maxVelocity = maxVelocity;
|
|
|
|
}
|
2020-11-11 16:10:35 +01:00
|
|
|
|
2020-11-15 16:30:41 +01:00
|
|
|
/**
|
|
|
|
* Setter for brakeProb
|
|
|
|
*
|
|
|
|
* @return brakeProb
|
|
|
|
*/
|
|
|
|
public void setBrakeProb(float brakeProb) {
|
|
|
|
this.brakeProb = brakeProb;
|
|
|
|
}
|
2020-11-11 16:10:35 +01:00
|
|
|
|
2020-11-15 16:30:41 +01:00
|
|
|
/**
|
|
|
|
* Calculates the new velocity of the vehicle based on the following rules:
|
|
|
|
* <ol>
|
|
|
|
* <li>Increase by one if the vehicle is slower than it's maximum speed</li>
|
|
|
|
* <li>Random brake by one</li>
|
|
|
|
* <li>Break if the distance to the forerunner is less than it's speed</li>
|
|
|
|
* </ol>
|
|
|
|
*
|
|
|
|
* @param distanceForerunner distance to the forerunner of the vehicle
|
|
|
|
*/
|
2020-10-12 18:26:52 +02:00
|
|
|
public void updateVelocity(float distanceForerunner) {
|
|
|
|
Random random = new Random();
|
|
|
|
|
|
|
|
float r = random.nextFloat();
|
|
|
|
|
|
|
|
if (curVelocity < maxVelocity) {
|
|
|
|
curVelocity = curVelocity + 1;
|
|
|
|
}
|
2020-11-11 16:10:35 +01:00
|
|
|
if (r < brakeProb && curVelocity > 0) {
|
|
|
|
curVelocity = curVelocity - 1;
|
2020-10-12 18:26:52 +02:00
|
|
|
}
|
|
|
|
if (curVelocity > distanceForerunner) {
|
|
|
|
curVelocity = distanceForerunner;
|
|
|
|
}
|
2020-09-16 14:20:51 +02:00
|
|
|
}
|
|
|
|
|
2020-11-15 16:30:41 +01:00
|
|
|
/**
|
|
|
|
* Updates the position of the vehicle according to it's previously calculated speed.
|
|
|
|
*/
|
2020-11-02 17:19:07 +01:00
|
|
|
public void timeElapse() {
|
2020-10-29 11:53:35 +01:00
|
|
|
position = (position + curVelocity) % this.trackLength;
|
2020-09-16 14:20:51 +02:00
|
|
|
}
|
2020-10-12 18:26:52 +02:00
|
|
|
|
2020-09-16 14:20:51 +02:00
|
|
|
}
|
2020-10-12 18:26:52 +02:00
|
|
|
|