Partially implement timesteps
This commit is contained in:
		
							parent
							
								
									58054d6eea
								
							
						
					
					
						commit
						4b3ba0e22c
					
				@ -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);
 | 
			
		||||
        //}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -27,7 +27,7 @@ public class Track extends Observable {
 | 
			
		||||
    protected List<Vehicle> createVehiclesList(int numberVehicles){
 | 
			
		||||
        List<Vehicle> result = new ArrayList<>();
 | 
			
		||||
        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);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -46,12 +46,12 @@ public class Track extends Observable {
 | 
			
		||||
            if(distanceForerunner < 0.0){
 | 
			
		||||
                distanceForerunner += this.trackLength;
 | 
			
		||||
            }
 | 
			
		||||
            v.updateVelocity(distanceForerunner);
 | 
			
		||||
            v.updateVelocity(distanceForerunner, timeStep);
 | 
			
		||||
        }
 | 
			
		||||
        List <VehicleTimeRecord> 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
 | 
			
		||||
    //
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user