From 4b3ba0e22c98f4788e68235f88e807417fe1b271 Mon Sep 17 00:00:00 2001 From: "Loch Christian (uib05376)" Date: Fri, 30 Oct 2020 22:35:33 +0100 Subject: [PATCH] Partially implement timesteps --- .../main/java/de/hems/trafficsim/MainActivity.java | 4 ++-- app/src/main/java/de/hems/trafficsim/Track.java | 9 ++++++--- app/src/main/java/de/hems/trafficsim/Vehicle.java | 14 ++++++++------ app/src/main/java/de/hems/trafficsim/Worker.java | 7 ++++++- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/de/hems/trafficsim/MainActivity.java b/app/src/main/java/de/hems/trafficsim/MainActivity.java index e2581c5..7e5be08 100644 --- a/app/src/main/java/de/hems/trafficsim/MainActivity.java +++ b/app/src/main/java/de/hems/trafficsim/MainActivity.java @@ -21,7 +21,7 @@ public class MainActivity extends AppCompatActivity implements Observer { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - this.track = new Track(25, 100); + this.track = new Track(1, 100); this.trackView = new TrackView(this, this.track); this.track.addObserver(this); ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout); @@ -54,7 +54,7 @@ public class MainActivity extends AppCompatActivity implements Observer { public void onStepButtonClick(View view) { //for (int j = 0; j < 3000; j++) { - this.track.timeElapse(50); + this.track.timeElapse(1f); //} } diff --git a/app/src/main/java/de/hems/trafficsim/Track.java b/app/src/main/java/de/hems/trafficsim/Track.java index 06f9ed6..b0103a7 100644 --- a/app/src/main/java/de/hems/trafficsim/Track.java +++ b/app/src/main/java/de/hems/trafficsim/Track.java @@ -27,7 +27,7 @@ public class Track extends Observable { protected List createVehiclesList(int numberVehicles){ List result = new ArrayList<>(); for(int i=0;i records = new ArrayList<>(vehicles.size()); this.vtr_list.add(records); for(Vehicle v: vehicles){ - v.timeElapse(50); + v.timeElapse(timeStep); VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity, timeStep); records.add(vtr); } @@ -60,4 +60,7 @@ public class Track extends Observable { this.notifyObservers(this); this.clearChanged(); } + + // Durchschnittsgeschwindigkeit, Anteil der Fahrzeuge unter einem bestimmten Threshold + // } diff --git a/app/src/main/java/de/hems/trafficsim/Vehicle.java b/app/src/main/java/de/hems/trafficsim/Vehicle.java index e6fbb0b..6b35ed1 100644 --- a/app/src/main/java/de/hems/trafficsim/Vehicle.java +++ b/app/src/main/java/de/hems/trafficsim/Vehicle.java @@ -33,25 +33,27 @@ public class Vehicle { return maxVelocity; } - public void updateVelocity(float distanceForerunner) { + public void updateVelocity(float distanceForerunner, float timeStep) { Random random = new Random(); float r = random.nextFloat(); if (curVelocity < maxVelocity) { - curVelocity = curVelocity + 1; + curVelocity = curVelocity + 1*timeStep; } if (r < brakeProp && curVelocity > 0) { curVelocity = getCurVelocity() - 1; + if (curVelocity < 0) + curVelocity = 0; } - if (curVelocity > distanceForerunner) { - curVelocity = distanceForerunner; + float distance = this.curVelocity * timeStep; + if (distance > distanceForerunner) { + curVelocity = distanceForerunner/timeStep; } - } public void timeElapse(float timeStep) { - position = (position + curVelocity) % this.trackLength; + position = (position + curVelocity*timeStep) % this.trackLength; } } diff --git a/app/src/main/java/de/hems/trafficsim/Worker.java b/app/src/main/java/de/hems/trafficsim/Worker.java index 862f60f..6cf44bb 100644 --- a/app/src/main/java/de/hems/trafficsim/Worker.java +++ b/app/src/main/java/de/hems/trafficsim/Worker.java @@ -1,5 +1,7 @@ package de.hems.trafficsim; +import java.util.Date; + public class Worker extends Thread { protected Track track; protected boolean stop; @@ -15,8 +17,11 @@ public class Worker extends Thread { @Override public void run() { + long lastStartTime = System.currentTimeMillis(); while (!stop) { - this.track.timeElapse(50); + long startTime = System.currentTimeMillis(); + this.track.timeElapse((startTime - lastStartTime)); + lastStartTime = startTime; } } }