Browse Source

Partially implement timesteps

tags/Release_1
Loch Christian (uib05376) 3 years ago
parent
commit
4b3ba0e22c
4 changed files with 22 additions and 12 deletions
  1. +2
    -2
      app/src/main/java/de/hems/trafficsim/MainActivity.java
  2. +6
    -3
      app/src/main/java/de/hems/trafficsim/Track.java
  3. +8
    -6
      app/src/main/java/de/hems/trafficsim/Vehicle.java
  4. +6
    -1
      app/src/main/java/de/hems/trafficsim/Worker.java

+ 2
- 2
app/src/main/java/de/hems/trafficsim/MainActivity.java View File

@@ -21,7 +21,7 @@ public class MainActivity extends AppCompatActivity implements Observer {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); 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.trackView = new TrackView(this, this.track);
this.track.addObserver(this); this.track.addObserver(this);
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout); ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout);
@@ -54,7 +54,7 @@ public class MainActivity extends AppCompatActivity implements Observer {


public void onStepButtonClick(View view) { public void onStepButtonClick(View view) {
//for (int j = 0; j < 3000; j++) { //for (int j = 0; j < 3000; j++) {
this.track.timeElapse(50);
this.track.timeElapse(1f);
//} //}
} }




+ 6
- 3
app/src/main/java/de/hems/trafficsim/Track.java View File

@@ -27,7 +27,7 @@ public class Track extends Observable {
protected List<Vehicle> createVehiclesList(int numberVehicles){ protected List<Vehicle> createVehiclesList(int numberVehicles){
List<Vehicle> result = new ArrayList<>(); List<Vehicle> result = new ArrayList<>();
for(int i=0;i<numberVehicles;i++){ for(int i=0;i<numberVehicles;i++){
Vehicle vehicle = new Vehicle(i, i, 5, 0.2f, this.trackLength);
Vehicle vehicle = new Vehicle(i, i, 5, 0.1f, this.trackLength);
result.add(vehicle); result.add(vehicle);
} }


@@ -46,12 +46,12 @@ public class Track extends Observable {
if(distanceForerunner < 0.0){ if(distanceForerunner < 0.0){
distanceForerunner += this.trackLength; distanceForerunner += this.trackLength;
} }
v.updateVelocity(distanceForerunner);
v.updateVelocity(distanceForerunner, timeStep);
} }
List <VehicleTimeRecord> records = new ArrayList<>(vehicles.size()); List <VehicleTimeRecord> records = new ArrayList<>(vehicles.size());
this.vtr_list.add(records); this.vtr_list.add(records);
for(Vehicle v: vehicles){ for(Vehicle v: vehicles){
v.timeElapse(50);
v.timeElapse(timeStep);
VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity, timeStep); VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity, timeStep);
records.add(vtr); records.add(vtr);
} }
@@ -60,4 +60,7 @@ public class Track extends Observable {
this.notifyObservers(this); this.notifyObservers(this);
this.clearChanged(); this.clearChanged();
} }

// Durchschnittsgeschwindigkeit, Anteil der Fahrzeuge unter einem bestimmten Threshold
//
} }

+ 8
- 6
app/src/main/java/de/hems/trafficsim/Vehicle.java View File

@@ -33,25 +33,27 @@ public class Vehicle {
return maxVelocity; return maxVelocity;
} }


public void updateVelocity(float distanceForerunner) {
public void updateVelocity(float distanceForerunner, float timeStep) {
Random random = new Random(); Random random = new Random();


float r = random.nextFloat(); float r = random.nextFloat();


if (curVelocity < maxVelocity) { if (curVelocity < maxVelocity) {
curVelocity = curVelocity + 1;
curVelocity = curVelocity + 1*timeStep;
} }
if (r < brakeProp && curVelocity > 0) { if (r < brakeProp && curVelocity > 0) {
curVelocity = getCurVelocity() - 1; 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) { public void timeElapse(float timeStep) {
position = (position + curVelocity) % this.trackLength;
position = (position + curVelocity*timeStep) % this.trackLength;
} }


} }


+ 6
- 1
app/src/main/java/de/hems/trafficsim/Worker.java View File

@@ -1,5 +1,7 @@
package de.hems.trafficsim; package de.hems.trafficsim;


import java.util.Date;

public class Worker extends Thread { public class Worker extends Thread {
protected Track track; protected Track track;
protected boolean stop; protected boolean stop;
@@ -15,8 +17,11 @@ public class Worker extends Thread {


@Override @Override
public void run() { public void run() {
long lastStartTime = System.currentTimeMillis();
while (!stop) { while (!stop) {
this.track.timeElapse(50);
long startTime = System.currentTimeMillis();
this.track.timeElapse((startTime - lastStartTime));
lastStartTime = startTime;
} }
} }
} }

Loading…
Cancel
Save