1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.jcurl.demo.util;
20
21 import org.jcurl.core.api.RockDouble;
22 import org.jcurl.core.api.RockSet;
23 import org.jcurl.core.api.RockSetUtils;
24 import org.jcurl.core.api.TrajectorySet;
25 import org.jcurl.core.api.RockType.Pos;
26 import org.jcurl.core.api.RockType.Vel;
27
28
29
30
31
32
33
34
35 public class RealTimePlayer implements Runnable {
36
37 private final TrajectorySet src;
38
39 private double t0Last;
40
41 private final double t0Start;
42
43 private volatile double timeScale;
44
45 private final long timeSleep = 25;
46
47 private double tNow;
48
49 public RealTimePlayer(final double t0, final double scale,
50 final TrajectorySet src) {
51 t0Start = t0Last = tNow = t0;
52 timeScale = scale;
53 this.src = src;
54 }
55
56
57
58
59 public double getTimeScale() {
60 return timeScale;
61 }
62
63
64
65
66
67 public void run() {
68 try {
69 final RockSet<Pos> pos = RockSetUtils.allHome(null);
70 final RockSet<Vel> speed = new RockSet<Vel>(new RockDouble<Vel>());
71 final long start = System.currentTimeMillis();
72 for (;;) {
73 final long dt = System.currentTimeMillis() - start;
74 tNow = t0Last + dt * timeScale * 1e-3;
75
76 src.setCurrentTime(tNow);
77 if (0 == RockSet.nonZero(src.getCurrentVel())) {
78 t0Last = t0Start;
79 break;
80 }
81 try {
82 Thread.sleep(timeSleep);
83 } catch (final InterruptedException e) {
84 t0Last = tNow;
85 break;
86 }
87 }
88 } catch (final Exception e) {
89 System.err.println(e.toString());
90 e.printStackTrace(System.err);
91 }
92 }
93
94
95
96
97
98 public void setTimeScale(final double timeScale) {
99 t0Last = tNow;
100 this.timeScale = timeScale;
101 }
102 }