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.

61 lines
1.5KB

  1. package de.hems.trafficsim;
  2. import java.util.Random;
  3. public class Vehicle {
  4. protected int id;
  5. protected float position;
  6. protected float curVelocity;
  7. protected float maxVelocity;
  8. protected float brakeProp;
  9. protected float trackLength;
  10. public Vehicle(int id, int position, float maxVelocity, float brakeProp, float trackLength) {
  11. this.id = id;
  12. this.position = position;
  13. this.maxVelocity = maxVelocity;
  14. this.brakeProp = brakeProp;
  15. this.curVelocity = 0;
  16. this.trackLength = trackLength;
  17. }
  18. public float getPosition() {
  19. return position;
  20. }
  21. public float getCurVelocity() {
  22. return curVelocity;
  23. }
  24. public float getMaxVelocity() {
  25. return maxVelocity;
  26. }
  27. public void updateVelocity(float distanceForerunner, float timeStep) {
  28. Random random = new Random();
  29. float r = random.nextFloat();
  30. if (curVelocity < maxVelocity) {
  31. curVelocity = curVelocity + 1*timeStep;
  32. }
  33. if (r < brakeProp && curVelocity > 0) {
  34. curVelocity = getCurVelocity() - 1;
  35. if (curVelocity < 0)
  36. curVelocity = 0;
  37. }
  38. float distance = this.curVelocity * timeStep;
  39. if (distance > distanceForerunner) {
  40. curVelocity = distanceForerunner/timeStep;
  41. }
  42. }
  43. public void timeElapse(float timeStep) {
  44. position = (position + curVelocity*timeStep) % this.trackLength;
  45. }
  46. }