You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.2KB

  1. package de.hems.trafficsim;
  2. import java.util.Random;
  3. /**
  4. * Model class which represents a vehicle in the simulation.
  5. */
  6. public class Vehicle {
  7. /**
  8. * a number to index the vehicle
  9. */
  10. protected int id;
  11. /**
  12. * current position of the vehicle on the track
  13. */
  14. protected float position;
  15. /**
  16. * current velocity of the vehicle
  17. */
  18. protected float curVelocity;
  19. /**
  20. * maximum velocity of the vehicle
  21. */
  22. protected float maxVelocity;
  23. /**
  24. * probability of the vehicle to brake without reason
  25. */
  26. protected float brakeProb;
  27. /**
  28. * length of the track the vehicle is placed on
  29. */
  30. protected float trackLength;
  31. /**
  32. * Constructs a new vehicle.
  33. *
  34. * @param id a number to index the vehicle
  35. * @param position current position of the vehicle on the track
  36. * @param maxVelocity maximum velocity of the vehicle
  37. * @param brakeProb probability of the vehicle to brake without reason
  38. * @param trackLength length of the track the vehicle is placed on
  39. */
  40. public Vehicle(int id, int position, float maxVelocity, float brakeProb, float trackLength) {
  41. this.id = id;
  42. this.position = position;
  43. this.maxVelocity = maxVelocity;
  44. this.brakeProb = brakeProb;
  45. this.curVelocity = 0;
  46. this.trackLength = trackLength;
  47. }
  48. /**
  49. * Getter for position
  50. *
  51. * @return position
  52. */
  53. public float getPosition() {
  54. return position;
  55. }
  56. /**
  57. * Getter for curVelocity
  58. *
  59. * @return curVelocity
  60. */
  61. public float getCurVelocity() {
  62. return curVelocity;
  63. }
  64. /**
  65. * Getter for maxVelocity
  66. *
  67. * @return maxVelocity
  68. */
  69. public float getMaxVelocity() {
  70. return maxVelocity;
  71. }
  72. /**
  73. * Setter for maxVelocity
  74. *
  75. * @return maxVelocity
  76. */
  77. public void setMaxVelocity(float maxVelocity) {
  78. this.maxVelocity = maxVelocity;
  79. }
  80. /**
  81. * Setter for brakeProb
  82. *
  83. * @return brakeProb
  84. */
  85. public void setBrakeProb(float brakeProb) {
  86. this.brakeProb = brakeProb;
  87. }
  88. /**
  89. * Calculates the new velocity of the vehicle based on the following rules:
  90. * <ol>
  91. * <li>Increase by one if the vehicle is slower than it's maximum speed</li>
  92. * <li>Random brake by one</li>
  93. * <li>Break if the distance to the forerunner is less than it's speed</li>
  94. * </ol>
  95. *
  96. * @param distanceForerunner distance to the forerunner of the vehicle
  97. */
  98. public void updateVelocity(float distanceForerunner) {
  99. Random random = new Random();
  100. float r = random.nextFloat();
  101. if (curVelocity < maxVelocity) {
  102. curVelocity = curVelocity + 1;
  103. }
  104. if (r < brakeProb && curVelocity > 0) {
  105. curVelocity = curVelocity - 1;
  106. }
  107. if (curVelocity > distanceForerunner) {
  108. curVelocity = distanceForerunner;
  109. }
  110. }
  111. /**
  112. * Updates the position of the vehicle according to it's previously calculated speed.
  113. */
  114. public void timeElapse() {
  115. position = (position + curVelocity) % this.trackLength;
  116. }
  117. }